Sélect all onload

Sélect all onload

gmisterkgmisterk Posts: 4Questions: 2Answers: 0

Hi all
I use select extension .
I have a button selectAll. .and It works fine.
By défaut nothing is selected and I want inverse.

I.m trying to select all items onload (when document ready)

I have tried this : $('#myid').addClass('selected'); but whiout success

Any suggestion?

Thanks for your Help.

Answers

  • colincolin Posts: 15,240Questions: 1Answers: 2,599

    Hi @gmisterk ,

    The best bet is to use initComplete, this will get called when the table has fully loaded. You can do something like this:

          initComplete: function() {
            this.api().rows().select();
          }
    

    See this live example.

    Cheers,

    Colin

  • jshuffjshuff Posts: 1Questions: 0Answers: 0

    This was very helpful in getting me started. Any suggestions on how to have it select only specific rows instead of all?

    My objective is that I'm setting this up for a user to UPDATE an existing record in the database. I have the complete form loaded and pre-populated with the existing database values.

    What I need to accomplish is now AFTER LOAD my datatable shows all US States but I need to auto select New York and California. My JSON that populates the datatable has an additional attribute name 'active' which is set to '1' for the rows I'd like to auto select.

    Any ideas?

  • kthorngrenkthorngren Posts: 21,311Questions: 26Answers: 4,948

    Good question. The solution I would use makes use of rows().indexes() and filter(). Inside initComplete a combo of those APIs is used to build an array of indexes containing the rows with active === 1. The indexes is then used to select the rows to select using rows().select(). Here is an example selecting all rows where the Office is London.
    http://live.datatables.net/ketaripi/1/edit

    Kevin

This discussion has been closed.