Get value of row on another page

Get value of row on another page

TanaxTanax Posts: 9Questions: 0Answers: 0
edited September 2011 in General
New to the forums, sorry if this is in the wrong section!
Curious how I would get the value from a cell on a row that is located on another page in the table than the page I'm currently viewing.

If I look at the HTML in firebug it doesn't seem to be loaded on the page so it returns null when trying to access it.
Any ideas?

Replies

  • TanaxTanax Posts: 9Questions: 0Answers: 0
    Alright, I didn't check the API. I read about fnGetPosition and fnGetData.
    However, it does not appear to work in any case.

    I have a function that when I click on a button(update) it's supposed to fetch the ID's of the users in the table that needs updating from the database(this is done via an AJAX call) and then go through each one of those ID's and update the database(again with an AJAX call), after updated the PHP returns that it's been updated. I then have to fetch the row with the ID.

    Each row has an ID of #user_, example: #user_734 for user with id 734.
    So I tried:

    [code]
    var pos = oTable.fnGetPosition( '#user_' + id );
    alert(pos);
    [/code]

    in the foreach loop. It gives me Javascript error:

    [quote]
    nNode.nodeName is undefined
    ar sNodeName = nNode.nodeName.toUpperCase();

    jquery.dataTables.js (line 1903)
    [/quote]

    Why's that?
  • allanallan Posts: 63,531Questions: 1Answers: 10,475 Site admin
    > oTable.fnGetPosition( '#user_' + id );

    fnGetPosition doesn't take a string as an argument, it takes a node ( http://datatables.net/api#fnGetPosition ) - although that's a nice idea for an extension.

    What you need to do is something like this:

    [code]
    var nodes = oTable.fnGetNodes();
    var jqRow = $(nodes).filter('#user_'+id);
    [/code]

    Allan
  • TanaxTanax Posts: 9Questions: 0Answers: 0
    Thanks for the help!
    What exactly would the object of jqRow contain?

    Do I need to do this?
    [code]
    var nodes = table1.fnGetNodes();
    var row = $(nodes).filter( '#user_' + id );

    var pos = table1.fnGetPosition( row );
    var rowData = table1.fnGetData( pos );
    [/code]

    It still gives me the same kind of error :/
  • allanallan Posts: 63,531Questions: 1Answers: 10,475 Site admin
    It would be the jQuery object returned with the element selected from the source.

    If you want the data then:

    [code]
    var nodes = oTable.fnGetNodes();
    var tr = $(nodes).filter('#user_'+id)[0];
    var data = oTable.fnGetData( tr );
    [/code]

    Allan
  • TanaxTanax Posts: 9Questions: 0Answers: 0
    edited September 2011
    Sorry, I'm such a newbie.
    It's working now! :)

    One more question though, is it possible to access the data based on what class the TD-cell has?
    My HTML looks like this:
    [code]


    1
    Allan
    hello@yes.com


    [/code]

    I know I can access it with:
    [code]
    var data = oTable.fnGetData( tr );
    alert( data[1] );
    [/code]

    to print the user name. Is it possible to access it with something along the lines of

    [code]
    $( data ).something( '.user_name' );
    [/code]

    ???

    Thanks for your help by the way! :)
  • fbasfbas Posts: 1,094Questions: 4Answers: 0
    "data" returned is an array, so if you know the column number, just use data[1]

    if you don't want to hard code the column number like that, you can use some jquery to find out which column in "tr" has that class

    [code]
    var data = oTable.fnGetData( tr );

    var username_col = $('.user_name', tr);
    var colnum = $(tr).index(username_col);
    var username = data[colnum];
    [/code]

    http://api.jquery.com/index/
  • TanaxTanax Posts: 9Questions: 0Answers: 0
    I do know the column number, I was just thinking that if I at a later point changes or add more columns to the table, the numbers I use might get screwed up so I thought it would be easier to be able to access it by name instead.

    In any case, I tried what you said and for some reason colnum returns -1, any idea why?
    [code]
    // Set up the base-DIV
    var baseDiv = '#user_' + id;

    // Get the correct node
    var node = $( table1.fnGetNodes() ).filter( baseDiv )[0];
    // Get the TR row-position from the node
    var row = table1.fnGetPosition( node );
    // Get the row-data
    var rowData = table1.fnGetData( row );
    alert(rowData);

    // Username data
    var column = $( '.user_name', row );
    alert(column);
    var username_col = $( row ).index( column );
    alert(username_col);
    var username = rowData[ username_col ];
    alert(username);
    [/code]

    First alert outputs an array.
    Second alert outputs an object
    Third alert outputs -1
    Last alert outputs "undefined"
  • fbasfbas Posts: 1,094Questions: 4Answers: 0
    -1 means not found
  • TanaxTanax Posts: 9Questions: 0Answers: 0
    That's strange.. seeing as it finds the row-array.
    Does it not find it because the row is on another page than the one you're on?

    Perhaps I'll just have to use the row-array data instead..
  • fbasfbas Posts: 1,094Questions: 4Answers: 0
    you won't get a if it's not on the current page, so all your other operations, jquery or not, won't get a DOM element or jQuery element.

    but yes, you can iterate through the data in the datatables
  • TanaxTanax Posts: 9Questions: 0Answers: 0
    Alright, I'll just use the data-array then! Thanks for the help :)
  • fbasfbas Posts: 1,094Questions: 4Answers: 0
    edited September 2011
    you CAN get the aoColumns from oTable and use that to find the column for user name (rather than hard code the col number)

    oTable.fnSettings().aoColumns is an array of column objects. the aoColumn objects may have an sName, sTitle, sClass, or mDataProp and such, depending on how you initialize your datatable

    best way to know which is worth using in your code is to return it in the debugger console and examine the fields your instance has filled in
This discussion has been closed.