php - 排序 table 锯表加载而不仅仅是点击

标签 php mysql sorting html-table jquery-ui-sortable

我正在使用 Tablesaw,当您单击表格标题时,它会对数据进行排序,但是该脚本不允许您在页面加载时对特定列进行排序。

那么我的问题是如何使表格可以按我想要的任何列排序?

Tablesaw 演示:http://filamentgroup.github.io/tablesaw/demo/sort.html

台锯 https://www.filamentgroup.com/lab/tablesaw.html

我们还从数据库中调用数据...这是我们的代码...

<?php 

    // Function to create the HTML table using the data stored in the database.
    function displayTable($table) {
      include "connection.php";
      $sql = "SELECT * FROM $table";
      $result = mysqli_query($conn, $sql)or die(mysql_error());

      if (!mysqli_num_rows($result)==0) {
        print "<table id='carTable' class='tablesaw paginated' data-tablesaw-sortable data-tablesaw-sortable-switch><thead>";
        print "<tr><th scope='col' data-tablesaw-sortable-col>Model</th><th scope='col' data-tablesaw-sortable-col>Pack</th><th scope='col' data-tablesaw-sortable-col>Colour</th><th scope='col' data-tablesaw-sortable-col>Registration</th><th scope='col' data-tablesaw-sortable-col data-sortable-numeric data-tablesaw-sortable-default-col>Price When New</th><th scope='col' data-tablesaw-sortable-col data-sortable-numeric>New Price</th><th scope='col' data-tablesaw-sortable-col data-sortable-numeric><span class='red'>Saving</span></th><th scope='col' data-tablesaw-sortable-col>Retail Centre</th></tr></thead><tbody>";

        while($row = mysqli_fetch_array($result)){
          $variant = $row['Variant'];
          $model = $row['Model'];
          $pack = $row['Pack'];
          $colour = $row['Colour'];
          $regNo = $row['RegNo'];
          $priceWhenNew = $row['PriceWhenNew'];
          $nearlyNewPrice = $row['NearlyNewPrice'];
          $saving = $row['Saving'];
          $retailCentre = $row['RetailCentre'];


          print "<tr><td>".$model."</td><td>".$pack."</td><td>".$colour."</td><td class='regno'>".$regNo."</td><td>&#163;".number_format($priceWhenNew)."</td><td>&#163;".number_format($nearlyNewPrice)."</td><td class='red'>&#163;".number_format($saving)."</td><td class='retailer'>".$retailCentre."</td><td><a href='#' class='wide-btn contact-btn'>CONTACT US FOR MORE INFO</a></td></tr>";

    } // End while loop
    print "</tbody></table>"; 
    }
    else {
      print "<h1>No results found.</h1>";
    }
    }
    ?>

最佳答案

您可以首先以正确的顺序从数据库获取结果。假设您希望表默认按“型号”排序。您的 SQL 语句需要是:

$sql = "SELECT * FROM $table ORDER BY 模型 ASC";

此外,根据 https://github.com/filamentgroup/tablesaw 处的文档,按照正确的顺序输出结果后您需要将 data-tablesaw-sortable-default-col 放入要默认排序的列中。

因此,在您的示例中,这将变为:

print "<tr>
<th scope='col' data-tablesaw-sortable-col data-tablesaw-sortable-default-col>Model</th>
<th scope='col' data-tablesaw-sortable-col>Pack</th>
<th scope='col' data-tablesaw-sortable-col>Colour</th>
<th scope='col' data-tablesaw-sortable-col>Registration</th>
<th scope='col' data-tablesaw-sortable-col data-sortable-numeric>Price When New</th>
<th scope='col' data-tablesaw-sortable-col data-sortable-numeric>New Price</th>
<th scope='col' data-tablesaw-sortable-col data-sortable-numeric><span class='red'>Saving</span></th>
<th scope='col' data-tablesaw-sortable-col>Retail Centre</th>
</tr></thead><tbody>";

关于php - 排序 table 锯表加载而不仅仅是点击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34679168/

相关文章:

php - 在PHP页面上显示泰语字符/泰语字符可以在服务器的数据库中显示

php - 多个系列、多个图表 Highcharts

用于检查当前日期是否比数据库中的日期新的php代码

c++ - n 作为参数传递给函数后为 0,这里似乎有什么问题?

java - 对整数数组的 ArrayList 进行排序

php - Wamp vhost docroot 始终显示为 c :\wamp\www not the vhost docroot

java - 如何通过 Android 应用程序与外部 SQL 数据库通信?

php - 如何使用 PHP 从 XML 中获取标签的属性?

c - 如果在冒泡排序程序中输入偶数个整数,则在中间插入一个随机数

php - 希伯来语正则表达式匹配在 php 中不起作用