[英]Call javascript function multiple times with different json-array
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: 我从我的PHP脚本中调用该函数,如下所示:
$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. 当我两次或多次调用函数时,问题是json-Object Array总是被最后一个覆盖。
Is there something like OOP in Javascript? Javascript中是否有类似OOP的内容? 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. 我为div和Sort-Links生成了唯一的ID,但随后仅使用了最新的JSON对象。
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. 为了回答您的紧迫问题,使用javascript的内置对象应该可以解决您的问题。
var str = "something"; // choose your own key
this.store = {}
this.store[str] = JSON.stringify(json);
or 要么
this.array = []; // in your initializer or constructor
this.array.push(JSON.stringify(json)); // this will add to the array as you go
In this way you can keep a history of json without overwriting what you had before. 这样,您可以保留json的历史记录,而不会覆盖以前的内容。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.