0

I am using DataTables multi item selection and like to submit the selected rows to my form.

Of course, I could manually create an input element for each row in my table:

<input type="hidden" id="row_0" name="par" value="456" disabled="disabled" />
<input type="hidden" id="row_1" name="par" value="876" disabled="disabled" />
...

and then toggle the disabled attribute based on the row selection. Something like this:

for (let row of table.rows({ selected: true })) {
  $(`#row_${row.index()}`).removeAttr("disabled");
}

But maybe there is an easier solution which requires less coding, I did not find any.

1 Answer 1

0

I think, option columns.render is the solution I was looking for.

Should be similar to this (not tested yet):

new DataTable('#myTable', {
    columnDefs: [
        {
            targets: -1,
            visible: false,
            searchable: false,            
            render: function (data, type, row, meta) {
               if ( row.selected() ) {               
                  return `<input type="hidden" name="par" value="${row.id}" />`;
               } else {
                  return null;
               }
            }
        }
    ]
});

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.