Custom edit to edit record and creat

Custom edit to edit record and creat

info@invisiblefarm.itinfo@invisiblefarm.it Posts: 11Questions: 4Answers: 0

Description of problem:
Hi, I'm using .NET Editor 1.9.2
I need to create a custom edit button: this button must do
1. update one column for the selected row
2. open modal to edit row
3. create a new record in DB; data source is modal (point 2) whitout delete the selected row

I try to do my goal with this example:
* Customised Control Button example to update one column for the selected row
* Duplicate Button to open modal (for the new value) and to create record in DB

This is the code: it's update the desiderata colum, it's open the desiderata modal BUT when I click 'Update' modal button it's insert the new row AND delete the selected row

{
    extend: "selectedSingle",
    text: "Duplicate",
    action: function (e, dt, node, config) {

        editor
            // Spengo quella che devo modificare
            .edit(main_table.row({ selected: true }).index(), false)
            .set('valid_to', (new Date()).toISOString().split('T')[0])
            .submit();

        editor
            // Creo il nuovo record
            .edit(main_table.row({ selected: true }).index(), {
                title: 'Edit entry',
                buttons: 'Update'
            })
            .mode('create');
    }
}

Where is my error?
Thanks

This question has an accepted answers - jump to answer

Answers

  • colincolin Posts: 15,143Questions: 1Answers: 2,586

    I'm not seeing that here - I've taken your code and it's doing what you're describing. Could you look at that, please, and see if it helps. If it's still not working for you, please can you update my example, or link to your page, so that we can see the problem.

    Cheers,

    Colin

  • info@invisiblefarm.itinfo@invisiblefarm.it Posts: 11Questions: 4Answers: 0
    edited June 2020

    Hi @colin , thanks for the help.
    It works good on client-side but on server-side it is always in edit mode: in this way the selected row is going to update with duplicate entry.
    It's seem that .mode('create') doesn't change the editor behavior

  • allanallan Posts: 61,715Questions: 1Answers: 10,108 Site admin
    Answer ✓

    The key thing to remember here is that you cannot have Editor running two operations at the same time and the operations it does perform are asynchronous, so you need to wait for one to complete before the other can happen - otherwise - well who knows what will happen!

    The subnmitComplete can be used to handle this, or the callback option of the submit() method - e.g.:

    {
      extend: "selectedSingle",
      text: "Duplicate",
      action: function (e, dt, node, config) {
        editor
          // Spengo quella che devo modificare
          .edit(main_table.row({ selected: true }).index(), false)
          .set("valid_to", new Date().toISOString().split("T")[0])
          .submit(function () {
            editor
              // Creo il nuovo record
              .edit(main_table.row({ selected: true }).index(), {
                title: "Edit entry",
                buttons: "Update",
              })
              .mode("create");
          });
      },
    };
    

    Allan

  • info@invisiblefarm.itinfo@invisiblefarm.it Posts: 11Questions: 4Answers: 0

    Hi @allan ,
    This code work like a charm!
    editor.on('submitComplete', function (e, json, data, action) {
    if (action === "edit") {
    editor
    // Creo il nuovo record
    .edit(main_table.row({ selected: true }).index(), {
    title: "Edit entry",
    buttons: "Update",
    })
    .mode("create");
    }
    });

    ...But I use Soft Delete to (logical) remove the DB record.
    Soft Delete works in edit mode: how can I understand if it's Duplicate or Soft Delete inside submitComplet?

    Thanks

  • allanallan Posts: 61,715Questions: 1Answers: 10,108 Site admin

    You probably need to set a variable on each action:

    var editType = '';
    
    // Inside soft action delete function
    editType = 'softDelete';
    
    // Inside duplicate action function
    editType = 'duplicate';
    

    I'm not sure there is a better way to do it at the moment.

    Allan

  • info@invisiblefarm.itinfo@invisiblefarm.it Posts: 11Questions: 4Answers: 0

    Thanks @allan , I've found the solution.
    In my case, edit operation is a soft delete più duplicate: so I decide to hide edit button and use a custom button "Edit & Delete". In this way, first operation is soft delete; next user can edit table row with duplicate

    This is "Edit & Delete" button

    {
        extend: "selectedSingle",
        text: 'Edit&Delete',
        action: function ( e, dt, node, config ) {
            var rows = main_table.rows( {selected: true} ).indexes();
    
            editor
                .hide( editor.fields() )
                .one( 'close', function () {
                    setTimeout( function () { // Wait for animation
                        editor.show( editor.fields() );
                    }, 500 );
                } )
                .edit( rows, {
                    title: 'Delete',
                    message: 'Are you sure you wish to delete this row?',
                    buttons: 'Delete' 
                } )
                .val('valid_to', (new Date()).toISOString().split('T')[0]);
        }
    }
    

    On submitComplete, I do this

    // Azione scatenata quando è stata completata la submit
    // discriminando sulla action
    editor.on('submitComplete', function (e, json, data, action) {
        if (action === "edit") {
            editor
                // Creo il nuovo record
                .message() // Flush 'Are you sure you wish to delete this row?' message
                .edit(main_table.row({ selected: true }).index(), {
                    title: "Edit entry",
                    buttons: "Update",
                })
                .mode("create");
        }
    });
    

    Thanks again!

This discussion has been closed.