Update database

Update database

danielrm79danielrm79 Posts: 6Questions: 3Answers: 0

Hi, I'm trying to update a datatabase from datatables. I'd like to change the value of one ot the columns for every row, doing this:

                                    $('#example').DataTable().rows().every( function ( rowIdx, tableLoop, rowLoop ) {
                                        var datos = this.data();        
                                        counter ++;
                                        datos.NUM = counter;
                                        this.data(datos);
                                    });

Once finished, it shows the table modified, but when I refresh data, it seems that changes have not been saved in database, as it shows original data. I don't know what I'm doing wrong.

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 61,934Questions: 1Answers: 10,155 Site admin
    Answer ✓

    row().data() is a client-side API. It has no communication with the server and thus nothing would be stored in the database.

    Are you hoping to use Editor for this? If so, the way to do this is with its multi-row editing APIs. Specifically, trigger editing on all rows and then loop over the values (field().multiGet()) and update them as required (field().multiSet()). Once that's done, submit to the server (submit()) and the database and table will be updated.

    Regards,
    Allan

  • danielrm79danielrm79 Posts: 6Questions: 3Answers: 0

    Thank you Allan.
    I'm using Editor, and when I try to do

    editor
        .edit(0 ,false)
        .val('NUM',2)
        .submit();
    

    It works and updates 'NUM' Field with value '2'
    But when try to use

    editor.multiGet();
    

    it says

    SCRIPT5007: Unable to get property 'length' of undefined or null reference
    dataTables.editor.min.js (153,14)

  • danielrm79danielrm79 Posts: 6Questions: 3Answers: 0

    Thnak you very much, Allan. It worked using one of the examples of your webpage

    editor.edit( tabla.rows( ).indexes() , false);
    // Get the ids of the rows being edited
    var rows = editor.field( 'NUM' ).multiGet();
    // Set each item value
    $.each( rows, function ( id, val ) {
        editor.field( 'NUM' ).multiSet( id, contador );
        contador ++;
    } );
    editor.submit();
    
This discussion has been closed.