Leave the editor window open after creating a row and do not reset the form

Leave the editor window open after creating a row and do not reset the form

av-technikav-technik Posts: 4Questions: 1Answers: 0

Hello,

I would like to have the possibility to insert several entries with similar content one after the other.
For this I wanted to replace the "Create" button with two buttons: "Create" and "Create and close".
The "Create" button should create an entry but leave the form open so that I can adjust the contents of the form to create another entry.
However, with my current code there is still the problem that the form is reset after creating. Is there a way that this does not happen?

new $.fn.dataTable.Buttons(table, [
    {
        extend: 'create',
        editor: editor,
        formButtons: [
            {
                text: 'Create and close',
                className: 'btn-primary',
                action: function () {
                    this.submit();
                },
            },
            {
                text: 'Create',
                className: 'btn-secondary',
                action: function () {
                    const that = this;

                    this.submit(function () {
                        that.create();
                    }, null, null, false);
                },
            },
        ],
    },
    { extend: 'edit',   editor: editor },
    { extend: 'remove', editor: editor },
]);

Answers

  • allanallan Posts: 61,928Questions: 1Answers: 10,153 Site admin
    edited June 2023

    create() will clear out the form values and set them to the defined default (or empty if no value defined). This is by design.

    If you want to keep the previous values, you could copy them and then apply them back - e.g.:

    let values = this.val();
    this.submit(
      () => {
        this.create();
        this.val(values);
      },
      null,
      null,
      false
    );
    

    Allan

  • av-technikav-technik Posts: 4Questions: 1Answers: 0

    Unfortunately, this code still resets the form. I also tried to chain the two functions "create" and "val", here unfortunately the same result.
    Also trying to put the val function into a setTimeout didn't work (but I wouldn't prefer that anyway).

  • av-technikav-technik Posts: 4Questions: 1Answers: 0

    @allan Any update on this?

  • Aryan1703Aryan1703 Posts: 63Questions: 15Answers: 1

    Are you inserting same values for every entry?

  • av-technikav-technik Posts: 4Questions: 1Answers: 0

    @Aryan1703
    Almost, I would like to enter all values once and create the entry. Then I want to be able to adjust individual values and save them again.
    The project is a hardware inventory. For example, I would like to add 10 monitors. All data is basically identical, only the inventory number and serial number are different. I would just change these briefly and create the next entry.

  • Aryan1703Aryan1703 Posts: 63Questions: 15Answers: 1

    If you would like to create same entries You can optionally pass the first parameter of .create() as a row count to make it create multiple new rows. So: in my case I wanted to create 15 duplicated entries.

     editor
                        .create(15, false, {
                            title: 'Create Entry',
                            buttons: 'Add',
                            formOptions: {
                                main: {
                                    onSubmit: function (e, action) {
                                        action.submit();
                                    }
                                }
                            }
                        })
    
Sign In or Register to comment.