Updating jQuery Tablesorter plugin after removing a row from DOM

前端 未结 7 623
迷失自我
迷失自我 2020-11-30 07:40

I have some code at the moment that hides a row that is deleted and then removes it using the .remove() function.

However I\'m having difficulty is making it remain

7条回答
  •  遥遥无期
    2020-11-30 08:15

    Jitter solution was almost working for me although a line was missing for update (see code below). I've extended the code to allow insert new TR's in table.

    I've been playing around and it works for me under FFox, didn't check on IExplorer. Anyway there's a bug I couldn't fix yet: If you add a new TR and then you try to delete it it won't be deleted from table :(

    // "borrowed" from John Resig: Javascript Array Remove
    // http://ejohn.org/blog/javascript-array-remove/
    Array.prototype.remove = function(from, to) {
        var rest = this.slice((to || from) + 1 || this.length);
        this.length = from < 0 ? this.length + from : from;
        return this.push.apply(this, rest);
    };
    
    //repopulate table with data from rowCache
    function repopulateTableBody(tbl) {
        //aka cleanTableBody from TableSorter code
        if($.browser.msie) {
            function empty() {
                while ( this.firstChild ) this.removeChild( this.firstChild );
            }
            empty.apply(tbl.tBodies[0]);
        } else {
            tbl.tBodies[0].innerHTML = "";
        }
        jQuery.each(tbl.config.rowsCopy, function() {
            tbl.tBodies[0].appendChild(this.get(0));
        });
    }
    //removes the passed in row and updates the tablesorter+pager
    function tablesorter_remove(tr, table) {
        //pager modifies actual DOM table to have only #pagesize TR's
        //thus we need to repopulate from the cache first
        repopulateTableBody(table.get(0));
        var index = $("tr", table).index(tr)-2;
        var c = table.get(0).config;
        tr.remove();
    
        //remove row from cache too
        c.rowsCopy.remove(index);
        c.totalRows = c.rowsCopy.length;
        c.totalPages = Math.ceil(c.totalRows / config.size);
    
        //now update
        table.trigger("update");
        table.trigger("appendCache");
    
        //simulate user switches page to get pager to update too
        index = c.page < c.totalPages-1;
        $(".next").trigger("click");
        if(index)
            $(".prev").trigger("click");
    }
    
    function tablesorter_add(tr, table) {
        //pager modifies actual DOM table to have only #pagesize TR's
        //thus we need to repopulate from the cache first
        repopulateTableBody(table.get(0));
        table.append(tr);
    
        //add row to cache too
        var c = table.get(0).config;
        c.totalRows = c.rowsCopy.length;
        c.totalPages = Math.ceil(c.totalRows / config.size);
    
        //now update
        table.trigger("update");
        table.trigger("appendCache");
    
        //simulate user switches page to get pager to update too
        index = c.page < c.totalPages-1;
        $(".next").trigger("click");
        if(index)
            $(".prev").trigger("click");
    
        //Set previous sorting method
        var sorting = c.sortList;
        if(sorting == '')
            sorting = [[0,0]];
        table.trigger("sorton", [sorting]);
    }
    

    And you can use is as follows:

    Add new TR with complex HTML in it:

    tablesorter_add(''+data.genus+''+data.species+'', $("#orchid_list"));
    

    Remove any TR:

    tablesorter_remove($("#"+orchid_id),$("#orchid_list"));
    

    My simplified sample table:

    Género Especie
    Amitostigma capitatum
    Amitostigma tetralobum

提交回复
热议问题