【问题标题】:Cannot initialize Row Reordering plugin - rowReordering is not a function无法初始化 Row Reordering 插件 - rowReordering 不是函数
【发布时间】:2015-10-04 23:07:09
【问题描述】:

我正在使用带有 Row Reordering add-on 的 jQuery DataTables,由于某种原因,我收到以下错误消息:

未捕获的 TypeError: $(...).DataTable(...).rowReordering 不是函数

当这样做时:

$(document).ready(function() {

        $('#demo').html('<table cellpadding="0" cellspacing="0" border="0" class="display cell-border"  id="example" ></table>');

        t = $('#example').DataTable({
            "columns": 
            [
                {width: "10%", "className": "ageClass", "title": "Priority",         "data": "priority" },
                {"className": "actionClass", "title": "Action",         "data": "action" },
            ],
            "bPaginate": false,
            "bLengthChange": false,
            "bFilter": false,
            "bInfo": false,
            "bAutoWidth": false,
            "scrollY":        "200px",
            "scrollCollapse": true,
            "paging":         false
        }).rowReordering();;
        // This line is where the console says the error is
        for (var i = 0; i < 10; i ++)
        {
            t.row.add(
            {
                priority: i,
                action: i,
            }).draw();
        }    
    });

HTML:

<div id="demo"> </div>

我只是在做这里描述的事情: https://code.google.com/p/jquery-datatables-row-reordering/wiki/Index

【问题讨论】:

    标签: javascript jquery html datatables row


    【解决方案1】:

    原因

    Row Reordering add-on与DataTables 1.10不兼容。

    $(selector).DataTable() 方法是在上次更新 Row Reordering Add-on 后在 DataTables 1.10 中添加的。

    解决方案

    适用于 DataTables 1.9

    要使用rowReordering(),您需要将表初始化为$('#example').dataTable(),而不是$('#example').DataTable()

    适用于 DataTables 1.10

    我有 forked the add-on on github 并添加了对 DataTables 1.10 的支持 通过使用comments 中的建议。

    查看jQuery DataTables - Row Reordering文章了解更多详情和演示。

    演示

    $(document).ready( function () {
       var table = $('#example').DataTable({
          "createdRow": function( row, data, dataIndex ) {
             $(row).attr('id', 'row-' + dataIndex);
          }    
       });
    
       for(var i = 1; i <= 100; i++){
          table.row.add([ 
             i,
             i + '.2',
             i + '.3',
             i + '.4',
             i + '.5',
             i + '.6'
          ]);
       }  
       
       table.draw();
       
       table.rowReordering();
    } );
    <!DOCTYPE html>
    <html>
    
    <head>
    <meta charset=utf-8 />
    <title>jQuery DataTables</title>  
    <link href="//cdn.datatables.net/1.10.7/css/jquery.dataTables.min.css" rel="stylesheet" type="text/css" />
    <script src="http://code.jquery.com/jquery-1.11.3.min.js"></script>
    <script src="http://cdn.datatables.net/1.10.7/js/jquery.dataTables.min.js"></script>
    <script src="https://code.jquery.com/ui/1.9.2/jquery-ui.min.js"></script>
    <script src="http://mpryvkin.github.io/jquery-datatables-row-reordering/1.2.3/jquery.dataTables.rowReordering.js"></script>
    
    </head>
      
    <body>
    <table id="example" class="display" width="100%">
    <thead>
    <tr>
      <th>Name</th>
      <th>Position</th>
      <th>Office</th>
      <th>Age</th>
      <th>Start date</th>
      <th>Salary</th>
    </tr>
    </thead>
    
    <tfoot>
    <tr>
      <th>Name</th>
      <th>Position</th>
      <th>Office</th>
      <th>Age</th>
      <th>Start date</th>
      <th>Salary</th>
    </tr>
    </tfoot>
    
    <tbody>
    </tbody>
    </table>
    </body>
    </html>

    【讨论】:

    • 然后我收到这个错误:Uncaught TypeError: $(...).disableSelection is not a function @ jquery.dataTables.rowReordering.js:202
    • @user3262713, disableSelection() 是一个 jQuery UI 方法。您很可能忘记加载行重排序插件所需的 jQuery UI。
    • 谢谢。那解决了它。但是现在我的 row.add 函数不再起作用了。
    • @user3262713,请改用t.api().row.add
    • 抱歉回复晚了,但是当我使用上面的代码和你的更正时出现这个奇怪的语法错误:error
    猜你喜欢
    • 2013-06-02
    • 1970-01-01
    • 2017-06-10
    • 2015-08-09
    • 1970-01-01
    • 2017-06-16
    • 2014-04-23
    • 1970-01-01
    • 2012-05-03
    相关资源
    最近更新 更多