Custom button to edit multiple rows

Custom button to edit multiple rows

bill_Tombill_Tom Posts: 19Questions: 4Answers: 0
edited October 2015 in Buttons

Hello,
i need a custom button which edit the selected rows. I tried it with multiset, but i didn't get an solution...

'''js

    buttons: [

        { extend: 'create', editor: editor },
        { extend: 'edit',   editor: editor },

        {
           // extend: 'selectedSingle',
            text:   'Alter + 10' ,
            action: function ( e, dt, node, config ) {
                // Immediately add `250` to the value of the salary and submit
                editor
                   //.field('sql_alter_1').multiSet('row_1', 1)
                    .multiset( 'sql_alter_1',   1 )
                  // .edit( table.row( { selected: true } ).index() )           //, false
                  // .set( 'sql_alter_1',  (editor.get( 'sql_alter_1' )*1) +  1 )
                   .submit();
            }
        } ,
        { extend: 'remove', editor: editor }
    ]
} );

'''

Does i need a loop? I have no idea...
Is there anywhere an example (example for a loop in buttons)- i didn't find something.

Thx for your help,
Thomas

This question has an accepted answers - jump to answer

Answers

  • bill_Tombill_Tom Posts: 19Questions: 4Answers: 0

    '''
    text: 'Alter + 10' ,
    action: function ( e, dt, node, config ) {
    //alert( table.rows('.selected').data().length +' row(s) selected' );

                    table.rows('.selected').every(function( t, tableLoop, rowLoop ) {
                        editor
                            .edit( table.row( t ).index() , false )
                            .set( 'sql_alter_1',  (editor.get( 'sql_alter_1' )*1) +  1 )
                            .submit();
                            j = 300000000
    
                            for (var i = 0; i < j; i++) {
                               //LOOP - Do Something
                            }
                        //alert (table.row(t).index() + table.row(t).data()) ;
                    } );
    

    '''
    This is my Script - The editor doesn't work correct, because there are to many sql request in a short time.
    If i use the '''alert''' Button - it works.

    I think there must be a better way - has somebody an idea?
    Thx,
    Thomas

  • allanallan Posts: 61,972Questions: 1Answers: 10,160 Site admin

    What is it that you want the button to do? I know you want to edit multiple values - but alter them in what way?

    Allan

  • bill_Tombill_Tom Posts: 19Questions: 4Answers: 0

    Hello Allan,
    I like that the user can select multipe rows and if the user press the button, this rows will get a new value in one cell.

    In this table all orders for the user are shown. The orders have all the status "NEW - ID is 1", if the user press the button the next status is "RUN - ID is 2".

    In my PHP-SQL-Script for this table I made an where-clause, so that in this table will shown only datas with the status 1 and the user will only the orders with the status new.

    Table:
    ID | Order | Date | Status

    So the user can handle it really easy.

  • allanallan Posts: 61,972Questions: 1Answers: 10,160 Site admin

    Got it - thanks. So the value to set for the cell is not common to all rows being edited - but rather the value is dependent upon the data in the rest of the row's data. In which case, I'm afraid a loop is the only way to do it at the moment.

    Being able to provide a function that will do that loop for you is a nice idea though - thanks for the suggestion - I will look into that.

    Regards,
    Allan

  • bill_Tombill_Tom Posts: 19Questions: 4Answers: 0

    Hello Allan,
    I think we are thinking the same. For beeing sure...

    Table

    Order-ID | Order-Name | Date | Status
    1        |  dog       | 2015 | 1
    2        |  cat       | 2015 | 1
    3        | mouse      | 2015 | 1
    4        | pig        | 2015 | 1
    5        | spider     | 2015 | 1
    

    Selected rows 1, 3 and 4 -> User press the button for the next status

    Order-ID | Order-Name | Date | Status
    1        |  dog       | 2015 | 2
    2        |  cat       | 2015 | 1
    3        | mouse      | 2015 | 2
    4        | pig        | 2015 | 2
    5        | spider     | 2015 | 1
    

    Hoping now everything is clear.
    Thanks a lot for your support and i am looking forward for your solution.

    Regards
    Thomas

  • allanallan Posts: 61,972Questions: 1Answers: 10,160 Site admin

    In your example above, the Status column has been updated to a common value for the selected rows. So you could simply use editor.field( 'Status' ).val( 2 ); (or similar) to set the value (since it is a common value).

    It is where there is no common value that you would have to use a loop.

    Allan

  • bill_Tombill_Tom Posts: 19Questions: 4Answers: 0
    edited October 2015

    Hello,
    and this doesn't work with a custom button.
    That is my problem.
    And I think there must be an easy way. Because the user want it easy.

    buttons: [
                 {
                    text: 'Reload',
                    action: function ( e, dt, node, config ) {
                        dt.ajax.reload();
                    }
                },
                {
                    text:   'Next Status' ,
                    action: function ( e, dt, node, config ) {
                        editor
                            .edit(row, false)
                            .val('status', '2' )
                            .submit();
    

    It was already a discussion:

    https://datatables.net/forums/discussion/27973/update-multiple-rows-using-customised-control-button

  • allanallan Posts: 61,972Questions: 1Answers: 10,160 Site admin
    Answer ✓

    The thread you linked to was before Editor 1.5 was released (the version which added multi-row editing).

    The code you have above should work - although I don't see where the row variable is being defined? I think you might want to try:

                       editor
                            .edit(dt.rows({selected:true}).indexes(), false)
                            .val('status', '2' )
                            .submit();
    

    If that doesn't work, can you link to a test page showing the issue.

    Allan

  • bill_Tombill_Tom Posts: 19Questions: 4Answers: 0

    Works perfect. Thanks a lot.

    Could be nice example on your page...

    Thx
    Thomas

This discussion has been closed.