jqueryjquery-jtable

jQuery jTable empty all rows


I would like to empty all rows in jTable. Is there a way to do this with something else except:

   $("div[id=tblContacts] tr").remove();

I see this method in jTable's source code, but it's a private method:

 /* Removes all rows in the table and adds 'no data' row.
        *************************************************************************/
        _removeAllRows: function (reason) {
            //If no rows does exists, do nothing
            if (this._$tableRows.length <= 0) {
                return;
            }

            //Select all rows (to pass it on raising _onRowsRemoved event)
            var $rows = this._$tableBody.find('tr.jtable-data-row');

            //Remove all rows from DOM and the _$tableRows array
            this._$tableBody.empty();
            this._$tableRows = [];

            this._onRowsRemoved($rows, reason);

            //Add 'no data' row since we removed all rows
            this._addNoDataRow();
        },

This method even adds empty row. Is there a way to call it?


Solution

  • The jTable plugin is more than just a grid / table painter, it provides a simple CRUD interface to a database. Once given the URLs for the various CRUD actions it will display the data sent from the server and manage that data on the server. The _removeAllRows method is correctly a private method, used when anticipating new data during paging or reloading of the table.

    Given the context that the jTable is a front end to a database, what does emptying all rows mean? If it means deleting all the rows in the database, then there is the jtable method deleteRows http://jtable.org/ApiReference/Methods#met-deleteRows.

    However I suspect that is not what the question means. Within the JTable paradigm the neatest way to present an empty jTable, is to request the server to send an empty list. This a special case of a filter function, so use the jTable load method http://jtable.org/ApiReference/Methods#met-load . You could either use flag on the server request to return an empty list.

    $('#myjtable').jtable('load',{empty: 'true'});
    

    or construct a filter request that will always be an empty list.

    $('#myjtable').jtable('load',{id: 0});
    

    Choose whichever approach best suites you server code. The load method does require an ajax request, and a little work on the server. If that is unacceptable (I don't mean undesirable) then you can replace the simple action URLs with a function, inside which you can either return an empty list, or perform the ajax request yourself to get actual data. Quite a lot more code to write than the one line solutions offered.