Programmatically select rows

Programmatically select rows

jrenautjrenaut Posts: 3Questions: 0Answers: 0
edited May 2011 in TableTools
I'd like to implement a "Select Visible" button that would only select the rows currently visible on screen. Not a big deal - here's the snippet defining my buttons:
[code]"aButtons": [ "select_all", "select_none",
{
"sExtends": "text",
"sButtonText": "Select Visible",
"fnClick": function ( nButton, oConfig, oFlash )
{
var oTable = $('#mytable').dataTable();
var nodes = oTable.fnGetDisplayNodes();
for (var i=0; i

Replies

  • allanallan Posts: 63,498Questions: 1Answers: 10,470 Site admin
    Hmm - actually there is a public API function for that. An oversight on my part - sorry. I've added that to the to-do list for the next version of TableTools! Until then, as you are seeing just adding a class isn't quite enough for TableTools since it keeps an internal array of the nodes which are selected for a faster lookup. So what you can do is cause a click event to occur to select the row ( $(nodes[i]).click() would do it). You would probably want to check that the node isn't already selected before doing that :-)

    Allan
  • jrenautjrenaut Posts: 3Questions: 0Answers: 0
    Ahh, I thought it might be keeping its own list. Thanks for the help.
  • hozthozt Posts: 45Questions: 0Answers: 0
    edited April 2012
    [quote]Hmm - actually there is a public API function for that. An oversight on my part - sorry. I've added that to the to-do list for the next version of TableTools! Until then, as you are seeing just adding a class isn't quite enough for TableTools since it keeps an internal array of the nodes which are selected for a faster lookup. So what you can do is cause a click event to occur to select the row ( $(nodes[i]).click() would do it). You would probably want to check that the node isn't already selected before doing that :-)

    Allan [/quote]

    Hello

    I would love to check if it isn't selected already, but it doesn't work.
    This is my code:

    [code]
    var oTT = TableTools.fnGetInstance('my_table');
    var rVis = oTable.fnGetDisplayNodes();
    for (var i=0; i
  • allanallan Posts: 63,498Questions: 1Answers: 10,470 Site admin
    fnIsSelected expects a node, not a jQuery object. So a small modification should do it:

    [code]
    if (oTT.fnIsSelected( rVis[i] )) {
    [/code]

    work now it will :-)

    Allan
  • hozthozt Posts: 45Questions: 0Answers: 0
    yeah I figured it out yesterday in the TableTools topic I linked there.
    spammed the topic with my problem and solution for other people, since it's not a native function, and you have to use both API's to do what I wanted. DataTables and TableTools.

    Thanks anyhow!
  • cliftonitecliftonite Posts: 8Questions: 0Answers: 0
    I'm still a bit confused on this. I'm trying to programmatically select a row. I've tried simulating a .click() with the row ID, but jQuery doesn't like it since the rows are dynamically introduced into the DOM.

    So I either need a Datatables API method that selects a row, or I need to know how to simulate a click with the 'delegate' method (which, I believe, is the new way to use jQuery "Live").

    Any ideas?
  • allanallan Posts: 63,498Questions: 1Answers: 10,470 Site admin
    > So I either need a Datatables API method that selects a row

    In TableTools that API method is fnSelect. DataTables itself does not have row selection abilities built in (TableTools provides that feature as an optional extra).

    > I need to know how to simulate a click with the 'delegate' method

    $(myElement).click(); should do the job just fine.

    Perhaps you can use http://live.datatables.net to show us an example of the problem you are having?

    Allan
  • adurakadurak Posts: 1Questions: 0Answers: 0
    Hi Allan, great product. Congratulations!

    cliftonite:

    I think the best way to select a row is add the "DTTT_selected" class to the node and push the node to "selected" array. (of course you have to use TableTools)

    [code]
    $(nodes[i]).addClass('DTTT_selected');
    TableTools.fnGetInstance('oTable').s.select.selected.push(nodes[i]);
    [/code]
This discussion has been closed.