只需单击

时间:2016-01-08 14:16:31

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

我正在使用Tablesaw,当您单击表标题时对其进行排序,但是当脚本加载时,脚本不允许您对特定列进行排序。

所以我的问题是如何通过我想要的任何列对表进行排序?

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

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

1 个答案:

答案 0 :(得分:1)

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

$sql = "SELECT * FROM $table ORDER BY Model 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>";