$(...).remove(...).draw is not a function.Why it's not working?

$(...).remove(...).draw is not a function.Why it's not working?

MS_26MS_26 Posts: 1Questions: 1Answers: 0

"fnRowCallback" : function(nRow,aData, iDisplayIndex,iDisplayIndexFull) {
$.each(aData,function(k, v) {

                if (k == "ACCOUNT_NUMBER") {
                    if (v == mirror_account_number) {
                        ```$(nRow).remove().draw();```
                    }
                                }

            })
           },

Answers

  • rf1234rf1234 Posts: 2,808Questions: 85Answers: 406
    edited November 2018

    I used the newer syntax. Try this please:

    rowCallback: function ( row, data ) {
        if (data.k == "ACCOUNT_NUMBER") {
           if (data.v == mirror_account_number) {
               $(row).remove();
               yourTable.draw();
        }
    }
    

    If that doesn't work you might need to do the draw() later outside the callback.

  • kthorngrenkthorngren Posts: 20,309Questions: 26Answers: 4,771

    Is your goal to filter certain rows based on content?

    rowCallback is not the place to remove rows. The reason it doesn't work is $(nRow).remove() is a jQuery method and .draw() is a Datatables API. Datatables won't know about changes to the table you make using $(nRow).remove() and would result in a confusing table display.

    My suggestion is to create a Search Plugin that filters the rows you don't want to display.

    Kevin

  • rf1234rf1234 Posts: 2,808Questions: 85Answers: 406

    I guess Kevin is right! So ignore my previous post please.
    You can actually use the search functionality Kevin mentioned without creating a plugin. You just add this code on top of your Data Table definition

    $.fn.dataTable.ext.search.push(
        function( settings, data, dataIndex, row, counter ) {
            //we only do this for the second table
            if ( (typeof row.k !== 'undefined') &&
                 (typeof row.v !== 'undefined') )   {
                if (row.k == "ACCOUNT_NUMBER") {                    
                    if (row.v == mirror_account_number) {                
                        return false;
                    }
                }
            }
            return true;
        }                
    );
    

    If you have muliple Data Tables on your page this code will run for each table. Hence you need to check for "undefined" in order to avoid problems with other tables.

  • kthorngrenkthorngren Posts: 20,309Questions: 26Answers: 4,771

    You can actually use the search functionality Kevin mentioned without creating a plugin

    That is a search plugin :smile: Thanks for providing an example(I was being lazy).

    Kevin

  • rf1234rf1234 Posts: 2,808Questions: 85Answers: 406

    I always think of a separate file and an installation process when I hear plugin :smiley:
    Thanks for clarifying!
    Roland

  • kthorngrenkthorngren Posts: 20,309Questions: 26Answers: 4,771

    The term plugin does make it seem more than it really is. It is a very cool feature of Datatables.

    Kevin

This discussion has been closed.