jQuery table sort

后端 未结 15 2658
温柔的废话
温柔的废话 2020-11-22 09:00

I have a very simple HTML table with 4 columns:

Facility Name, Phone #, City, Specialty

I want the user to be able to sort by Faci

相关标签:
15条回答
  • 2020-11-22 09:55

    We just started using this slick tool: https://plugins.jquery.com/tablesorter/

    There is a video on its use at: http://www.highoncoding.com/Articles/695_Sorting_GridView_Using_JQuery_TableSorter_Plug_in.aspx

        $('#tableRoster').tablesorter({
            headers: {
                0: { sorter: false },
                4: { sorter: false }
            }
        });
    

    With a simple table

    <table id="tableRoster">
            <thead> 
                      <tr>
                        <th><input type="checkbox" class="rCheckBox" value="all" id="rAll" ></th>
                        <th>User</th>
                        <th>Verified</th>
                        <th>Recently Accessed</th>
                        <th>&nbsp;</th>
                      </tr>
            </thead>
    
    0 讨论(0)
  • 2020-11-22 09:55

    Here's a chart that may be helpful deciding which to use: http://blog.sematext.com/2011/09/19/top-javascript-dynamic-table-libraries/

    0 讨论(0)
  • 2020-11-22 09:56

    I ended up using Nick's answer (the most popular but not accepted) https://stackoverflow.com/a/19947532/5271220

    and combined it with the https://stackoverflow.com/a/16819442/5271220 but didn't want to add icons or fontawesome to the project. The CSS styles for sort-column-asc/desc I did color, padding, rounded border.

    I also modified it to go by class rather than by any so we could control which ones are sortable. This could also come in handy later if there are two tables although more modifications would need to be done for that.

    body:

     html += "<thead>\n";
        html += "<th></th>\n";
        html += "<th class=\"sort-header\">Name <span></span></i></th>\n";
        html += "<th class=\"sort-header\">Status <span></span></th>\n";
        html += "<th class=\"sort-header\">Comments <span></span></th>\n";
        html += "<th class=\"sort-header\">Location <span></span></th>\n";
        html += "<th nowrap class=\"sort-header\">Est. return <span></span></th>\n";
        html += "</thead>\n";
        html += "<tbody>\n"; ...
    

    ... further down the body

    $("body").on("click", ".sort-header", function (e) {
        var table = $(this).parents('table').eq(0)
        var rows = table.find('tr:gt(0)').toArray().sort(comparer($(this).index()))
        this.asc = !this.asc
        if (!this.asc) { rows = rows.reverse() }
        for (var i = 0; i < rows.length; i++) { table.append(rows[i]) }
    
        setIcon(e.target, this.asc);
    });
    

    functions:

    function comparer(index) {
            return function (a, b) {
                var valA = getCellValue(a, index), valB = getCellValue(b, index)
                return $.isNumeric(valA) && $.isNumeric(valB) ? valA - valB : valA.toString().localeCompare(valB)
            }
        }
    
        function getCellValue(row, index) {
            return $(row).children('td').eq(index).text()
        }
    
        function setIcon(element, inverse) {
    
            var iconSpan = $(element).find('span');
    
            if (inverse == true) {
                $(iconSpan).removeClass();
                $(iconSpan).addClass('sort-column-asc');
                $(iconSpan)[0].innerHTML = " &#8593 " // arrow up
            } else {
                $(iconSpan).removeClass();
                $(iconSpan).addClass('sort-column-desc');
                $(iconSpan)[0].innerHTML = " &#8595 " // arrow down 
            }
    
            $(element).siblings().find('span').each(function (i, obj) {
                $(obj).removeClass();
                obj.innerHTML = "";
            });
        }
    
    0 讨论(0)
提交回复
热议问题