Soft delete from a datatable field in editor
Soft delete from a datatable field in editor

I am following https://editor.datatables.net/examples/api/softDelete.html to try to do a soft delete. This is coming from a datatable that is defined under a field in the editor.
I am writing a function under action and I haven't been able to figure out how to get data from the row that is being deleted as I want to include it in the prompt to the user:
buttons: [
{ extend: 'edit', editor: commentsEditor },
{ extend: 'selected',
text: 'Delete',
action: function (e, dt, node, config) {
var rows = dt.rows({ selected: true }).indexes();
let data = rows.data();
console.log('rows data[0]=',data[0];
//from the console:
//rows data[0]=
// Object { DT_RowId: "row_21", ActLog: {…}, view_Residents: {…}, Act: {…} }
Basically I want to return data from any field name in the underlying datatable and prompt the user if they are sure they want to delete the row. Once they confirm, then I want to update a few different fields.
Below are the field names generated by the php:
Field::inst( 'ActLog.ActLogAID' )->set(false),
Field::inst( 'ActLog.ActAID' )->set(false),
Field::inst( 'ActLog.UserAID' )->set(false),
Field::inst( 'ActLog.AddressAID' )->set(false),
Field::inst( 'ActLog.ResidentAID' )->set(false),
Field::inst( 'ActLog.ActLogDT' ),
Field::inst( 'ActLog.ActLogComments' ),
Field::inst( 'ActLog.ReqFollowUp' ),
Field::inst( 'ActLog.FollowedUp' ),
Field::inst( 'ActLog.ActLogInactive' ),
Field::inst( 'ActLog.ActLogInactiveDateT' )
->setFormatter( Format::ifEmpty( null ) ),
Field::inst( 'ActLog.ActLogInactiveUserAID' ),
Field::inst( 'view_Residents.FullNameParty')->set(false),
Field::inst( 'Act.ActName')->set(false),
Field::inst( 'Act.ActSort')->set(false),
Field::inst( 'Act.ColorAID')->set(false),
Field::inst( 'Act.Rollup_Name')->set(false),
Field::inst( 'Act.ActInactive')->set(false)
Edited by Allan - Syntax highlighting. Details on how to highlight code using markdown can be found in this guide.
This question has an accepted answers - jump to answer
Answers
Actually I figured out how to look at specific columns:
console.log('rows data[0]=',data[0]);
console.log('rows data[0][DT_RowId]=',data[0]['DT_RowId']);
console.log('rows data[0][ActLog]=',data[0]['ActLog']);
console.log('rows data[0][ActLog][ActLogAID]=',data[0]['ActLog']['ActLogAID']);
console.log('rows data[0][ActLog][ActLogDT]=',data[0]['ActLog']['ActLogDT']);
console.log('rows data[0][Act][ActName]=',data[0]['Act']['ActName']);
Now I want to update a few fields in the underlying datafield datatable type once the user confirms.
Yup - nicely done. I would suggest a slight tweak:
No need to call
rows().indexes()
first, unless you actually need the index for something, which you don't appear to.Keep in mind that unless you configure Select to allow single row selection only, then the
data
array might have multiple entries, so you might want to use afor
loop to extract data for each row.Allan
Thank you. Down below in the code, there is a prompt that tests to see how many rows are selected but I don't know if I have that option enabled to allow multiple row selection.
I did see that no matter what row I am currently on, the row that is being referenced by the js always the first row in the datafield datatable.
This code:
action: function (e, dt, node, config) {
var rows = dt.rows({ selected: true }).indexes();
let data = rows.data();
When I analyze the data array, it's not the selected row. Any thoughts as to why?
As Allan said don't use
rows().indexes()
. Just us what he suggested above. For example:https://live.datatables.net/dawemebo/1/edit
Kevin
I understand, thank you. I was able to read the values and it's from the correct row.
Next issue (sorry), I get the confirmation, when I click update, I get an error. Below is from the PHP:
$_POST= {"action":"edit","AddressAID":"56"}
The error that I see in devtools is
<br />
<b>Warning</b>: foreach() argument must be of type array|object, null given in <b>C:\ClientSites\da-pool.com\jyorkejudge4.org\php\lib\Editor.php</b> on line <b>1050</b><br />
<br />
<b>Warning</b>: foreach() argument must be of type array|object, null given in <b>C:\ClientSites\da-pool.com\jyorkejudge4.org\php\lib\Editor.php</b> on line <b>845</b><br />
<br />
<b>Warning</b>: foreach() argument must be of type array|object, null given in <b>C:\ClientSites\da-pool.com\jyorkejudge4.org\php\lib\Editor.php</b> on line <b>1074</b><br />
{"data":[],"options":{"Act.ActInactive":[]},"debug":["Editor PHP libraries - version 2.4.3"]}
below is the code that is prompting for confirmation then setting hidden fields on the commentsEditor form:
That is this line in the Editor.php file.
The error suggests that
data
doesn't exist in the submitted data, which is part of the Editor client / server data interchange.That appears to be true from:
Are you modifying the submitted data on the client or server-side to make it look like that?
Allan