简体   繁体   中英

How to make a Joomla Table sortable?

I have added following PHP code in my Joomla webapplication. This will create a table of all users from a User Group .

How Can I make this table sortable?

Is there some Joomla/PHP code or plugin to order this list by Name? Can I add a parameter to sort this by any column?

<?php
$teachers = JAccess::getUsersByGroup(10); //change number in the brackets

echo "<table class=\"table table-striped\">";
echo "<thead>";
echo "<tr>";
echo "<th>Name</th>";
echo "<th>Street</th>";
echo "<th>Zip</th>";
echo "<th>City</th>";
echo "<th>Phone</th>";
echo "<th>Email</th>";
echo "</tr>";
echo "</thead>";
echo "<tbody>";
foreach($teachers as $user_id) {
    $user = JFactory::getUser($user_id);
    $profile = JUserHelper::getProfile($user->id);
    echo "<tr>";
    echo "<td>".$user->name."</td>";
    echo "<td>".$profile->profile['address1']."</td>";
    echo "<td>".$profile->profile['postal_code']."</td>";
    echo "<td>".$profile->profile['city']."</td>";
    echo "<td>".$profile->profile['phone']."</td>";
    echo "<td>".$user->email."</td>";
    echo "</tr>";
}
echo "</tbody>";
echo "</table>";
?>

eg if you want it make title sortable use code similar like follwing to add table headers

  <?php echo JHTML::_('grid.sort', JText::_('TITLE'),  'title',
  $this->lists['order_Dir'],   $this->lists['order'] ); ?>

and use following function in modal to build your where clause in SQL.

  getUserStateFromRequest(
  $option.'filter_order_Dir', 'filter_order_Dir', 'ASC'));

above function will return you the ASC or DSC based upon user choice

JHTMLand JHTMLGRID are useful classes to build interactive tables

details page 279

I think the easiest way would be to use jquery or use a jquery plugin. You can use DataTables or Tablesorter , they are jquery plugins that would put that feature.

I assume you want the table to sortable on the client side, which means you will need some Javascript code. There are some jquery libraries out there that do that (like DataTables), but there also Joomla specific extensions that do exactly that, like Tabulizer for Joomla at http://www.tabulizer.com with many sorting options, plus you avoid any jQuery conflicts with other extensions. Here is a sorting demo http://www.tabulizer.com/index.php/support-menu/tabulizer-tips/63-sort-second-row

You can also use plugin Szaki Table developed directly for Joomla (currently available for J! 2.5 and J! 3.x). Check it out, it's powerful and highly customizable tool.

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