get index of a row containing specific string

get index of a row containing specific string

doncullendoncullen Posts: 32Questions: 2Answers: 0
edited October 2010 in General
Currently, I have my script set up to obtain data from a server, and it uses fnAddData to pop the data in the table.

There are five columns in the table. The first column contains the customer's unique ID.

My script periodically queries my server for data. Upon getting data, it'll go through the data, pull the customer ID. Now here's the issue I run into. Ideally, after pulling the customer ID, it'd then search the rows (specifically in the first column) for the matching customer id, pull the index of that row, and return the index so I can run further operations on it.

I have it set to run two type of operations depending on the data returned from the server: if the customer ID in the table no longer exists in the server data, then the row has 'expired', and I run fnDeleteRow -- which requires the index of the row to be deleted -- thus the reason why I need to search the rows for that corresponding customer ID, and return the index of that row so I can pass it along to the fnDeleteRow function for the row to be deleted.

The second operation is if the server data shows the customer id still exists -- if it still exists in there, then the row is still active (not 'expired'), so instead of deleting the row, it'd update the row with new info about that customer (queued time). For this, I'd be using fnUpdate -- which again, requires the index of the row to be updated.

So my question should be apparent at this point: how do I have the script search each row in a specific column for the unique customer ID, and upon finding a match, obtain the index of that row? If I could get the index of the corresponding row, I'd be able to feed the index into fnUpdate and fnDeleteRow.

Thanks in advance for your time, and I am looking forward to be hearing your feedback soon!

Replies

  • doncullendoncullen Posts: 32Questions: 2Answers: 0
    edited October 2010
    Code removed; intranet code.

    I solved it on my own. Allan, you can keep the donation; consider it my thanks for your work on the project.

    For those wondering what the solution was, I had to develop my own function to obtain the index.

    Here's the function:

    [code] // each column will be numbered 0, 1, 2, ... etc from the left to right.
    // tell the function which column to base the string check on via iColumn parameter.
    // tableToCheck is the html id of the table you'd like to check, example: '#thetable'
    //
    // example usage:
    // var accountnumber, rowIndex;
    // accountnumber = '1234567890';
    // rowIndex = rowfindRowIndexUsingCol(accountnumber, '#comcast', 0);
    //
    function findRowIndexUsingCol(StringToCheckFor, tableToCheck, iColumn){
    // Initialize variables
    var i, aData, sValue, IndexLoc, oTable, iColumn;

    oTable = $(tableToCheck).dataTable();
    aiRows = oTable.fnGetNodes();

    for (i=0,c=aiRows.length; i
  • allanallan Posts: 63,514Questions: 1Answers: 10,472 Site admin
    Hi doncullen,

    Very nice - thanks for posting your solution to the problem you were seeing - probably quite a common issue, so I'll have a look at wrapping this up into an plug-in API function.

    I think there is a small optimisation which can be made to your code (which is more or less exactly how I would have suggested it be done!). You can take advantage of the fact that fnGetData() returns an 2D array (if no parameters are passed) in the order of the internal sequencing. For if you find what you are looking for in the data array, that is the row's position in aoData:

    [code]
    function findRowIndexUsingCol(StringToCheckFor, tableToCheck, iColumn){
    var i, c, oTable, aiRows;

    oTable = $(tableToCheck).dataTable();
    aiRows = oTable.fnGetData();

    for (i=0,c=aiRows.length; i
  • doncullendoncullen Posts: 32Questions: 2Answers: 0
    no problem, my pleasure. =)
  • bigdaddyvbigdaddyv Posts: 29Questions: 0Answers: 0
    Hi Allan,

    When I run your code instead of doncullen's I get an error saying that aData is undefined.

    Vern
  • doncullendoncullen Posts: 32Questions: 2Answers: 0
    Vern, add aData to var declaration. It's at the 2nd line of his code.
  • bigdaddyvbigdaddyv Posts: 29Questions: 0Answers: 0
    No matter what I do, don's code works and allen's doesn't. It's strange.
  • allanallan Posts: 63,514Questions: 1Answers: 10,472 Site admin
    My code above does look a bit suspect... Try this one:

    [code]
    function findRowIndexUsingCol(StringToCheckFor, tableToCheck, iColumn){
    var i, c, oTable, aaRows;

    oTable = $(tableToCheck).dataTable();
    aaRows = oTable.fnGetData();

    for ( i=0, c=aaRows.length ; i
  • doncullendoncullen Posts: 32Questions: 2Answers: 0
    Allan- any word on when you'll be wrapping the function up into an API?
  • doncullendoncullen Posts: 32Questions: 2Answers: 0
    P.S. Try Allan's updated code above-- when I get to work tomorrow, I'll open up my code and see what I used. I'm not sure if I used his code, or continued to use the function I coded.

    By the way, Vern, if you're going to use my code, change the last few lines to this:

    [code] if(sValue == StringToCheckFor){
    return i; // match found, return location
    }
    }

    return -1; // match not found
    }[/code]

    I just noticed my original code didn't catch cases where no match was found. The changes I just gave you will make sure it catches that.
  • allanallan Posts: 63,514Questions: 1Answers: 10,472 Site admin
    I've put together to plug-in API functions based on our conversation here:

    http://datatables.net/plug-ins/api#fnFindCellRowIndexes - which returns an array of data indexes
    http://datatables.net/plug-ins/api#fnFindCellRowNodes - which returns an array of nodes

    They both do basically the same thing, but you can pick which return you would prefer. I've gone with an array return rather than just a straight integer or node, to allow multiple rows to be matched. If this isn't needed, you can add in a 'return' to try and speed the code up a touch.

    Allan
  • doncullendoncullen Posts: 32Questions: 2Answers: 0
    edited December 2010
    Allan-

    To make it easier on people who use the API, you could add an optional limit parameter to the function. Using your fnFindCellRowIndexes function as an example (keep in mind, the modification is untested):

    [code]$.fn.dataTableExt.oApi.fnFindCellRowIndexes = function ( oSettings, sSearch, iColumn, iLimit)
    {
    var
    i,iLen, j, jLen, iCount, bFound,
    aOut = [], aData;
    bFound = false;
    if( typeof iLimit == "undefined" ) iLimit = false;
    for ( i=0, iLen=oSettings.aoData.length ; i
  • allanallan Posts: 63,514Questions: 1Answers: 10,472 Site admin
    Yup not a bad idea at all - note made, I'll put that into it.

    Allan
This discussion has been closed.