我正在尝试在我的HTML表中添加可排序的列,我想我会尝试一下jQuery Tablesorter。这是我的语法没有实际的数据库呼叫,我认为我已经正确设置了它,但是我的桌子不允许我进行排序。为什么我不能排序?
<head>
<script type="text/javascript" src="/path/to/jquery-latest.js"></script>
<script type="text/javascript" src="/path/to/jquery.tablesorter.js"></script>
<script>
$(document).ready(function()
{
$("#SaleDistro").tablesorter();
}
);
</script>
</head>
<table id="SaleDistro" class="tablesorter" border="1">
<thead>
<tr>
<th>Sales Name </th>
<th>Sales Region </th>
<th>Sales Count </th>
<th>Sales Supervisor </th>
</tr>
</thead>
<?php
foreach ($query as $res)
{
print "<tbody>";
print "<tr>";
print "<td>" . $res->sn . "</td>";
print "<td>" . $res->sr . "</td>";
print "<td>" . $res->sc . "</td>";
print "<td>" . $res->ss . "</td>";
print "</tr>";
print "</tbody>";
}
?>
</table>
</html>
编辑--->
我编辑了我的语法以这样阅读,但仍然有问题
</thead>
<tbody>
<?php
foreach ($query as $res)
{
print "<tr>";
print "<td>" . $res->sn . "</td>";
print "<td>" . $res->sr . "</td>";
print "<td>" . $res->sc . "</td>";
print "<td>" . $res->ss . "</td>";
print "</tr>";
}
?>
</tbody>
</table>
</html>
编辑2
以下是更新以显示$query
如何获取它的值
<head>
<script type="text/javascript" src="/path/to/jquery-latest.js"></script>
<script type="text/javascript" src="/path/to/jquery.tablesorter.js"></script>
<script>
$(document).ready(function()
{
$("#SaleDistro").tablesorter();
}
);
</script>
</head>
<?php
$option = array();
$option['driver'] = 'mssql';
$option['host'] = 'IP Address';
$option['user'] = 'username';
$option['password'] = 'password';
$option['database'] = 'database';
$option['prefix'] = '';
$db = JDatabase::getInstance($option);
$query = $db->getQuery(true);
$query = "Select SalesName, SalesRegion, SalesCount, SalesSupervisor from salesdata;";
$db->setQuery($query);
$query = $db->loadObjectList();
if ($query)
{
?>
<table id="SaleDistro" class="tablesorter" border="1">
<thead>
<tr>
<th>Sales Name </th>
<th>Sales Region </th>
<th>Sales Count </th>
<th>Sales Supervisor </th>
</tr>
</thead>
<?php
foreach ($query as $res)
{
print "<tbody>";
print "<tr>";
print "<td>" . $res->sn . "</td>";
print "<td>" . $res->sr . "</td>";
print "<td>" . $res->sc . "</td>";
print "<td>" . $res->ss . "</td>";
print "</tr>";
print "</tbody>";
}
?>
</table>
</html>
您只需要一个<tbody>
打开和关闭标签,因此您需要将它们移出foreach
环路。