Ruby on rails RubyonRails中的DataTables行详细信息-如何将HTML与Javascript分离

Ruby on rails RubyonRails中的DataTables行详细信息-如何将HTML与Javascript分离,ruby-on-rails,datatables,Ruby On Rails,Datatables,我在Ruby on Rails项目中使用DataTables jQuery插件(直接,而不是通过DataTables Rails gem),我需要显示行详细信息,如下所述: 在我的例子中,在调用$(“#table_id”).DataTable()后,我使用.js文件中的这个format()函数将要显示的数据传递给行.child: /* Formatting function for row details */ function format(d) { // `d` is the or

我在Ruby on Rails项目中使用DataTables jQuery插件(直接,而不是通过DataTables Rails gem),我需要显示行详细信息,如下所述:

在我的例子中,在调用
$(“#table_id”).DataTable()后,我使用.js文件中的这个format()函数将要显示的数据传递给
行.child

/* Formatting function for row details */
function format(d) {
    // `d` is the original data object for the row
    return '<table cellpadding="5" cellspacing="0" border="0" style="padding-left:50px;">' +
        '<tr>' +
        '<td>' + label_variable1 + ':</td>' +
        '<td>' + d.office.name + '</td>' +
        '</tr>' +
        '<tr>' +
        '<td>' + label_variable2 + ':</td>' +
        '<td>' + d.office.location + '</td>' +
        '</tr>' +
        // Several more rows...
        '</table>';
}
/*行详细信息的格式化功能*/
函数格式(d){
//`d`是该行的原始数据对象
返回“”+
'' +
''+label_variable1+':'+
“+d.office.name+”+
'' +
'' +
''+标签_variable2+':'+
''+d.办公室.地点+''+
'' +
//还有几排。。。
'';
}

这可以很好地工作,但是在.js资产文件中,所有这些HTML看起来都很混乱。我想了解是否有办法提取HTML(可能是部分提取)。

当然可以。使用form_for或simple_form_for在模板中创建表单(如果愿意,将其设置为部分表单)。然后在js代码中,您只需调用
$(“#table_id”).DataTable()
(当然,table_id是您的表的id,您可以同时传入选项)。

谢谢!我已经在使用
$(“#table_id”).DataTable()
(编辑问题以澄清),但您的回答仍然为我指明了正确的方向。我最后做的是通过jQuery创建表(类似于这里的解释:)并从CSS资产加载其样式。