Skip to content

Categories:

Joomla Component Ordering, Positioning Rows

When creating a custom component in Joomla admin area it is somewhat frequent that one wants the ability to order items based on an order value associated with each row. Just like how articles have an order in the Joomla “Article Manager”.

The following are the necessary code snippets that allow you to make this possible.

I hope to add a little more explanation to these soon.

controller.php

function saveorder()
{
	global $option;
	// Initialize variables
	$db	=& JFactory::getDBO();
	$cid	= JRequest::getVar( 'cid', array(), 'post', 'array' );
	$total	= count( $cid );
	$order= JRequest::getVar( 'order', array(0), 'post', 'array' );
	JArrayHelper::toInteger($order, array(0));
 
	$row =& JTable::getInstance('article', 'Table');
 
	// update ordering values
	for( $i=0; $i < $total; $i++ ) {
		$row->load( (int) $cid[$i] );
 
		if ($row->ordering != $order[$i]) {
			$row->ordering = $order[$i];
			if (!$row->store()) {
				JError::raiseError(500, 
                                     $db->getErrorMsg() );
			}
		}
	}
 
	$row->reorder();
 
	$msg 	= 'New ordering saved';
	$this->setRedirect('index.php?option='.$option, $msg);
}

views/all/tmp/default.php

<th width="8%">
	<?php echo JHTML::_('grid.sort',   'Position', $row->ordering, @$lists['order_Dir'], @$lists['order'] ); ?>
	<?php echo JHTML::_('grid.order',  $this->rows ); ?>
</th>

views/all/tmp/default.php

<td class="order">
        <span><?php echo $this->pagination->orderUpIcon( $i, ($i > 0), 'orderup', 'Move Up'); ?></span>
	<span><?php echo $this->pagination->orderDownIcon( $i, $n, ($i < $n ), 'orderdown', 'Move Down'); ?></span>
	<input type="text" name="order[]" size="5" value="<?php echo $row->ordering; ?>" class="text_area" style="text-align: center" />
</td>

Posted in General.

Tagged with , .


0 Responses

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.



Some HTML is OK

or, reply to this post via trackback.