Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Joomla- adding sortable columns to a table in a component

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 956
    Comment on it

    Adding sortable columns to Users Table in Admin section

    <th class="left"-->
        <!--?php echo JHtml::_('grid.sort', 'Candidate Number', 'candidate_number', $listDirn, $listOrder); ?-->
    </th>
    

    Adding sortable columns to a table in a component

    Step 1=> Model


    A)For sortable columns first we have to populate the order state of our model.
    protected function populateState($ordering = null, $direction = null) {
        parent::populateState('default_column_name', 'ASC');
    }
    

    Replace 'default_sort_column' with the column we want to sort by default.
    We can also change 'ASC' to 'DESC', depending on which way we want to sort by default.

    B) Creating $config['filter_fields'] array of sortable columns :
    public function __construct($config = array())
        {   
            $config['filter_fields'] = array(          
                'candidate_number',         
                'username'
            );
            parent::__construct($config);
        }
    
    C)Build an SQL query to load the list data.
    public function getListQuery() {
            $db = JFactory::getDbo();
            $query = $db->getQuery(true);  
            // ...                  
            // Add the list ordering clause in last.
            $query->order($db->escape($this->getState('list.ordering', 'default_sort_column')).' '.$db->escape($this->getState('list.direction', 'ASC')));
            return $query;
    
        }
    

    Step 2: The View

    Now we will assign sortable columns to the view so that they show up on the page when it is displayed. Add following code to view.html.php file.

    public function display($tpl=null) {
        $items = $this->get('Items');
        $state = $this->get('State');
    
        $this->sortDirection = $state->get('list.direction');
        $this->sortColumn = $state->get('list.ordering');
    
        parent::display($tpl);
    }
    

    Step 3: The Template

    grid.sort: so that JHTML will insert the correct behaviour for a sortable column.
    Candidate Number: Name of column that is displayed to the user on table.
    candidate_number: Name of datbase field.
    $this->sortDirection: Current order direction.
    $this->sortColumn: Currently sorted column name.

    <form action="index.php?option=com_users" method="post" name="adminForm">    
        <table class="adminlist" cellpadding="1">                   
            <th class="title">
            <?php echo JHTML::_('grid.sort',   'Candidate Number', 'candidate_number', $this->sortDirection, $this->sortColumn ); ?>
            </th>                           
            <td>
                <?php echo $row->candidate_number; ?>
            </td>
        </table>
    </form>
    
    
    <script language="javascript" type="text/javascript">
    function tableOrdering( order, dir, task )
    {
        var form = document.adminForm;
    
        form.filter_order.value = order;
        form.filter_order_Dir.value = dir;
        document.adminForm.submit( task );
    }
    </script>
    

    Reference

    https://docs.joomla.org/Adding_sortable_columns_to_atable_in_acomponent

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: