我正在使用 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>£".number_format($priceWhenNew)."</td><td>£".number_format($nearlyNewPrice)."</td><td class='red'>£".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/