jquery tablesorter index column insert

后端 未结 1 1266
我在风中等你
我在风中等你 2020-12-02 03:12

I have a PHP generated table from a postgres query. How can I insert an index column with numbered rows at the beginning of the table using tablesorter plugin? The sorting w

相关标签:
1条回答
  • 2020-12-02 03:38

    I'm not that great with php, but couldn't you just do this?

    echo "<table id=\"tabel\" class=\"tablesorter\">
    <thead>
    <tr>
    <th>#</th>";
    .
    .
    .
    //next code fetch cells content
    echo "<tbody>";
    
    $i=1;
    
    while ($row=pg_fetch_row($result)){
        echo "<tr>";
        echo "<td> $i </td>";
        $i++;
    
        foreach($row as $_column){
        echo "<td> $_column </td>";
        }
        echo "</tr>";
    
    }
    

    If you want a that column to not sort and stay unchanged, you can use the following widget (demo) with header option to prevent sorting:

    // target the number column using a zero-based index
    var number_column = 0;
    
    // add custom numbering widget
    $.tablesorter.addWidget({
        id: "numbering",
        format: function(table) {
            var c = table.config;
            $("tr:visible", table.tBodies[0]).each(function(i) {
                $(this).find('td').eq(number_column).text(i + 1);
            });
        }
    });
    
    $("table").tablesorter({
        theme: 'blue',
        // prevent first column from being sortable
        headers: {
            0: { sorter: false }
        },
        // apply custom widget
        widgets: ['numbering']
    });
    
    0 讨论(0)
提交回复
热议问题