Getting Table Cell Values.

Getting Table Cell Values.

dneedlesdneedles Posts: 24Questions: 0Answers: 0
edited February 2010 in General
You would think grabbing a cell of a table at column X and row Y would be simple, but I didn't find it. I started with the MULTIPLE LINE example but fnGetNodes didn't want to play with fnGetData and fnGetPosition and returned an alert box with "undefined" instead:
[code]
function fnGetSelected( oTableLocal )
{
var aReturn = new Array();
var aTrs = oTableLocal.fnGetNodes();
var aPos;
var aData;

for ( var i=0 ; i MAX_DUMP_DEPTH) {
return indent + name + ": " + depth + "\n";
}
if (typeof obj == "object") {
var child = null;
var output = indent + name + "\n";
indent += "\t";
for (var item in obj)
{
try {
child = obj[item];
} catch (e) {
child = "";
}
if (typeof child == "object") {
output += dumpObj(child, item, indent, depth + 1);
} else {
output += indent + item + ": " + child + "\n";
}
}
return output;
} else {
return obj;
}
}
[/code]

Dumping the object showed me there was no way to reference an individual cells of data?! The entire data row can be grabbed by the method .textConent. So I hacked together:
[code]
var pos=aTrs[i].textContent;
var posarr=pos.split("\n");
for ( var j=0 ; j

Replies

  • dneedlesdneedles Posts: 24Questions: 0Answers: 0
    edited February 2010
    Now I am remembering why I stayed in system development. Of course like JAVA backward and cross compatability in the language and browsers is pretty much non existent. The textContent method only works with FireFox, not IE. So ... I had to really muck around ... Wish there was something abstracted in datatables!
    [code]
    var pos = this.innerHTML;
    var poscln = pos.replace(/^\s|<\/?[^>]+(>|$)/g, "");
    var posarr=poscln.split("\n");
    for ( var j=0 ; j
  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    Hi dneedles,

    I'm not exactly clear on what you are trying to do. Perhaps if you could explain that, I'd (hopefully) be able to point you at an easier solution. For example, I'm don't think this little piece of code makes sense:

    [code]
    aPos=oTableLocal.fnGetPosition(aTrs[i]);
    aData=oTableLocal.fnGetData(aTrs[i]);
    alert(aData[aPos[1]] );
    [/code]
    When passed a TR element, fnGetPosition() will only return an integer - not an array ( http://datatables.net/api#fnGetPosition ). Therefore using the undefined variable aPos[1] is going to give an error. Which is what you are seeing. So the question is - how do you know which column you want the data from?

    Allan
  • dneedlesdneedles Posts: 24Questions: 0Answers: 0
    edited February 2010
    >> I'm not exactly clear on what you are trying to do

    Thanks for the follow up. That helped. I'm a bit green and thought in the example 'this' refered to a row rather than the table.

    In general I want a generic way to get the value of a cell from a row of data in the context of the multi row select example.

    When I failed to get the native GetData and GetPosition function to work I tried cell() and other methods, which also failed. I finally grabbed the innerHTML which worked with both FireFox and IE, but this dumped the raw text and I had to parse the HTML myself as shown in the earlier post. I suspect I am missing something here.
  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    Hi dneedles,

    fnGetData will give you the innerHTML of a cell - so if you have HTML in there, then you will get this in the string as well. What it sounds like you might what to do is get the cell and use DOM selectors on it to get the value of nested HTML - does this sound like I'm going along the right lines?

    So if you have event handlers attached to a row, then you can just bypass any DataTables specific methods and query the DOM:

    [code]
    $('#example tbody tr').click( function () {
    // Alert the contents of an element in a SPAN in the first TD
    alert( $('td:eq(0) span', this).html() );
    } );
    [/code]
    Regards,
    Allan
  • dneedlesdneedles Posts: 24Questions: 0Answers: 0
    Ah. It appears my lack of JQUERY centric thinking was the issue. Of course you can index the fields within a row of data via its numerical position. So it was easy thanks!
This discussion has been closed.