gpt4 book ai didi

javascript - DataTables rows.add(array).draw() 被多次调用

转载 作者:行者123 更新时间:2023-12-01 05:37:48 26 4
gpt4 key购买 nike

我这样做是为了根据服务器返回的数据创建列数。我想检查数组中的数据是否与列名匹配,如果匹配则返回 yes,否则返回 no。

function initTable(userJvmsArray) {
var collumnsCallbacks = [{
title: "Username",
data: "username"
}];

for (var i = 0; i < $scope.unique.length; i++) {
$('#nameColumn').after('<th>' + $scope.unique[i] + '</th>');
var jvm = $scope.unique[i];
var col = {
title: jvm,
data: function(data, type, row) {
var data2 = data;
var jvms = $scope.unique;
for (var k = 1; k < jvms.length; k++) {
console.log(jvms);
for (var j = 0; j < data2.myarray.length; j++) {
if (table.column(k).title() == data2.myarray[j].Name) {
console.log(table.column(k).title() + " = " + data2.myarray[j].Name + " YES");
return "yes";
} else {
console.log(table.column(k).title() + " = " + data2.myarray[j].Name + " NO");
return "no";
}
}
}


}
};
collumnsCallbacks.push(col);
$scope.columns = collumnsCallbacks;
}
$scope.myTable = $('#myTable').DataTable({
'paging': true,
"pageLength": 3,
'columns': collumnsCallbacks
});

问题是该函数似乎运行了多次并且错误地填充了表,这是我的控制台日志:

["myword", "secondword", fastLoop: function, fastLoopAsc: function] myctrl.controller.js:63
user1: myword = myword YES myctrl.controller.js:66
["myword", "secondword", fastLoop: function, fastLoopAsc: function] myctrl.controller.js:63
user1: myword = myword YES myctrl.controller.js:66

["myword", "secondword", fastLoop: function, fastLoopAsc: function] myctrl.controller.js:63
user11: myword = secondword NO myctrl.controller.js:69
["myword", "secondword", fastLoop: function, fastLoopAsc: function] myctrl.controller.js:63
user11: myword = secondword NO myctrl.controller.js:69

["myword", "secondword", fastLoop: function, fastLoopAsc: function] myctrl.controller.js:63
user1: myword = myword YES myctrl.controller.js:66
["myword", "secondword", fastLoop: function, fastLoopAsc: function] myctrl.controller.js:63
user11: myword = secondword NO myctrl.controller.js:69

["myword", "secondword", fastLoop: function, fastLoopAsc: function] myctrl.controller.js:63
user1: myword = myword YES myctrl.controller.js:66
["myword", "secondword", fastLoop: function, fastLoopAsc: function] myctrl.controller.js:63
user11: myword = secondword NO myctrl.controller.js:69
["myword", "secondword", fastLoop: function, fastLoopAsc: function] myctrl.controller.js:63
user1: myword = myword YES myctrl.controller.js:66
["myword", "secondword", fastLoop: function, fastLoopAsc: function] myctrl.controller.js:63
user1: myword = myword YES myctrl.controller.js:66
["myword", "secondword", fastLoop: function, fastLoopAsc: function] myctrl.controller.js:63
user11: myword = secondword NO myctrl.controller.js:69
["myword", "secondword", fastLoop: function, fastLoopAsc: function] myctrl.controller.js:63
user11: myword = secondword NO

最佳答案

CAUSE

columns.data的函数由于 DataTables 的工作方式,会被执行多次。虽然还不清楚,但这个函数可能会被多次调用,因为 DataTables 会针对它需要的不同数据类型调用它 - 类型检测、排序、过滤和显示 (type == 'type', type == 'sort'type == 'filter'type == 'display')。还可以调用它来设置单元格的数据(set == 'set')。

函数应定义如下,参见 columns.data了解更多详情。

data: function(row, type, set, meta){ }

NOTES

  • 您可以使用columns.render它可以被认为是 columns.data 的只读伴侣。而且不太复杂。
  • 您的两个循环的逻辑存在缺陷,它们只会运行一次,因为您在内循环中立即返回“yes”“no”值。我无法更正您的代码,因为不清楚您要做什么。

关于javascript - DataTables rows.add(array).draw() 被多次调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32822552/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com