Update CDataProvider (solved)

I have a CGridView, displaying data from a custom DataProvider.

Then, in a javascript function, I change the condition of the DataProvider and

then do $.fn.yiiGridView.update(‘gridview’).

But, the gridview simply re-displays the same data, meaning that it does not update/refresh the dataprovider first.

Any ideas?

Should the dataprovider be in the controller? Currently I have it in the view.

Can you post your code. It is not clear what you are doing

The top gridview displayes the parent model’s records.

The bottom gridview displayes the child records - obtained via the dataprovider(targeting the child model and using the child model’s many-many relation). This part works fine.

If the user clicks a record in the parent gridview, it activates the ParentRecordClick javascript function, which:

  1. gets the new parentRecordId in the top gridview (to be used as condition by the dataprovider) and;

  2. updates the bottom gridview.

So I need the dataprovider to be refreshed before the bottom gridview is updated - but it does not happen.

<?php

&#036;this-&gt;widget('zii.widgets.grid.CGridView', array( 


'id'=&gt;'parent-grid', 


'dataProvider'=&gt;&#036;parent_model-&gt;search(), 


'filter'=&gt;&#036;parent_model, 


'columns'=&gt;array(


	'role_id',


	'role_desc',


	'system_record',


	array(


		'class'=&gt;'CButtonColumn',


	),


),


'selectionChanged'=&gt;'ParentRecordClick',


)); 





&#036;parentRecordId=1; // This must still be changed to get the Id of the first record in the parent gridview

?>

<?php

&#036;dataProvider = new CActiveDataProvider('Permission'/* related model */, array( 


	'criteria'=&gt;array( 


		'with'=&gt;array('roleRelation' /* many-many relation-name in related model */ =&gt;array( 


			'together'=&gt;'true', 


			'alias'=&gt;'rolePermissions' /* name for the combined file*/)),


			'condition'=&gt;&quot;rolePermissions.role_id=&#036;parentRecordId&quot;,


	), 


));


&#036;this-&gt;widget('zii.widgets.grid.CGridView', array(


	'id'=&gt;'permissions-grid',


	'dataProvider'=&gt;&#036;dataProvider,


	'columns'=&gt;array(


		'permission_id',


		'authcontroller_id',


		'authaction_id',


		'system_record',


		array(


			'class'=&gt;'CButtonColumn',


			'viewButtonUrl' =&gt; 'array(&quot;permission/view&quot;, &quot;id&quot;=&gt;&#036;data-&gt;permission_id)',


			'updateButtonUrl' =&gt; 'array(&quot;permission/update&quot;, &quot;id&quot;=&gt;&#036;data-&gt;permission_id)', 


			'deleteButtonUrl' =&gt; 'array(&quot;permission/delete&quot;, &quot;id&quot;=&gt;&#036;data-&gt;permission_id)',


		),


	),


));	

?>

<script type="text/javascript">

function ParentRecordClick(target_id) { 


	&#036;parentRecordId=&#036;.fn.yiiGridView.getSelection(target_id);


	&#036;.fn.yiiGridView.update('permissions-grid');


}

</script>

For those interested, there is a working example here:

http://www.yiiframework.com/wiki/323/dynamic-parent-and-child-cgridciew-on-single-view-using-ajax-to-update-child-gridview-via-controller-with-many_many-relation-after-row-in-parent-gridview-was-clicked/