Editor-create-submit in the "for"

Editor-create-submit in the "for"

ostmalostmal Posts: 102Questions: 33Answers: 0
edited July 17 in Editor

I need to automate the process of creating new rows in the database.

I read it:
https://editor.datatables.net/reference/api/
https://editor.datatables.net/reference/api/create()
https://editor.datatables.net/manual/api

I did this:

for (i = 1; i <= 10; i++) {
editor
     .create( false )
     .val( 'field', 'value_test' )
     .submit();
}
table.ajax.reload();

As a result, only 1 record is created in the database!
I don't understand what's wrong?

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 54,523Questions: 1Answers: 8,525 Site admin
    Answer ✓

    submit() is an async action and Editor can only perform one submission at a time. While the Ajax request is going on for the first time around the loop, the Javascript will quickly spin over the other items in the loop but Editor won't be able to process them.

    For this sort of thing it is much better to use multi-row editing (creation in this case). The requests will all be grouped together and submitted as a single Ajax call - e.g.:

    editor.create(10, false);
    
    for (let i=0; i<10; i++) {
      editor.field('myField').multiSet(i, 'Value ' + i);
    }
    
    editor.submit();
    

    Allan

  • ostmalostmal Posts: 102Questions: 33Answers: 0

    It's okay! Thanks, Allan!

Sign In or Register to comment.