fnRowSelected returning HTMLTableRowElement instead of id?

fnRowSelected returning HTMLTableRowElement instead of id?

ChrisGChrisG Posts: 29Questions: 0Answers: 0
edited July 2013 in TableTools
I can't seem to get the id to return on a selected row. Here's my code:

[code]
'oTableTools': {
'sRowSelect': \"multi\",
'fnRowSelected': function (nodes) {
alert( 'The row with ID '+ nodes[0] +' was selected' );
}
[/code]

This just gives me the following alert:

"The row with ID [object HTMLTableRowElement] was selected"

What's going on here? I pulled the code directly from the example for fnRowSelected... but I can't seem to get it to actually give me back the row info. I just want to get the id of the row.

Replies

  • allanallan Posts: 63,489Questions: 1Answers: 10,470 Site admin
    edited July 2013
    `nodes` is an array of nodes. So if you want the ID of the first in the array, you'd just use `nodes[0].id` .

    Allan
  • ChrisGChrisG Posts: 29Questions: 0Answers: 0
    edited July 2013
    Thanks!
  • ChrisGChrisG Posts: 29Questions: 0Answers: 0
    How could I then take this row id and store it in a mysql variable? I've tried several different methods but can't seem to get the id over to serverside to use PHP to send the mysql insert.
  • allanallan Posts: 63,489Questions: 1Answers: 10,470 Site admin
    What have you got? I might be able to suggest a modification. But basically what you need to do is use the `data` option of jQuery's Ajax request (assuming you are using $.ajax).

    Allan
  • ChrisGChrisG Posts: 29Questions: 0Answers: 0
    What I have so far is that you can select a row and it will then save a value of "1" to a mysql variable named after the row... So if you click row_50 then it will store 1 in the variable "row_50".

    My ultimate goal is to then read this data when the table initializes and re-select rows that have a value of 1.

    Essentially creating a serverside selecting that stores each row selection.

    I feel like I've run into a wall with how to proceed from this point though. I'm assuming something to do with fnRowCallback and then getting the current row ids... then sending that info to the mysql server to retrieve the appropriate values for those rows... and then return that back into the restore function somehow and have it automatically select the proper rows.

    I don't know though... I feel like I'm in over my head at this point. Any tips?
  • allanallan Posts: 63,489Questions: 1Answers: 10,470 Site admin
    Sorry - I meant code wise - what is the Ajax request that you are currently using (again, assuming you are using ajax)? Basically, what have you tried thus far, code-wise?

    Allan
  • ChrisGChrisG Posts: 29Questions: 0Answers: 0
    [code]$.post( './models/process.php', { sRow: selectedRow, userName: uname}, function(data){});[/code]

    When a user selects a row, it calls a function with this inside. It basically sends the selected row as "row_#" and the username of the person logged in.

    Then in process.php it connects to the database and does a mysql_query to update the user's account info to store that row selection info. Right now it's storing it in a field for each row... but I was only doing that to test. I think my best bet would be to store all the row selections as single numbers in a long string. For example if I have 10 rows, I could have a string representing 0's and 1's to show which rows have been selected or not (0 = not selected, 1 = selected)

    So:

    rows = "0100001000"

    would mean that rows 2 and 7 are selected.

    I just need to know how to best go about pulling that data from mysql, getting it into datatables and having it use that string to iterate through the rows and re-select the ones that should be selected.

    I hope this makes sense.
  • allanallan Posts: 63,489Questions: 1Answers: 10,470 Site admin
    Would it not be a lot easier to just send the IDs of the selected rows? That way it is independent of the table ordering and won't need any decoding on the server-side.

    You could do something like:

    [code]
    var ids = $.map( tt.fnGetSelected(), function ( i, row ) {
    return row.id;
    } );
    [/code]

    where `tt` is the TableTools instance for the table and assuming that each row has an ID. Then you'd just use the `ids` array as the data to submit to the server.

    Allan
  • ChrisGChrisG Posts: 29Questions: 0Answers: 0
    Where would I place that bit of code exactly?

    I was under the impression that mysql database variables can't be stored as arrays.

    I think my bigger concern is just figuring out how to load the stored data and actually populate the selected rows from it. :(
  • allanallan Posts: 63,489Questions: 1Answers: 10,470 Site admin
    You'd used that code whenever you want to get the ids of the selected rows.

    > I was under the impression that mysql database variables can't be stored as arrays.

    You are correct, MySQL doesn't have an array type. I don't quite understand why this is an issue though - shouldn't you be storing information for each row - which is presumably a row in the database?

    > I think my bigger concern is just figuring out how to load the stored data and actually populate the selected rows from it. :(

    Have you got the data being loaded? If so, that's a great step. The next one is to use fnGetData to get the data for the table and then loop through it. You'd check to see if a row is selected and then use the TableTools fnSelect method to select it on the client-side.

    Allan
  • ChrisGChrisG Posts: 29Questions: 0Answers: 0
    Well because I'm trying to save this row selection data to individual users, I felt it was best to try and condense it into a single value that could be stored as a single row in the database in the user's info.

    Storing each selected row individually in the row data itself would only work if a single user were selecting rows. I want it to be instanced per user so each user can store whatever selected rows they choose.

    I don't have the data being loaded yet. I'm probably going to attempt saving it into the condensed string of numbers first and then run through those. Thanks for the info though... I think my plan now is to get the data loading from the mysql db, and then loop through the table and use the id numbers of the visible rows as the index to look at in the loaded string data... and then from there I can fnSelect them.

    I'll give that a shot. :)
This discussion has been closed.