table.columns不是datatable.js中的函数

use*_*790 37 javascript asp.net datatables

<script>

    jQuery(document).ready(function () {


        $('#sample_3 tfoot th').each(function () {

            var title = $('#sample_3 thead th').eq($(this).index()).text();

            $(this).html('<input type="text" placeholder="Search ' + title + '" />');
        });

        // DataTable
        var table = $('#sample_3').dataTable();

        // Apply the filter
        table.columns().eq(0).each(function (colIdx) {

            $('input', table.column(colIdx).footer()).on('keyup change', function () {

                table
                    .column(colIdx)
                    .search(this.value)
                    .draw();
            });
        });

    });
</script>
Run Code Online (Sandbox Code Playgroud)

我得到table.columns不是函数js错误,缺少什么我不明白.

来源:https://datatables.net/examples/api/multi_filter.html

seb*_*n_k 128

尝试改变

var table = $('#sample_3').dataTable();
Run Code Online (Sandbox Code Playgroud)

var table = $('#sample_3').DataTable();
Run Code Online (Sandbox Code Playgroud)

......就是把资本化DataTable().资料来源:https://datatables.net/manual/api#Accessing-the-API

  • 这需要标记为正确答案. (12认同)

小智 31

更改:

table.columns()
Run Code Online (Sandbox Code Playgroud)

至:

table.api().columns()
Run Code Online (Sandbox Code Playgroud)

它对我有用.

  • 如果有人想使用 table.dataTable 而不是 table.DataTable,这是正确的方法。(见大写“d”区别) (2认同)
  • 将其更改为 otable.api().cloumns() 给了我 `Uncaught TypeError: otable.api is not a function` 错误 (2认同)

Nad*_*man 5

我正在尝试使用dataTables的makeEditable()函数.如果我用DataTables()更改dataTables(),它就不起作用.

h0mayun的答案对我有用.以防万一其他人搜索这个问题我在h0mayun的评论中添加了一些东西.

var table = $('#sample_3').dataTable();
$('#sample_3 tfoot th').each(function (i) 
{

            var title = $('#sample_3 thead th').eq($(this).index()).text();
            // or just var title = $('#sample_3 thead th').text();
            var serach = '<input type="text" placeholder="Search ' + title + '" />';
            $(this).html('');
            $(serach).appendTo(this).keyup(function(){table.fnFilter($(this).val(),i)})
});
Run Code Online (Sandbox Code Playgroud)

并删除以下部分

// Apply the filter
        table.columns().eq(0).each(function (colIdx) {

            $('input', table.column(colIdx).footer()).on('keyup change', function () {

                table
                    .column(colIdx)
                    .search(this.value)
                    .draw();
            });
        });
Run Code Online (Sandbox Code Playgroud)

希望它能帮助别人.