Question

I am having problems with Yii (using 1.1.x), I am using the TbGridView to display the bootstrap grid view with filtering & paging.

It works ok however when I select from the 'fulfilled' (Yes, No or All) dropdown when I try to use the pager (e.g to go page 2 of the page recordset) it doesn't retain this 'fulfilled' option that I have selected. So e.g if I have selected 'No' on the fulfilled dropdown and then use the pager it doesn't retain the 'No' value in the dropdown menu for some reason

Can anyone suggest any ideas as I am totally at a lost - although I would presume it is related to my model function?

<?php $this->widget('bootstrap.widgets.TbGridView',array(
'id'            => 'shoppurchases-grid',
'type'          => 'striped bordered condensed',
'dataProvider'  => $model->setPurchaseType('org')->search(10),
'filter'        => $model,
'template'      => '{summary}{items}{pager}',
'columns'       => array(
    array(
        'header'    =>  'Product Image',
        'value'     => 'CHtml::image($data->product->displayImageUrl, $data->product->product_name, array("class"=>"grid-image"))',
        'type'      => 'raw',
    ),
    array(
        'header'    =>  'Product',
        'name'      =>  'product.product_name',
        'value'     => '$data->product->product_name',
        'filter'    => CHtml::activeTextField($model, 'filterProductName', array('placeholder' => 'filter by product name')),
        'type'      => 'raw',
    ),
    array(
        'header'    =>  'Username',
        'name'      =>  'user.firstname',
        'value'     =>  '($data->user instanceof MyUser) ? CHtml::link(CHtml::encode($data->user->username),array("/carrot/myuser/update","id"=>$data->user->user_id)) : ""',
        'filter'    => CHtml::activeTextField($model, 'filterUserName', array('placeholder' => 'filter by username')),
        'type'      =>  'raw'
    ),
    array(
        'header'    =>  'Form Name',
        'name'      =>  'user.form_name',
        'value'     => '($data->user instanceof MyUser) ? $data->user->form_name : ""',
        'filter'    => CHtml::activeTextField($model, 'filterFormName', array('placeholder' => 'filter by form name')),
        'type'      => 'raw'
    ),
    array(
        'header'    =>  'Student',
        'name'      =>  'user.username',
        'value'     => '($data->user instanceof MyUser) ? $data->user->fullname : ""',
        'filter'    => CHtml::activeTextField($model, 'filterFirstName', array('placeholder' => 'filter by first name')),
        'type'      => 'raw'
    ),
    array(
        'header'    =>  'Price',
        'name'      =>  'price',
        'filter'    => BHtml::activeCompareableTextField($model, 'price', array('placeholder' => 'filter by price')),
        'type'      => 'raw'
    ),
    array(
        'header'    =>  'Purchase Time',
        'name'      =>  'purchase_time',
        'value'     => 'app()->dateFormatter->format("dd/MM/y H:m:s", $data->purchase_time)',
        'filter'    => BHtml::activeCompareableDateRange($model, 'purchase_time', array('placeholder' => 'filter by purchase time')),
        'type'      => 'raw'
    ),

    array(
        'header'    => 'Fulfilled',
        'name'      => 'fulfilled',
        'value'     => user()->hasAuth(Group::READ_ONLY, "equal") ? '$data->fulfilled ? "Yes" : "No"' : 'CHtml::activeCheckBox($data, "fulfilled")',
        'filter'    => CHtml::activeDropDownList($model, 'fulfilled', array(0 => 'No', 1 => 'Yes'), array('prompt' => 'All')),
        'type'      => 'raw',
        'htmlOptions'       => array('class' => 'align-center'),
        'headerHtmlOptions' => array('class' => 'align-center'),
    ),
    array(
        'name'              => 'organisation_name',
        'visible'           => ((user()->hasAuth(Group::GROUP_ADMIN, 'equal')) && (!user()->hasState('view_org'))),
        'filter'            => CHtml::activeDropDownList($model, 'organisation_id', CHtml::listData(Organisation::model()->leaChildren, 'organisation_id', 'organisation_name'), array('prompt'=>'All Schools')),
    ),
    array(
        'header'    =>  '',
        'value'     => 'CHtml::link("Refund", Yii::app()->controller->createUrl("bulk",array("id"=>$data->primaryKey)), array("class" => "btn btn-save", "name" => CHtml::activeName($data,"refunded")))',
        'type'      => 'raw',
        'visible'   => !user()->hasAuth(Group::READ_ONLY, "equal")
    ),
),

)); ?>

// Model

    public function search() {
    // Warning: Please modify the following code to remove attributes that
    // should not be searched.

    $criteria = new CDbCriteria;

    $criteria->with = array('user', 'product');
    $criteria->compare('t.purchase_id', $this->purchase_id);
    $criteria->compare('t.user_id', $this->user_id, true);
    $criteria->compare('t.price', $this->price);
    $criteria->compare('t.form_price', $this->form_name);
    $criteria->compare('t.GUID', $this->GUID, true);
    $criteria->compare('t.refunded', $this->refunded);
    $criteria->compare('user.organisation_id', $this->organisation_id);

    ActiveRecord::addDateRange($criteria, 't.purchase_time', $this->purchase_time, false);
    if (strlen($this->fulfilled) > 0)
        $criteria->addCondition('fulfilled ' . (($this->fulfilled == 1) ? "IS NOT NULL" : "IS NULL"));

    //Related fields filtering
    $criteria->compare('product.product_name', $this->filterProductName, true);
    $criteria->compare('user.username', $this->filterUserName, true);
    $criteria->compare('user.firstname', $this->filterFirstName, true);
    $criteria->compare('user.form_name', $this->filterFormName, true);

    return new CActiveDataProvider($this->currentUserOrganisation(), array(
                'criteria' => $criteria,
                'sort' => array(
                    'defaultOrder' => 't.purchase_time DESC',
                    'attributes' => array(
                        'organisation_name' => array('asc' => 'o.organisation_name', 'desc' => 'o.organisation_name DESC'),
                        'user.username' => array('asc' => 'user.username', 'desc' => 'user.username desc'),
                        'user.form_name' => array('asc' => 'user.form_name', 'desc' => 'user.form_name desc'),
                        'user.firstname' => array('asc' => 'user.firstname', 'desc' => 'user.firstname desc'),
                        'product.product_name' => array('asc' => 'product.product_name', 'desc' => 'product.product_name desc'),
                        '*'
                    )
                )
            ));
}
Was it helpful?

Solution

it was because your fulfilled value is 0, sometimes when you pass the 0 to php. it will treat it as false.

if possible set it like below in your view page

array('no' => 'No', 'yes' => 'Yes')

and then you need change the model as well.

$criteria->addCondition('fulfilled ' . (($this->fulfilled == 'yes') ? "IS NOT NULL" : "IS NULL"));

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top