'Invert selection' button?

'Invert selection' button?

krychaj5krychaj5 Posts: 19Questions: 8Answers: 0

Hi!
I'm loooking for code button which will invert selection in my rows.

For example: I have 10 rows, 4 selected / 6 unselected and I need button which will make it 6 selected / 4 unselected.

Is there any solution?

Answers

  • kthorngrenkthorngren Posts: 21,591Questions: 26Answers: 5,006
    edited June 2017

    You can use the code snippets from the first two Select API examples here to create the invert selection button:
    https://datatables.net/extensions/select/examples/api/index.html

    Maybe something like this:

      var rows = table.rows( { selected: true } )[0] ;  //gets array of selected row numbers
      table.rows().select(); 
      table.rows(rows).deselect();
    

    You can use the Datatables buttons as shown in the examples or create your on button with a click event handler.

    Kevin

  • krychaj5krychaj5 Posts: 19Questions: 8Answers: 0

    Not working.
    Can You help me out with this example?
    http://live.datatables.net/buhiseti/1/edit

  • HPBHPB Posts: 73Questions: 2Answers: 18
    edited June 2017

    Start by adding the select extension and setting it as an option during Datatables initialisation.
    Kevin's code works fine then.

  • krychaj5krychaj5 Posts: 19Questions: 8Answers: 0

    I've initialized "select extension" and It still not working:
    http://live.datatables.net/buhiseti/3/

  • tangerinetangerine Posts: 3,365Questions: 39Answers: 395

    Start by adding the select extension....

    The extension has to be included.

  • kthorngrenkthorngren Posts: 21,591Questions: 26Answers: 5,006

    In addition to adding the code for the select extension you also need to create the table variable, something like this:
    table = $('#example').DataTable( {

    You can add the select extension code to your example by clicking Add Library.

    Kevin

  • allanallan Posts: 63,893Questions: 1Answers: 10,531 Site admin

    Kevin's code looks just about spot on to me. The only thing I would say is to use rows().indexes() rather than rows()[0] on the first line. This is just the formal way to get the indexes, but really they amount of the same thing in the case of a single table API instance.

    Allan

This discussion has been closed.