0

I have a collection ,which has total 30rows,Now,i want add new row at start of grid collection.

Current code of prepareCollection() in grid.php

protected function _prepareCollection()
    {
        $card = Mage::registry('amit_data');

        Mage::registry('amit_data')->setData('temp_amount', $card->getCardAmount());
        $collection = Mage::getModel('amit/order')->getCollection()
            ->addFieldToFilter('id_amit', $card->getId());
        $collection->getSelect()->joinLeft('sales_flat_order', 'sales_flat_order.entity_id=main_table.id_order', 'sales_flat_order.increment_id as increment_id');
        //$this->_prepareInitialRow();

        $this->setCollection($collection);
        return parent::_prepareCollection();
    }

Current state enter image description here I want this type work

enter image description here

1

Amit Bera's solution is in the right direction. Depending on what type of Collection it is Mage_Eav_Model_Entity_Collection_Abstract or Mage_Core_Model_Resource_Db_Collection_Abstract that might not work though, and you can only add an object of the class you specified when you initialised the collection with ($this->_init('customer/customer'); in the _construct method for example).

Try this:

$first = Mage::getModel('amit/order')
    ->setUsedAmount('foo')
    ->setRemainingAmount('bar');

$collection->addItem($first);

$this->setCollection($collection);

Varien_Data_Collection::addItem adds an item at the end of the collection, but because the collection hasn't been loaded yet at that point - because of lazy loading - it's index will be at the beginning of the array.

0

Create a new Varien object, and add with your current collection. For example

$data = array(
        'id' => 0,
        'firstname' => 'firstname',
        'lastname' => 'lastname',
        'gender' => 'Male',
        'created_at' => '2014-04-02 12:22:00'
    );
    $obj = new Varien_Object($data);
    $collection->addItem($obj);
    $this->setCollection($collection);
  • Sahel raha ,it is not woking – Amit Bera Apr 2 '14 at 12:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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