Soft delete from a datatable field in editor

Soft delete from a datatable field in editor

crcucbcrcucb Posts: 47Questions: 13Answers: 0
edited July 17 in Free community support

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

  • crcucbcrcucb Posts: 47Questions: 13Answers: 0

    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.

  • allanallan Posts: 64,756Questions: 1Answers: 10,716 Site admin

    Yup - nicely done. I would suggest a slight tweak:

    let data = dt.rows({ selected: true }).data();
    

    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 a for loop to extract data for each row.

    Allan

  • crcucbcrcucb Posts: 47Questions: 13Answers: 0

    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?

  • kthorngrenkthorngren Posts: 22,162Questions: 26Answers: 5,102

    As Allan said don't use rows().indexes(). Just us what he suggested above. For example:
    https://live.datatables.net/dawemebo/1/edit

    Kevin

  • crcucbcrcucb Posts: 47Questions: 13Answers: 0

    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:

                        commentsEditor
                            .hide(commentsEditor.fields())
                            .one('close', function () {
                                setTimeout(function () {
                                    // Wait for animation
                                    commentsEditor.show(commentsEditor.fields());
                                }, 500);
                            })
                            .edit(data, {
                                title: 'Delete',
                                message:
                                    data.length === 1
                                        ? 'Are you sure you wish to delete row for ' + varConfirmation + '?'
                                        : 'Are you sure you wish to delete these ' +
                                          rows.length + ' rows',
                                buttons: 'Delete'
                            })
                            .set( 'ActLog.ActLogInactiveDateT' , new Date().toISOString().split('T')[0] )
                            .set( 'ActLog.ActLogInactive' , 1 )
                            .set( 'ActLog.ActLogInactiveUserAID' , varUserID );
    
  • allanallan Posts: 64,756Questions: 1Answers: 10,716 Site admin
    Answer ✓

    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:

    $_POST= {"action":"edit","AddressAID":"56"}
    

    Are you modifying the submitted data on the client or server-side to make it look like that?

    Allan

Sign In or Register to comment.