Problem with dataProvider for CGridView

Hi everybody,

I need help from you, please help me.

I’m using a model for many forms, which is one of the forms needs a different parameter than the others. So that I made a different search function for it.

Here is the default dataProvider




...

'dataProvider' => $model->search(),

...



and here is my own dataProvider




...

'dataProvider' => $model->searchUsername(),

...



This different form can load the dataProvider perfectly for the first time it is loaded. But when I tried to filter the data, the dataProvider can’t be load. But when I changed my own dataProvider to the default one,




...

'dataProvider' => $model->searchUsername(),

...


//change to

...

'dataProvider' => $model->search(),

...



nothing went wrong, even when I filter the data.

Can you please help me guys?

Whats wrong with these things I have?

Thank you very much for your help… :)

*nb: I have made the function searchUsername in the model I’ve mentioned above, I just don’t put it here :)

here is some detail on the view




$this->widget('zii.widgets.grid.CGridView', array(

    'dataProvider' => $model->searchUsername(),//can be filter when I use $model->search() instead of $model->searchUsername()

    'filter' => $model,

    'selectableRows' => 1,

    'selectionChanged' => 'CRUD_user',

    'id' => 'gridPegawai',

    'columns' => array(

        array(

            'header' => 'No',

            'type' => 'raw',

            'htmlOptions' => array('style' => 'text-align:center, width:50px'),

            'value' => '$this->grid->dataProvider->pagination->currentPage*$this->grid->dataProvider->pagination->pageSize + $row+1'

        ),

        array(

            'header' => 'Kecamatan',

            'name' => 'ms_kecamatan_id',

            'type' => 'raw',

            'htmlOptions' => array('sytle' => 'text-align:left'),

            'value' => 'CHtml::encode($data->msKecamatan->nama)',

            'filter' => LookupTable::GridFiltering('kecamatan')

        ),

        array(

            'header' => 'Desa',

            'name' => 'ms_desa_id',

            'type' => 'raw',

            'htmlOptions' => array('style' => 'text-align:left'),

            'value' => 'CHtml::encode($data->msDesa->nama)',

            'filter' => LookupTable::GridFiltering('desa', isset($_GET['Jabatan']['ms_kecamatan_id']) ? $_GET['Jabatan']['ms_kecamatan_id'] : '')

        ),

        array(

            'header' => 'Jabatan',

            'name' => 'ms_jabatan_id',

            'type' => 'raw',

            'htmlOptions' => array('style' => 'text-align: left'),

            'value' => 'CHtml::encode($data->msJabatan->nama)',

            'filter' => LookupTable::GridFiltering('jabatan')

        ),

        array(

            'name' => 'nama',

            'type' => 'raw',

            'sortable' => 'true',

            'htmlOptions' => array('style' => 'text-align: left'),

            'value' => 'CHtml::encode($data->msPerangkatDesa->nama)'

        ),

    )

        )

);



the columns Kecamatan and Desa is connected and both are combo box, when I change the Kecamatan it will affect the Desa as well. Both of them just like country and province, provinve and city, and so on…

I see that AJAX working fine here, because when I change the Kecamatan, Desa value is changing as well. But not with the CGridView data.

I think, that’s all information I need to add. Please tell me whether you need anymore information to solve my problem.

Thank you… :)

Could you post the searchUsername(). Post your action too while your at it

Thank you for your respond MrSoundless :)

well, when I put the content of searchUsername() in search(), nothing going wrong. And this searchUsername() can be load perfectly when the page is loaded for the first time / after being refreshed. It just doesn’t work well for filtering.

(like I’ve mentioned above)

here is the code :)




public function searchUsername(){

        $criteria = new CDbCriteria;

        $criteria->compare('t.ms_kecamatan_id', $this->ms_kecamatan_id);

        $criteria->compare('t.ms_desa_id', $this->ms_desa_id);

        $criteria->compare('t.ms_jabatan_id', $this->ms_jabatan_id);

        $criteria->compare('msPerangkatDesa.nama', $this->nama, true);

        $criteria->condition = 'username<>""';


        return new CActiveDataProvider(get_class($this), array(

            'criteria' => $criteria,

        ));

}



Is this enough MrSoundless?

Do you have $model = new MyModel(‘search’); in your action?

yes, I do. And I have tried to change it to




$model = new MyModel('searchUsername'); 

//or

$model = new MyModel();



, but both of them have no effect.

Move condition before compare. See explanation in this post.

/Tommy

I’m so sorry for my late reply, it was because I have to finish my project fast.

And thanks to you tri, I have finished this problem.

Thank you very much for your help… :)

and how did you solve it finally?

i’m sorry cappadochian, i seldomly log on this forum, and i just read your message…

i just move the


$criteria->condition = '[your condition]';

before the


$criteria->compare('your_attribute', $this->your_attribute);

previously




//...

$criteria->compare('your_attribute', $this->your_attribute);

$criteria->compare('your_attribute2', $this->your_attribute2);

$criteria->compare('your_attribute3', $this->your_attribute3);

$criteria->condition = '[your condition]';

//..



to become like




//...

$criteria->condition = '[your condition]';

$criteria->compare('your_attribute', $this->your_attribute);

$criteria->compare('your_attribute2', $this->your_attribute2);

$criteria->compare('your_attribute3', $this->your_attribute3);

//..



that’s all :)