Problem with fnAddTr and filtering/searching.

Problem with fnAddTr and filtering/searching.

cejenkincejenkin Posts: 2Questions: 0Answers: 0
edited October 2009 in General
If this has been answered, apologies.

I am using the fnAddTr which is working well, except for filtering/searching. When I add a row, the row will display correctly if I do not have a filter associated with the table. If I add a filter I will see all the old rows correctly but not the new one. I get the same behavior with searches. Is there someway to refresh the data that datatable is "seeing"?

Also, in the fnAddTr plug-in, the method _fnReDraw is undefined. I was able to change the section to the following:

if (bRedraw) {
this.fnDraw();
}

I did a bit of digging and found that fnDraw just calls _fnReDraw, so I figured there would be no ill effects and everything seems to be working.

Love this plug-in, has saved me tons of time and makes my users quite happy.

Replies

  • allanallan Posts: 63,498Questions: 1Answers: 10,470 Site admin
    Hi cejenkin,

    Thanks for the kind words about DataTables :-)

    With your fix for redrawing the table (I've just updated the plug-in to call this.oApi._fnReDraw()), does this allow your filtering to work as expected? DataTables will probably only apply the filter when it does the redraw.

    Regards,
    Allan
  • cejenkincejenkin Posts: 2Questions: 0Answers: 0
    Hmm, when I change the line to:

    [code]this.oApi._fnReDraw(oSettings);[/code]

    this pops up in Firebug:

    "this.oApi._fnReDraw is not a function"

    My page header looks like this:

    [code]








    /*
    * Function: fnAddTr
    * Purpose: Add a TR element to a table
    * Returns: -
    * Inputs: object:oSettings - automatically passed by DataTables
    * node:nTr - TR element to add to table
    * bool:bRedraw - optional - should the table redraw - default true.
    * Usage: var row = '12345';
    oTable.fnAddTr( $(row)[0] );
    */
    $.fn.dataTableExt.oApi.fnAddTr = function(oSettings, nTr, bRedraw) {
    if (typeof bRedraw == 'undefined') {
    bRedraw = true;
    }

    var nTds = nTr.getElementsByTagName('td');
    if (nTds.length != oSettings.aoColumns.length) {
    alert('Warning: not adding new TR - columns and TD elements must match');
    return;
    }

    var aData = [];
    for (var i = 0; i < nTds.length; i++) {
    aData.push(nTds[i].innerHTML);
    }

    /* Add the data and then replace DataTable's generated TR with ours */
    var iIndex = this.oApi._fnAddData(oSettings, aData);
    oSettings.aoData[iIndex].nTr = nTr;

    oSettings.aiDisplay = oSettings.aiDisplayMaster.slice();
    this.oApi._fnBuildSearchArray(oSettings, 1);

    if (bRedraw) {
    this.oApi._fnReDraw(oSettings);
    //this.fnDraw();
    }
    }


    [/code]

    The thing is, I can see the _fnReDraw function in the datatables js file. Is there some order of declaration in my header that is not allowing the fnAddTr to "see" the function?
  • allanallan Posts: 63,498Questions: 1Answers: 10,470 Site admin
    Hi cejenkin,

    Oops - it looks like you've discovered a bug in DataTables there! _fnReDraw() wasn't attached to the oApi object and it should have been. I've got this fixed in the development version of DataTables and that will hopefully be released as 1.5.3 sometime fairly soon.

    Until that time, I think your fix to call this.fnDraw() does the job very nicely indeed!

    Regards,
    Allan
This discussion has been closed.