Javascript 使用DataTables动态创建时,表具有意外行为

Javascript 使用DataTables动态创建时,表具有意外行为,javascript,jquery,html,datatables,Javascript,Jquery,Html,Datatables,有人能帮我弄清楚这是怎么回事吗?表#1和表#2生成的表外观相同,但表#2缺少排序箭头和排序和搜索功能。我希望他们的工作方式相同。有什么解决办法吗? 我正在尝试对我的一张桌子进行软编码。正在对该表进行排序,并通过提供搜索功能。以下代码创建的表的外观和行为完全符合我的要求: 表格#1:(.html) 表格排序控制器.js: var $rows = $('#sortable-cells tr'); $('#search').keyup(function () { var val = '^(?

有人能帮我弄清楚这是怎么回事吗?表#1和表#2生成的表外观相同,但表#2缺少排序箭头和排序和搜索功能。我希望他们的工作方式相同。有什么解决办法吗?

我正在尝试对我的一张桌子进行软编码。正在对该表进行排序,并通过提供搜索功能。以下代码创建的表的外观和行为完全符合我的要求:

表格#1:(.html)

表格排序控制器.js:

var $rows = $('#sortable-cells tr');
$('#search').keyup(function () {

    var val = '^(?=.*\\b' + $.trim($(this).val()).split(/\s+/).join('\\b)(?=.*\\b') + ').*$',
        reg = RegExp(val, 'i'),
        text;

    $rows.show().filter(function () {
        text = $(this).text().replace(/\s+/g, ' ');
        return !reg.test(text);
    }).hide();
});

$("#search-bar").submit(function (event) {
    event.preventDefault();
});
function sortTable(id) {
alert("sorting table");
    $(document).ready(function () {
        $("#" + id).DataTable(
            {
                searching: false,
                info: false,
                paging: false
            }
        );
    });
};
几天来,我一直在努力解决这个问题(很明显,我是JS的新手),但我仍然被卡住了。我能够在data-tables.js(from)的第4245行上获得错误
uncaughttypeerror:cannotread undefined的属性'aDataSort'。尽管有很多关于此错误的问题,但没有解决方案:

Don't bother viewing these links...they are just to show the lack of knowledge associated with this problem
(http://demoscripts.info/28454203/jquery-datatables-cannot-read-property-adatasort-of-undefined.html)
(http://datatables.net/forums/discussion/21965/error-uncaught-typeerror-cannot-read-property-adatasort-of-undefined)
(https://groups.google.com/forum/#!topic/web2py/2PsyOKAOztk)
(https://github.com/l-lin/angular-datatables/issues/122)
我发现这个错误的唯一“解决方案”是,但它似乎与我的问题不匹配


关于如何使软编码表具有与硬编码表相同的搜索和排序功能,您有什么想法吗?

初始化插件后,您将向表中添加html。如果不通知插件,则无法执行此操作。请将数据传递给插件,并让它管理html呈现,或者在将html插入插件后进行初始化DOM@charlietfl从你的评论,我到,然后到。结合起来,成功!!谢谢
function sortTable(id) {
alert("sorting table");
    $(document).ready(function () {
        $("#" + id).DataTable(
            {
                searching: false,
                info: false,
                paging: false
            }
        );
    });
};
Don't bother viewing these links...they are just to show the lack of knowledge associated with this problem
(http://demoscripts.info/28454203/jquery-datatables-cannot-read-property-adatasort-of-undefined.html)
(http://datatables.net/forums/discussion/21965/error-uncaught-typeerror-cannot-read-property-adatasort-of-undefined)
(https://groups.google.com/forum/#!topic/web2py/2PsyOKAOztk)
(https://github.com/l-lin/angular-datatables/issues/122)