Custome Joomla administrator Component Ordering and Positions Rows in Model-View-Controller(MVC)

When creating a custom component in Joomla administrator area it is somewhat frequent that one wants the ability to order items based on an order value associated with each row.

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

I hope to add a little more explanation.

Please go step by step it’s more help.

sterp 1: in component_name/view/view_name/tmpl/default.php

add following line:
<th width=”8%” align=”left”>
<?php echo JHTML::_(‘grid.sort’,  JText::_(‘Order’), ‘c.ordering’, @$this->lists[‘order_Dir’], @$this->lists[‘order’] ); ?>
<?php echo JHTML::_(‘grid.order’,  $this->items ); ?>
</th>

and
<td>
<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; ?>” style=”text-align: center” />
</td>

Step 2: in component_name/controllers/yourviewcontroller.php

function saveorder( )
{
// Check for request forgeries
$cid        = JRequest::getVar( ‘cid’, array(), ‘post’, ‘array’ );

$model      = $this->getModel(‘yourmodelname’);

if( $model->saveOrder($cid)){
$msg     = JText::_( ‘Ordering save Successfully’ );
$type = ‘message’;

}else{
$msg     = JText::_( ‘Ordering not save’ );
$type = ‘notice’;
}

$link = ‘index.php?option=component_name&view=viewname’;
$this->setRedirect( $link, $msg, $type );
}

function orderup()
{
$cid = JRequest::getVar( ‘cid’, array(0), ‘post’, ‘array’ );
$id = $cid[0];
$model = $this->getModel(‘yourmodelname’);
$model->orderproducts($id,-1);

$msg     = JText::_( ‘Ordering up Successfully’ );

$link = ‘index.php?option=component_name&view=viewname’;
$this->setRedirect( $link, $msg );
}

function orderdown()
{
$cid = JRequest::getVar( ‘cid’, array(0), ‘post’, ‘array’ );
$id = $cid[0];
$model = $this->getModel(‘yourmodelname’);
$model->orderproducts($id,1);
$msg     = JText::_( ‘Ordering down Successfully’ );

$link = ‘index.php?option=component_name&view=viewname’;
$this->setRedirect( $link, $msg );
}

Step 3 : component_name/models/yourviewmodel.php

function saveOrder($cid)
{
JArrayHelper::toInteger($cid);
$total        = count( $cid );
$order        = JRequest::getVar( ‘order’, array(0), ‘post’, ‘array’ );

JArrayHelper::toInteger($order, array(0));
$row  =& $this->getTable(”);
// 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())
{
$this->setError($this->_db->getErrorMsg());
return false;
}
}
}

return true;
}

function orderProducts( $id, $inc )
{
$row  =& $this->getTable(”);
$row->load( $id );
$row->move($inc);
}

Cheers, your Ordering and Positions Rows in component work now!.

Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s