0

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

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:

/* 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).

1 Answer 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).

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks EJ2015! I was already using the $("#table_id").DataTable() (edited question to clarify), but still your answer pointed me to the right direction. What I ended up doing was to create the table via jQuery (similarly to how it is explained here: htmlgoodies.com/beyond/css/working_w_tables_using_jquery.html) and load its styling from CSS asset.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.