Tom T
Tom T

Reputation: 439

using an array as the data-source for CGridView

Im hoping this will work.. Im writing and invoicing application and would like to hold the current invoice in an array which can be housed in a blob field. This would also be used for archive changes etc.

The sale items of the the invoice are displayed using CGridView. The only thing is all the documentation says the data source is supposed to be an IDataProvider. I don't want to store the entire object in my Db but something similar to this:

invoice->array(
                InvoiceHeader->array(//header information),
                InvoiceItems->array(
                                   item_1->array( 
                                                 item_id-> '1',
                                                 item_count->'3',
                                                 ....
                                                 ),
                                   ),
               ),

I would then like to do this in my view:

$this->widget('zii.widgets.grid.CGridView', array(
               'dataProvider'=>$this->invoiceItems,
              ));

-- side note. The permanent storage is a series of tables, this would be used to hold active records encase of browser errors, etc. The current system does this directly in the tables but leads to non concurrent invoice numbers and inaccurate stats.

Upvotes: 8

Views: 9898

Answers (2)

Tom T
Tom T

Reputation: 439

This is an extension to my issue and solution. Mukesh's answer is 100% correct. but I needed to add buttons to the grid but when I tried to add the delete button I was getting an error. You need to spcify the url in the button array like so:

 array( // delete button
      'class'=>'CButtonColumn',
      'template'=>'{delete}',
      'buttons'=>array(
              'delete'=>array(
                          'url'=>'Yii::app()->controller->createUrl(\'Invoicing/invoiceBody/test\', array(\'id\'=>$data["id"]))',
                        ),
               ),
                                ),

please notice the the id is declared by using:

array('id', $data['id'])

not:

array('id', $data->id)

Upvotes: 4

Mukesh Soni
Mukesh Soni

Reputation: 6668

you can first wrap your array in CArrayDataProvider and then use it in CGridView -

$invoiceItemsDataProvider = new CArrayDataProvider($this->invoiceItems);
$this->widget('zii.widgets.grid.CGridView', array(
               'dataProvider'=>$invoiceItemsDataProvider,
              ));

Upvotes: 14

Related Questions