Best use of results from fnGetTds?

Best use of results from fnGetTds?

MattCMattC Posts: 10Questions: 0Answers: 0
edited June 2012 in General
I've been working with DataTables for a couple days now and I like it quite a bit. Thanks Allan.

I'd like advice on how to make part of my code better. It works, and I'm satisfied with the effects, but I bet I'm missing something cleaner in one place.

I have a table that will be changing several times over the next couple weeks. I don't want to use numeric column indexes to identify fields. I have applied classes to the s and to certain s to pick out the data I want.

I am writing out the table in HTML including some columns with verbose data, which I'm then hiding. I'm using fnGetTds to get this hidden data back out. To pick the column I want, I'm taking each td from the array and turning it into a jQuery object, and then checking for the class that means it was one of the verbose cells. This feels awfully clumsy. I imagined something like $('').append(anTds.join()) and then .find(".dataclass") on the resulting row, but I'm not adept enough with jQuery to get it working.

Suggestions on getting rid of the ugly array loop?

[code]
function expand_details(e) {
e.preventDefault();

var parenttr = $(e.target).closest('tr')[0];

var expandLink;
var description;
var notes;
var internalNotes;
var anTds = dtExpedite.fnGetTds(parenttr);
for (var i = 0; i < anTds.length; i++) {
var cell = $(anTds[i]);
if (cell.hasClass('data_ExpandDetails')) {
expandLink = cell.find('a:first');
}
if (cell.hasClass('data_Description')) {
description = cell.html();
}
if (cell.hasClass('data_Notes')) {
notes = cell.html();
}
if (cell.hasClass('data_InternalNotes')) {
internalNotes = cell.html();
}
}

if (expandLink.html() == "-") {
dtExpedite.fnClose(parenttr);
expandLink.html("+");
} else {
dtExpedite.fnOpen(parenttr, fnFormatDetails(dtExpedite, description, notes, internalNotes), 'details');
expandLink.html("-");
}
}
[/code]

Replies

  • allanallan Posts: 63,541Questions: 1Answers: 10,476 Site admin
    edited June 2012
    Ah - a challenge :-). How about this:

    [code]
    function expand_details(e) {
    e.preventDefault();

    var parenttr = $(e.target).closest('tr')[0];
    var anTds = dtExpedite.fnGetTds(parenttr);

    var expandLink = $(anTds).filter('.data_ExpandDetails').find('a:first');
    var description = $(anTds).filter('.data_Description').html();
    var notes = $(anTds).filter('.data_Notes').html();
    var internalNotes = $(anTds).filter('.data_InternalNotes').html();

    if (expandLink.html() == "-") {
    dtExpedite.fnClose(parenttr);
    expandLink.html("+");
    } else {
    dtExpedite.fnOpen(parenttr, fnFormatDetails(dtExpedite, description, notes, internalNotes), 'details');
    expandLink.html("-");
    }
    }
    [/code]

    A couple of assumptions here - I presume you are using hidden columns, hence the need for fnGetTds? I've also assumes that there will always be those inner cells - is that correct?

    Allan
  • MattCMattC Posts: 10Questions: 0Answers: 0
    Your assumptions are correct, using hidden columns (not sure that's the best way here, but it's what I started with), and as long as I'm using the hidden columns those cells will always be present.

    Your improvement works great and is much tidier. Thanks.
This discussion has been closed.