Ruby on Rails中的DataTables行详细信息 - 如何从Javascript中分离HTML

时间:2022-08-07 15:15:46

I am using DataTables jQuery plugin in a Ruby on Rails project (directly, not through a datatables-rails gem) and I need to display row details as explained here: https://datatables.net/examples/server_side/row_details.html

我在Ruby on Rails项目中使用DataTables jQuery插件(直接,而不是通过datatables-rails gem),我需要显示行详细信息,如下所述:https://datatables.net/examples/server_side/row_details.html

In my case, after calling $("#table_id").DataTable(), I use this format() function in .js file to pass data for display to the row.child:

在我的例子中,在调用$(“#table_id”)。DataTable()之后,我在.js文件中使用这个format()函数将数据传递给row.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>';
}

This works fine, however it looks messy with all this HTML in the .js asset file. I want to understand if there is a way to extract the HTML (possibly in a partial).

这工作正常,但是.js资产文件中的所有HTML看起来都很混乱。我想了解是否有提取HTML的方法(可能是部分)。

1 个解决方案

#1


1  

Sure you can. Use form_for or simple_form_for to create the form in a template (make it a partial if you wish). And then in js code you just need to call $("#table_id").DataTable() (of course table_id is the id of your table here, and you can pass in options at the same time).

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

#1


1  

Sure you can. Use form_for or simple_form_for to create the form in a template (make it a partial if you wish). And then in js code you just need to call $("#table_id").DataTable() (of course table_id is the id of your table here, and you can pass in options at the same time).

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