How to update _other_ rows after edit?
How to update _other_ rows after edit?
I'm using DataTables with Editor to edit a MySQL table. I need to implement a custom logic when editing the "single" column. Here's my table structure and initial data:
CREATE TABLE tasks (
id INT PRIMARY KEY,
`group` VARCHAR(50),
sum INT,
single INT,
result VARCHAR(10)
);
INSERT INTO tasks VALUES
(1, 'task_a', 100, 5, 'fail'),
(2, 'task_a', 100, 94, 'fail'),
(3, 'task_a', 100, 2, 'fail'),
(4, 'task_b', 20, 20, 'success'),
(5, 'task_b', 20, 0, 'success');
I've set up DataTables Editor to edit only the single
column, and now I want to implement the following logic when this single
column is edited:
When a value for group task_a
is changed, collect other all rows where group is also task_a
and calculate the sum of all single
values.
If the calculated sum equals the sum
value of the edited row, then update all rows where group is task_a
to result
=> success
.
How can I implement this logic using DataTables Editor? I'm particularly unsure about:
- How to update multiple rows based on this calculation.
- How to refresh the DataTable to show the updated results.
Answers
Use "writeCreate" and "writeEdit". Then make the calculations and your db-updates as required.
https://editor.datatables.net/manual/php/events
If you want to do the db-updates using Editor's db methods here is more about this:
https://datatables.net/forums/discussion/80224/child-created-updated-upon-creation-update-of-the-parent
@rf1234, thanks for this suggestion and especially thanks for your helpful update() and $db->insert() examples">examples on $db->update() and $db->insert()!
I've resorted to "postEdit" and "postCreate" instead, as I need the full $row in order to access the
group
value needed for updating other rows of that samegroup
. $row seems to be available in postEdit/Create methods, only.However, neither will trigger a refresh of these other updated rows. Hence, my follow-up question:
Is it possible to have my PHP postEdit event trigger a table refresh, and in particular a refresh of only the updated rows? (A complete table refresh would take too much time.)