1

I want to add field sort order in admin grid and in frontend part (sort by). I make module "news" and want to sort my news by title.

Please guide me how to do it

protected function _prepareCollection()
{
    $collection = Mage::getModel('mdnews/news')->getCollection();
    $this->setCollection($collection);
    return parent::_prepareCollection();
}
protected function _prepareColumns()
{
    $helper = Mage::helper('mdnews');

    $this->addColumn('news_id', array(
    'header' => $helper->__('News ID'),
    'index' => 'news_id',
    'width' => '100px',
    ));

   $this->addColumn('title', array(
    'header' => $helper->__('Title'),
    'index' => 'title',
    'type' => 'text',
   ));

   $this->addColumn('category', array(
    'header' => $helper->__('Category'),
    'index' => 'category_id',
    'options' => $helper->getCategoriesList(),
    'type'  => 'options',
    'width' => '150px',
   ));

   $this->addColumn('created', array(
    'header' => $helper->__('Created'),
    'index' => 'created',
    'type' => 'date',
   ));

   return parent::_prepareColumns();
}
2
  • kindly add your grid code for better understanding the question Commented Jan 27, 2016 at 11:11
  • sorry, added my code
    – O.Petrov
    Commented Jan 27, 2016 at 11:15

1 Answer 1

2

add these two in _construct this will do in admin..

public function __construct()
   { 
$this->setDefaultSort('title'); //default field to sort
$this->setDefaultDir('ASC'); //sort by ASC ot DESC

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.