views:

54

answers:

0

I'm using jQuery and the Datatables plugin. When a row is clicked, a hidden row appears with details about that record that can be edited. After those details are edited and the hidden row is closed, the cells of the main table that have changed are updated.

Here's my old code:

function teacherTableUpdate(nTr) {
    oTable.fnClose(nTr);
    var temp1 = oTable.fnGetData(nTr);
    if (teacherEditResults[0] != temp1[2]) {
        oTable.fnUpdate(teacherEditResults[0], nTr, 2, false);
        $("td:nth-child(2)", nTr).effect("pulsate", {
            times: 3
        }, "slow");
    }
    if (teacherEditResults[1] != temp1[3]) {
        oTable.fnUpdate(teacherEditResults[1], nTr, 3, false);
        $("td:nth-child(3)", nTr).effect("pulsate", {
            times: 3
        }, "slow");
    }
    if (teacherEditResults[2] != temp1[4]) {
        oTable.fnUpdate(teacherEditResults[2], nTr, 4, false);
        $("td:nth-child(4)", nTr).effect("pulsate", {
            times: 3
        }, "slow");
    }
}

And here's what I did to try to make it more efficient using .each():

function teacherTableUpdate(nTr) {
    oTable.fnClose(nTr);
    var temp1 = oTable.fnGetData(nTr);
    $.each(teacherEditResults, function (index, value) {
        index = index + 2
        if (value != temp1[index]) {
            oTable.fnUpdate(value, nTr, index, false);
            $("td:nth-child(" + index + ")", nTr).effect("pulsate", {
                times: 3
            }, "slow");
        }
    });
}

These parameters are used to update the table. teacherEditResults[] refer to each of the new values, and temp1[] has the old data for all columns of the row (1,2,3,4,5,etc.) The two arrays are different sizes but for this purpose, the temp1[] indexes are always +2 higher.

teacherEditResults[0] != temp1[2]
teacherEditResults[1] != temp1[3]
teacherEditResults[2] != temp1[4]

QUESTION: Is this approach (adding +2 to each index of temp1[]) appropriate?