javascript - Create table if data from ajax not empty -
first have html:
<div class="showtable" style="display: none;"> <table id="example" class="table table-responsitive" cellspacing="0" width="100%" style="margin-top:30px;"> <thead> <tr> <th>id</th> <th>broj računa</th> <th>kupac</th> <th>datum izdavanja</th> <th>rok za plaćanje</th> <th>status</th> <th></th> </tr> </thead> </table> </div> <div class="showinfo" style="display: none"> </div>
after write js:
$(document).ready(function() { drawvisualization(); }); function drawvisualization() { console.log('proba'); $.ajax({ url: "getracuni.php", type: "post", async: true, datatype: "html", success: function(json) { console.log(json); if (!json.data){ json.data = []; $('.showinfo').show(); } else { var podaci = json.data; $('.showtable').show(); showtable(podaci); } }, error: function(data) { console.log(data); } }); };
so here try make this: 1. if data ajax not empty create datatable function showtable(podaci); 2. if data ajax empty want show div class 'showinfo'.
my showtable(podaci) function is:
function showtable(podaci) { $('#example').datatable({ data: podaci.data, paging: false, "dom":' <"search"f><"top"l>rt<"bottom"ip><"clear">', bfilter: false, "olanguage": { "sinfo": 'ukupno _end_ računa.', "sinfoempty": 'nema podataka o računima', "semptytable": "nemate račune, dodajte klikom na dugme ispod.", }, // end , "columns": [{ "data": "id" }, { "data": "br" }, { "data": "kupac" }, { "data": "datum" }, { "data": "rok" }, { "data": "status" }, { "data": "akcija" } ], "columndefs": [ { "targets": 6, "data": "akcija", "render": function(data, type, full, meta) { // return data; return '<div style="float:right;"><div class="btn-group"><a href="#" class="btn btn-primary btn-sm dropdown-toggle" data-toggle="dropdown" aria-expanded="false">izmeni <span class="caret"></span></a><ul class="dropdown-menu"><li><a href="#">izmeni</a></li><li><a href="#">pošalji</a></li><li><a href="#">opomeni</a></li><li><a href="#">plaćeno</a></li><li><a href="#">stoniraj</a></li><li><a href="#">kopiraj</a></li><li><a href="#">obriši</a></li></ul></div> <i data-toggle="modal" data-target="#delete" class="fa fa-times"></i></div>'; } }, { "targets": 0, "visible":false } ] }); }
also data ajax in json format:
{"data":[{"id":1,"br":"1-2015","kupac":"adakolor","datum":"2015-05-19","rok":"2015-05-21","status":"placeno"},{"id":2,"br":"2-2015","kupac":"milenk","datum":"2015-05-27","rok":"2015-05-28","status":""}]}
i think write fine when try run code blank screen. wont show there...
change condition if (!json.data)
if (json.data!=null)
Comments
Post a Comment