我这样做是根据服务器返回的数据创建的列数。我想检查数组中的数据是否与列名匹配,如果匹配则返回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

最佳答案

原因


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

功能应定义如下,有关更多详细信息,请参见set == 'set'

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



笔记



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

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

10-12 12:51
查看更多