Conditionally add a row in DataTables (jQuery library)

Conditionally add a row in DataTables (jQuery library)

ahardimanahardiman Posts: 1Questions: 1Answers: 0

I have a DataTable that is used on a number of different pages in my web-application. The table has an html button tag, that adds a new row to the table by way a modal form, completed by the end-user.

On change, the following is 'executed'.

var actionArray = ['<input type="radio" value="' + data._id.$oid + '" data-owner="1" name="selectTask" class="minimal selectTask">' + '<input type="hidden" value="' + data._id.$oid + '" name="associatedActions[]" class="minimal">', ' ' + data.title, ' ' + data.category, ' ' + data.originator.name, ' ' + data.assigned.name, ' ' + moment(data.dueDate.date + data.dueDate.timezone).format('DD MMM YY'), actionStatuses[data.status], filesHTML];

I have an array, actionArray, and depending on whether or not the condition (data.assigned._id.$oid === data.originator._id.$oid) is true, I either write to one datatable or two, with the following:

Part Two:

var myActionsArray = JSON.parse(JSON.stringify(actionArray)); var setActionsArray = JSON.parse(JSON.stringify(actionArray));

myActionsArray.splice(3, 1); setActionsArray.splice(4, 1);

if (data.assigned._id.$oid === data.originator._id.$oid) { $('#myActions').DataTable().rows.add([myActionsArray]).draw(); $('#myActions').DataTable().columns.adjust().draw(); $('#setActions').DataTable().rows.add([setActionsArray]).draw(); $('#setActions').DataTable().columns.adjust().draw(); } else { $('#setActions').DataTable().rows.add([setActionsArray]).draw(); $('#setActions').DataTable().columns.adjust().draw(); }

If the assigned._id is equal to the originator._id both tables are written to, otherwise it is just the #setActions table.

This all works just fine. My problem is that the table appears on numerous other web-pages also, when a row is added to the table elsewhere on the site, I need to just write the orginal array to the table, i.e. actionArray. Therefore, I need to make 'part two' of the code above conditional on where the 'on change' is being 'called' from, i.e. has the add row to DataTable request come from one of the two tables, #myActions or #setActions, or from elsewhere.

I need something akin to $(this).attr("id");, so I can make 'part two' conditional, and instead run:

DataTable().rows.add([actionArray]).draw()
How can I make the addition of a row in a Datatable conditional, as to the table the request is initiated from?

This discussion has been closed.