3

My sales order table has total 17000 records. Need to select and show only last 300 records in sales order grid. I have tried the below code, but it's not working

protected function _prepareCollection()
{
    $collection = Mage::getResourceModel($this->_getCollectionClass());
    $collection->getSelect()->limit(300);
    $this->setCollection($collection);
    return parent::_prepareCollection();
}
6
  • This gets controlled through the "View 20 per page" dropdown above the grid - you can't do it in the _prepareCollection method
    – Erfan
    Sep 16, 2015 at 7:05
  • Magento showing 20 orders by default in sales grid and you can change it to max 200 from drop-down. Do you want to set 300 by default on the page ? Sep 16, 2015 at 7:08
  • Ok. Is there any option to select only 300 records? I don't want the 17000 records Sep 16, 2015 at 7:08
  • @AkhileshPatel No. In top of the sales order grid, total recotds count shown as 170000. I selected "View 20 per page" option and there are total 856 pages Sep 16, 2015 at 7:10
  • I want to show it as 300 records as total count. Please check my code i limited the records Sep 16, 2015 at 7:13

1 Answer 1

1

Here is the working code:

protected function _prepareCollection()
{
    $this->setDefaultLimit(200);
    $collection = Mage::getResourceModel($this->_getCollectionClass());
    $collection->getSelect()->limit(300);
    $this->setCollection($collection);
    return parent::_prepareCollection();
}

Since the max limit is 200, you have to set the default limit to 200 and select query limit to 300. This will give you two grid pages.

1
  • 1
    Which file is being changed here?
    – snh_nl
    Aug 27, 2020 at 13:36

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.