konapaz
(Konapaz)
December 3, 2013, 11:16am
1
Hi
Is There easy way to have multiple same type models and show these in multiple cgridview (for each model) ?
For example
$model1 = new MyModel('search');
$model2 = new MyModel('search');
...
...
$this->widget('CGridview', array(
'id' => 'id-1',
'dataProvider' => $model1->search(),
'filter' => $model1,
'columns' => array(...)...)
$this->widget('CGridview', array(
'id' => 'id-2',
'dataProvider' => $model2->search2(),
'filter' => $model2,
'columns' => array(...)...)
The problem is that there is confusing on ajax requests between 2 cgridview, how to solve that?
Thanks!
Keith
(Kburton)
December 3, 2013, 12:03pm
2
Does explicitly setting the ajaxVar property help?
konapaz
(Konapaz)
December 3, 2013, 12:19pm
3
Hi Keith, thanks for your response
Unfortunately it not works…
Both of two Cgridview send back to the server the same form data name variables
I check with network firebug that sends MyModel[column1], MyModel[column2] …
Is there cgridview property that sent alias of this models (for example MyModel1, MyModel2) ?
In this way I could manipulate the POST form data in controller…
How to do that ?
Keith
(Kburton)
December 3, 2013, 1:07pm
4
Okay, here’s the best I can come up with…
In your view, before outputting the first CGridView:
CHtml::setModelNameConverter(function($model){return 'MyModel1'});
Then, in between the two CGridViews:
CHtml::setModelNameConverter(function($model){return 'MyModel2'});
Then, after the second CGridView:
CHtml::setModelNameConverter(null);
You would then need to update the controller to populate the two models using the correct array.
konapaz
(Konapaz)
December 3, 2013, 3:03pm
5
Keith:
Okay, here’s the best I can come up with…
In your view, before outputting the first CGridView:
CHtml::setModelNameConverter(function($model){return 'MyModel1'});
Then, in between the two CGridViews:
CHtml::setModelNameConverter(function($model){return 'MyModel2'});
Then, after the second CGridView:
CHtml::setModelNameConverter(null);
You would then need to update the controller to populate the two models using the correct array.
I can’t test it because I use Yii ver 1.1.13 and this method exists since 1.1.14.
I have compatibilities issues with 1.1.14 in my project.
So I will test it on the next project, In any way thanks!
Keith
(Kburton)
December 3, 2013, 3:11pm
6
Well, a hacky way to do it would be to put something like the following directly after your controller class:
class MyModel2 extends MyModel
{
}
You could use that to differentiate the request data. Not very pleasant though…
What are the incompatibilities you’re experiencing?
konapaz
(Konapaz)
December 4, 2013, 8:08pm
7
Keith:
Well, a hacky way to do it would be to put something like the following directly after your controller class:
class MyModel2 extends MyModel
{
}
You could use that to differentiate the request data. Not very pleasant though…
What are the incompatibilities you’re experiencing?
Although this trick way helps me to solve the problem I avoid it because I try to keep my code simple and readable, so the previous way is the best (and voted I will try to make my project compatible with ver 1.1.14. and I will use your tips
I think also setModelNameConverter added in new version because few user (included me) asked solution for similar issues in the past.
So thanks a lot the Yii developer team