【问题标题】:jquery tablesorter index column insertjquery tablesorter索引列插入
【发布时间】:2013-09-12 11:29:38
【问题描述】:

我有一个从 postgres 查询生成的 PHP 表。如何使用 tablesorter 插件在表的开头插入带有编号行的索引列?排序工作。谢谢。

Klejgkekrj

qwef

<html>
    <head>
        <link href="/media/css/blue/style.css" rel="stylesheet">
        <script src="/media/js/jquery.js" type="text/javascript"></script>
        <script src="/media/js/jquery.tablesorter.js" type="text/javascript"></script>
        <script src="/media/js/jquery.tablesorter.widgets.js" type="text/javascript">  </script>
        <script src="/media/js/jquery.tablesorter.pager.js" type="text/javascript"></script>

        <script type="text/javascript">
            $(function(){
              $("#tabel").tablesorter({ widgets: [ 'zebra' , 'filter' ] })

            });
        </script>               
    </head>

<body>

<?php

$con = pg_connect("user=* password=* host=localhost port=5432 dbname=users ") or die (pg_last_error());
$query = "SELECT * from users";
$result = pg_query($con, $query);

echo "<table id=\"tabel\" class=\"tablesorter\">
<thead>
<tr>";
//next code get column names
for($i=0; $i < pg_num_fields($result); $i++){
    $field_info = pg_field_name($result, $i);
 echo "<th> $field_info  </th>";
}
echo "
</tr>
</thead>";


//next code fetch cells content
echo "<tbody>";

while ($row=pg_fetch_row($result)){
    echo "<tr>";
    foreach($row as $_column){
    echo "<td> $_column </td>";
    }
    echo "</tr>";

}
echo "</tbody>
</table>";

pg_close($con);

?>
</body>
</html>

【问题讨论】:

    标签: php jquery tablesorter


    【解决方案1】:

    我不是很擅长 php,但你不能这样做吗?

    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>";
    
    }
    

    如果您希望该列不排序并保持不变,您可以使用带有标题选项的以下小部件 (demo) 来防止排序:

    // 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']
    });
    

    【讨论】:

    • 我已经用 php 生成了该列,但我需要使用 jquery tablesorter 插件生成它,因为我想对该表进行一些过滤,因此第一列需要保持不变。(需要重新索引它在我进行的每一次过滤中)
    • 如果我在你的例子中使用 php 生成的列,我如何从排序第一列中忽略?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-07
    • 2014-06-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多