how to submit data that is generated with table.row.add

how to submit data that is generated with table.row.add

jpavanjpavan Posts: 38Questions: 12Answers: 0
edited August 2018 in Editor

Good afternoon, I have defined a datatable editor and a custom button that adds information to the table. But this information is not recorded in the database, even if I indicated the submit ().
This is my code:

        buttons: [
            {
                extend: "create",                
                text: "-> Add Item",
                action: function ( e, dt, node, config ) {
                    var table = $('#sxi_v2').DataTable();
                    editor
                    .set( table.row.add( {                       
                                    "sxi_codisi": "5",
                                    "sxi_codseg": "102010",
                                    "sxi_activo":"1"
                                } ).draw())
                    .submit();
                }
            },
        ]

This button adds a row to my datatable with the indicated data, but it is not recorded in the database.
What instruction would I be missing from submit?
Thank you

Answers

  • allanallan Posts: 61,451Questions: 1Answers: 10,055 Site admin

    Its a little backwards that. You would actually set the values using the Editor API (set() and then submit it. Editor itself will then add the new row to the DataTable. No need to call row.add() yourself.

    Allan

  • jpavanjpavan Posts: 38Questions: 12Answers: 0

    Excellent. Thank you very much

  • jpavanjpavan Posts: 38Questions: 12Answers: 0

    Sorry Allan, I ask you again.
    Now, using "set", when I put it inside a "for" loop that traverses the elements of an array, it only saves the data once. That is, if the loop runs 5 times, it only saves data the first time.
    For example:

    for (var i=0; i < count ;i++){
    editor
    .create( false )
    .set ( { sxi_codisi: <?php echo $this->id ?>,
    sxi_codseg: elegidos1[i][0],
    sxi_activo:"1" } )
    .submit( );
    }

    The values that the "elegidos1" array takes are correct.
    What can cause that is not submitted all the times inside the loop?
    Thanks again.

  • kthorngrenkthorngren Posts: 20,147Questions: 26Answers: 4,736

    Is sxi_codisi a unique ID?

    If so maybe .set ( { sxi_codisi: <?php echo $this->id ?>, is generating the same ID each time through the loop.

    Kevin

  • jpavanjpavan Posts: 38Questions: 12Answers: 0

    No, this is not the case; the table has another column that is primary key and autoincremental.

  • allanallan Posts: 61,451Questions: 1Answers: 10,055 Site admin

    The problem here is the async nature of the submit action, and the fact that Editor can only handle a single submission at a time. So while the first submit is being handled by the server, the loop will have already run through - but it can't do anything with the since there is already a submission in progress.

    The best option is to use the multi-row editing abilities of Editor to create multiple new rows with a single submit.

    The next option is to use a queue and submitComplete to know when the first submit is finished so you can then take the next item off the queue and submit that, etc.

    Allan

  • jpavanjpavan Posts: 38Questions: 12Answers: 0

    Excellent. Now using "multiset" with this code, save many records as I want, but always keep the same values according to the last value of the loop.
    What is wrong here?

    editor.create( count, false );
    for (var i=0; i < count ;i++){
    editor.field( 'sxi_codisi' ).multiSet( 0, <?php echo $this->id ?> );
    editor.field( 'sxi_codseg' ).multiSet( 0, elegidos1[i][0] );
    editor.field( 'sxi_activo' ).multiSet( 0, "1" );
    };
    editor.submit();
    Thanks Allan!

  • allanallan Posts: 61,451Questions: 1Answers: 10,055 Site admin

    .multiSet( 0, should be .multiSet( i,.

    Allan

  • jpavanjpavan Posts: 38Questions: 12Answers: 0

    Thanks Allan, a detail haha!

This discussion has been closed.