Select specific table rows on button click

Select specific table rows on button click

huba_sbfhuba_sbf Posts: 16Questions: 6Answers: 1

Hello,

I have a table with 20 rows which need to be edited on a daily basis. 15 of them will always have to be edited with the same values each.
Is it possible to have a button which simply selects these 15 rows, in order to benefit of "Multi-row editing"? An example would be great.

Thank you.

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,818Questions: 1Answers: 10,517 Site admin

    Yes that should be quite possible. The documentation here details how to create a custom button. In it you would use the rows().select() method to select your 15 rows and then the Editor API methods (such as edit()) to trigger the editing.

    Allan

  • huba_sbfhuba_sbf Posts: 16Questions: 6Answers: 1
    edited October 2016

    Thank you Allan for the quick reply, I've got as far as adding a button to select all the rows, by adding the following button:
    ...

    {
      text: "Select",
      action: function ( e, dt, node, config ) {
      childTable.rows().select();                           
      }
    }
    

    Any chance to help me out in how to only select the 15 I need? Let's say I have a column called "fruit" and I would like to select all rows where fruit = "apples".

    Thanks again.

  • allanallan Posts: 63,818Questions: 1Answers: 10,517 Site admin

    I would suggest using rows().every() and looping over each row to see if you want to select it or not based on the data in it.

    Allan

  • huba_sbfhuba_sbf Posts: 16Questions: 6Answers: 1
    edited October 2016

    Hey Allan,

    I have tried the following:

                            text: "Select", 
                            action: function ( e, dt, node, config ) {
                                childTable.rows().every(function(){
                                    if (this.data().type == 'apple'){
                                        this.select
                                    }
                                });
                                console.log( this.data().type );
                                console.log( this.data() )
                            }
    

    for: console.log( this.data().type ), I have "Undefined
    for console.log( this.data() ), please see the attached image.

    Thank you.

  • allanallan Posts: 63,818Questions: 1Answers: 10,517 Site admin
    Answer ✓

    Can you put the console.log() statements inside the every function and run it again please? I'd need to know what their values are in there.

    Thanks,
    Allan

  • huba_sbfhuba_sbf Posts: 16Questions: 6Answers: 1
    edited October 2016

    Hey Allan,
    I have put the console.log() statements and everything seemed fine, yet the selection wasn't made.
    Then I have realized what my code was missing some brackets, so instead of this.select;
    I actually needed this.select();
    Thanks for your help, you have made my day!
    Finally we have:

    text: "Select",
    action: function ( e, dt, node, config ) {
        childTable.rows().every(function(){
            if (this.data().type == 'apple'){
                this.select();
            }
        });
    }
    
This discussion has been closed.