简体   繁体   English

在Ruby on Rails中使用DataTables上的Jeditable

[英]Using Jeditable on DataTables with Ruby on Rails

I have a DataTables table: 我有一个DataTables表:

  %table.table.datatable#datatable
    %thead
      %tr
        %th Name
    %tbody
      - @cars.each do |car|
        %tr
          %td
            = car.name

And Jeditable code based on: http://datatables.net/release-datatables/examples/api/editable.html 和可编辑的代码基于: http ://datatables.net/release-datatables/examples/api/editable.html

:javascript
$(document).ready(function() {
    /* Init DataTables */
    var oTable = $('#datatable').dataTable();

    /* Apply the jEditable handlers to the table */
    oTable.$('td').editable( 'update', {
        "callback": function( sValue, y ) {
            var aPos = oTable.fnGetPosition( this );
            oTable.fnUpdate( sValue, aPos[0], aPos[1] );
        },
        "submitdata": function ( value, settings ) {
            return {
                "row_id": this.parentNode.getAttribute('id'),
                "column": oTable.fnGetPosition( this )[2]
            };
        },
        "height": "14px",
        "width": "100%"
    } );
} );

I get the following error when page loads: 页面加载时出现以下错误:

DataTables warning (table id = 'datatable'): Cannot reinitialise DataTable. DataTables警告(表ID ='datatable'):无法重新初始化DataTable。 To retrieve the DataTables object for this table, pass no arguments or see the docs for bRetrieve and bDestroy 要检索此表的DataTables对象,请不传递任何参数,或参阅bRetrieve和bDestroy的文档

Add this before: var oTable = $('#datatable').dataTable(); 在此之前添加: var oTable = $('#datatable').dataTable();

    $('#datatable').dataTable({   
      "bRetrieve":true,
      "bDestroy":true
    });

However, my table is still not editable! 但是,我的表仍然不可编辑!

I initially thought the problem was in your HAML. 我最初以为问题出在您的HAML中。 DataTables will apply it's own class of "dataTable" to your table, which I thought might be causing it to think that the table has already been initialized. DataTables会将自己的“ dataTable”类应用于您的表,我认为这可能导致它认为该表已经初始化。

From the page source for the jEditable example, you can see that DataTables adds the 'dataTable' class to the table when it initializes it. 从jEditable示例的页面源中,您可以看到DataTables在初始化表时将'dataTable'类添加到表中。

<table cellpadding="0" cellspacing="0" border="0" class="display dataTable" id="example" aria-describedby="example_info">
</table>

They only needed to initialize their table with: 他们只需要使用以下方法初始化表:

var oTable = $('#example').dataTable();

I thought that adding the class 'datatable' to the table might confuse DataTables and causes it to complain about re-initialization. 我认为将“数据表”类添加到表中可能会使DataTables感到困惑,并使它抱怨重新初始化。 But from brief testing in jsFiddle, that appears to not be the case. 但是从jsFiddle中的简短测试来看,情况似乎并非如此。 I get a post error (obviously), but no initialization errors. 我收到一个发布错误(很明显),但是没有初始化错误。

The fiddle I created is here . 我创建的小提琴在这里

Still, the only thing you should need on your table is the 'dataTable' id. 尽管如此,您在表上唯一需要的就是'dataTable'ID。

Your 'fix' doesn't actually address the problem. 您的“解决方案”实际上并未解决问题。 If you look in the DataTables (v1.9.4) source at around line 6390, you'll notice that the bDestroy flag causes any previously initialized DataTable instance to be destroyed. 如果在第6390行附近查看DataTables(v1.9.4)源,您会注意到bDestroy标志导致任何先前初始化的DataTable实例被破坏。

/* Base check on table node */
if ( DataTable.settings[i].nTable == this )
{
   if ( oInit === undefined || oInit.bRetrieve )
   {
      return DataTable.settings[i].oInstance;
   }
   else if ( oInit.bDestroy )
   {
      DataTable.settings[i].oInstance.fnDestroy();
      break;
   }
   else
   {
      _fnLog( DataTable.settings[i], 0, "Cannot reinitialise DataTable.\n\n"+
         "To retrieve the DataTables object for this table, pass no arguments or see "+
         "the docs for bRetrieve and bDestroy" );
         return;
   }
}

So that clears out the previously initialized table, and the error message goes away. 这样就清除了先前初始化的表,并且错误消息消失了。 But it doesn't resolve the issue (as you stated). 但这不能解决问题(如您所述)。

Try using a different ID for this table (call it '#editableTable' or something), and see if that fixes the problem. 尝试为该表使用其他ID(称为“ #editableTable”或类似名称),看看是否可以解决问题。 If it does, then you do have another initialized table with the same id or class. 如果是这样,那么您的确有另一个具有相同ID 类的初始化表。 If you still get the initialization error after checking that's not the case, I'd verify that you're not using pjax or turbolinks which could be causing issues. 如果在检查不是这种情况后仍然出现初始化错误,我将确认您没有使用可能导致问题的pjax或turbolinks。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM