简体   繁体   中英

Sorting HTML Table With TableSorter

I am attempting to add sortable columns to my html table and I thought I would give the jquery tablesorter a try. This is my syntax sans the actual DB call, and I think I have it set-up properly, however my table is not allowing me to sort. Why am I not able to sort?

    <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>

EDIT --->
I edited my syntax to read like this, but still have the issue

</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>

EDIT 2
Below is update to show how $query get's it's value

    <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循环。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM