I got this code:
function datatable_display(json) {
var order = "desc";
var len = $('.div_gui_tabledata').length;
len = len++;
var table = $('#dynamic_' + len);
document.write("<div class='div_gui_tabledata' id='dynamic_" + len + "' >");
this.array = JSON.stringify(json);
$.ajax({
type: "POST",
url: "../../api.php?p=datatable_sort",
data: "sortby=2&data=" + array,
dataType: "html",
success: function (data) {
$('#dynamic_' + len).html(data);
}
});
document.write("</div>");
$('#dynamic_' + len + ' .sortable').live("click", function () {
var index = $(this).index();
$.ajax({
type: "POST",
url: "../../api.php?p=datatable_sort",
data: "order=" + order + "&sortby=" + index + "&data=" + array,
dataType: "html",
success: function (data) {
$('#dynamic_' + len).html(data);
if (order == "desc")
order = "asc";
else
order = "desc";
}
});
});
}
I call this function from my PHP-Script like that:
$json = json_encode($this->arr);
return "<script>datatable_display($json);</script></div>";
When I call the function twice or even more times, the problem is the json-Object Array is always overwritten by the last.
Is there something like OOP in Javascript? Or something that threatens each function call unique? I generate unique ids for the divs and the Sort-Links, but then only the latest JSON Object is used.
There are a variety of architectural things you could be doing differently to make life a lot easier for yourself, but you’re likely to discover these in time on your own. To answer your immediate question, using javascript’s built-in objects should solve your problem.
or
In this way you can keep a history of json without overwriting what you had before.