How would I call both the edit and create from a single button?

How would I call both the edit and create from a single button?

WCGPR0WCGPR0 Posts: 4Questions: 3Answers: 0

I'm trying to edit (soft delete) a row, and also create a new row (both makes ajax calls) from a single button.

var buttons = [ {
    label: 'Delete And Create'
    editor: editor,
    fn: function() {
        editor.field('Deleted').set(true);
        editor.submit();
        editor.field('Deleted').set(false);
        editor.create().submit(); //This isn't working, I think the form is being cleared
    }
}];

editor.edit( indexes, { buttons: buttons });

How would I be able to get a button that first edits a row, set the deleted field to true, and then, create a new row with deleted field to false?

This question has an accepted answers - jump to answer

Answers

  • rf1234rf1234 Posts: 2,944Questions: 87Answers: 416
    Answer ✓

    Here is a custom button called "deleteAndCreate" that should do the job. It requires Editor's "Select" extension.

    //custom button for edit / create = Allan's "duplicate" button
    $.fn.dataTable.ext.buttons.deleteAndCreate = {
        extend: 'selected',
        text: 'Delete And Create',
        className: "deleteAndCreate",
        action: function ( e, dt, node, config ) {
            //do the soft delete without showing the form
            editor
                .edit( dt.rows( {selected: true} ).indexes(), false )
                .set( 'Deleted', true )
                .submit();
            // Start editor in edit mode with the form, and then change to create
            editor
                .title($.fn.dataTable.Editor.defaults.i18n.create.title)
                .set( 'Deleted', false )
                .buttons({
                        label: $.fn.dataTable.Editor.defaults.i18n.create.submit,
                        fn: function () {
                            this.submit();
                        }
                    })
                .edit( dt.rows( {selected: true} ).indexes() )
                .mode( 'create' );
        }
    };
    

    Use it like this in your data table definition:

    ..............
    select: {
        style:    'single'
    },
    ......
    buttons: [
    .......
            {   extend: "deleteAndCreate ", editor: yourEditor}
    ......
    
This discussion has been closed.