table.columns is not a function in datatable.js

前端 未结 8 1626
鱼传尺愫
鱼传尺愫 2021-02-01 13:12



        
相关标签:
8条回答
  • 2021-02-01 13:26

    I know this is a 2 year old post, but it is still on top search results on Google when searching for this problem. So I had the same problem but I fixed it without changing code. The problem by my code was that I used a older jQuery or Datatables version (not sure which one caused trouble) so I generatet a new link on the Datatables site:

    https://datatables.net/download/index

    with including jQuery2.x and the rest left on default:

    <script type="text/javascript" src="https://cdn.datatables.net/v/dt/dt-1.10.12/datatables.min.js"></script>

    Therefore you have to remove your included jQuery Library and the Datatables Library, because both of them is included in this link. After that everything work fine without any error...

    So I figured out why the problem is on the older versions: In the older versions of Datatables the datatable was called with the function:

    $('#dt1').dataTable();
    

    and so and old version of the table was given back, which NOT included the function <tablevarname>.columns() so the calling new function: $('#dt1').DataTable(); should fix it (as Rizzim already said) but to do this you have to update your datatables, so the function is included...

    (Sorry for bad english)

    0 讨论(0)
  • 2021-02-01 13:27

    Thanks for your help. I had the same error message. But after trying nearly everything I found out, that my error was simply not having a

    <tfoot> ... </tfoot>
    block in my dataTable. Inserting this fixed it and my dataTable could append the search-inputs to this tfoot.

    0 讨论(0)
  • 2021-02-01 13:29

    Try something like this

    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)})
    });
    
    0 讨论(0)
  • 2021-02-01 13:30

    Try changing

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

    to

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

    ... that is, capitalize the DataTable(). Source: https://datatables.net/manual/api#Accessing-the-API

    0 讨论(0)
  • 2021-02-01 13:31

    Change:

    table.columns()
    

    to:

    table.api().columns()
    

    It worked for me.

    0 讨论(0)
  • 2021-02-01 13:40

    I was trying this with a makeEditable() function of dataTables. If I change dataTables() with DataTables() it doesn't work.

    The answer of h0mayun works for me. Just in case if someone else search for this problem I'm adding a little thing with h0mayun's comments.

    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)})
    });
    

    And remove the following part

    // 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();
                });
            });
    

    Hope it'll help someone.

    0 讨论(0)
提交回复
热议问题