fabrisss
(Fabrisss)
December 14, 2011, 8:46am
1
Hi,
I’m using a filterable CGridView to display a list of transactions, each transaction has a date and amount.
$this->widget('zii.widgets.grid.CGridView', array(
'id' => 'transaction-grid',
'dataProvider' => $transaction->search(),
'filter' => $transaction,
'columns' => array(
'date',
'amount',
),
));
I’d like a field displaying the sum of the amounts; this field would be updated when filtering. I tried to use ‘afterAjaxUpdate’ but it only displays the sum of ALL transactions:
'afterAjaxUpdate' => 'function(id, data) {$("#total-amount").html('.$transaction->totalAmount.');}',
NB: $transaction->totalAmount is computed and updated in $transaction->search()
What’s wrong ? Thank you for your help,
Fabrice
i guess you need calculate sum manually in controller
fabrisss
(Fabrisss)
December 14, 2011, 9:03am
3
I don’t compute sum in the controller because it is only triggered once (on page loading).
Grid filtering (and its update) are realized through Ajax and by a call to $transaction->search().
FYI, here is an excerpt of this function:
public function search() {
$criteria = new CDbCriteria;
$criteria->compare('date', $this->date, true);
$criteria->compare('amount', $this->amount, true);
// need to clone the criteria to compute sum ?
$amountCriteria = clone($criteria);
$amountCriteria->select = 'SUM(amount) AS totalAmount';
$this->totalAmount = Transaction::model()->find($amountCriteria)->totalAmount;
return new CActiveDataProvider($this, array(
'criteria' => $criteria,
'pagination' => array(
'pageSize' => 20,
),
));
}
Fabrice