Dropdown in Yii cGrid - 2 variables - drop-down-menu

I have $model1, $model2 and OwnerID populated in my controller and it renders the data in my zii.widgets.grid.CGridView Problem comes in when I want to send $model2 to the view as well like so:
$this->render('listView', array('model1'=>$model, 'model2' => $model2, 'OwnerID' => 14));
and in the view I have:
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'listKeys',
'dataProvider'=>$model1->search($OwnerID),
'summaryText'=>'Showing {start} to {end} of {count} keys.',
'columns'=>array(
array('value'=>'$data["id"]', 'header'=>'Key ID'),
array(
'type'=>'raw',
'header'=>'Edit',
'value'=>'CHtml::dropDownList("partyList","owner_party_id", $model2)'
),
),
));
I get "Undefined variable: model2" There is data in $model2, if I comment out the dropDown in the grid and have the dropdown list outside the grid like so:
<?php echo CHtml::dropDownList("partyList","owner_party_id", $model2) ?>
then everything works fine. How can I add the dropDownList in CGridView? $model1 is a list of file and $model2 is a list of users.

$model2 needs to be a string or a variable recognized by CGridView.
To get a string representation of the array use var_export()
...
'value'=>'CHtml::dropDownList("partyList","owner_party_id",'.var_export($model2).')',
...
To make $model2 a CGridView variable you'd need to extend CGridView as explained in this Yii wiki page

Related

Symfony2, dynamically refresh form choices

I am creating a simple Blog system with symfony2. Each blog Post is bound to a certain amount of Tags.
Tags can be selected with checkboxes when creating a new blog post. Now I want to be able to dynamically add new tag-checkboxes to the form.
The AJAX part is done and working, I can add new tag names to the Tag entity and append the new checkboxes to the form.
The problem is when I submit the form, symfony2 doesn't recognize the new added tags because they don't belong to the Tag entity yet (at the time the form was generated).
For example: after submitting the form, I dump:
$tags = $form->get('tags')->getData();
The controller ignores the tags that were added through ajax.
I know it has to be solved with events somehow, I already read this documentation: http://symfony.com/doc/current/cookbook/form/dynamic_form_modification.html but can't figure out how to implement it for this specific case.
Here is my form builder for "PostType":
$builder
->add('title')
->add('content', 'textarea')
->add('tags', 'entity', array(
'class' => 'Bundle:Tag',
'property' => 'name',
'multiple' => true,
'expanded' => true,
))
->add('save', 'submit')
;
You can try to use this: Form Collection
Make sure you persist the newly added Tags before submit the form, and the checkboxes has the right names and values.
Names should be like "post[tags][]" and values should be the database ids of the Tag entities.

x-editable drop down from remote not working

I am using X-editable bootstrap version 2
I am using codeigniter . But the text area and text box is working fine for me but drop down is not working .
in my view
< a href="#" id="contract" data-type="select" data-pk="1" data-source="<?php echo base_url('property/contract_get_update')?>" data-title="Select Contract type" class="" >contract data </a>
$( document ).ready(function() {
$('#contract').editable();
});
in my controller
public function contract_get_update()
{
echo "{'M': 'male', 'F': 'female'}";
}
i just tested with this output not working i tried many tricks but it is not seems to be working.
i want to display details from table name contract how can i get that value as a drop down in x-editable
I answered you on gitHub too. The problem is with your json format.
Try this:
$contract_types = array();
$contract_types[] = array('value' => 'M', 'text' => 'male');
$contract_types[] = array('value' => 'F', 'text' => 'female');
echo json_encode($contract_types);
If you are looping through a db query of results try this in your function:
foreach($results AS $result){
$json_response[] = array('value' => $result['id_column'], 'text' => $result['your_column']);
}
echo json_encode($json_response);
You need to use a custom class with text and value members (case sensitive) to enumerate it as a List of type this class then use it to response after ajax call.

Yii and Dropdown in a form

I am new to Yii framework.
I have a form with three fields. I need one of those be a select drop down element that its data comes from previously added data which are in mysql table.
How can I do it ?
If you have a model set up for the table that contains the data you want to use in your dropdown list you can use the CHtml::dropDownList() method the render a dropdown list, and CHtml::listData() to render that model into items for the list, for example;
echo CHtml::dropDownList(
'attribute_name',
'',
CHtml::listData(MyOtherModel::model()->findAll(),'id','name')
);
I use Gii a lot, which uses CActiveForm widget to display forms, if your form uses CActiveForm too you could render your dropdown something like;
$form=$this->beginWidget('CActiveForm', array(
'action'=>Yii::app()->createUrl($this->route),
'method'=>'get',
));
...
echo $form->label($model,'attribute_name');
echo $form->dropDownList(
$model,
'attribute_name',
CHtml::listData(MyOtherModel::model()->findAll(),'id','name')
);
...
$this->endWidget();
Note that CActiveForm uses CHtml::activeDropDownList() rather than CHtml::dropDownList() that I used in my first example, hence the slight difference in syntax between my two examples.

How do I use the ajaxUrl parameter of CGridView in Yii?

I'm trying to use ajaxUrl param of CGridView and it's having no effect.
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'ge-cg-'.$myLib['id'],
'filter'=>$geCGModel,
'dataProvider'=>$dataProvider,
'columns'=>$columns,
'ajaxUrl'=>Yii::app()->createUrl( 'Something/search' ),
));
This doesn't seem to be overriding the ajax url of the CGridView widget. The grid's ajax request is still going to the controller that rendered it (which is different than the grid's own controller).
Thanks!
The ajaxUrl property works only for searches, both the "per column filters" and "advanced search". The sort links and pagination links are generated from the $dataProvider that you specify. You can check the source of CGridView and CDataColumn to see how pagination and sorting is extracted from the dataProvider, respectively.
Hence if you want those links to also use the same ajaxUrl that you want to specify, then you have to set the route property for both the CPagination object, and CSort object of the $dataProvider, somewhat like this:
$dataProvider=new CActiveDataProvider('Modelname',array(
'criteria'=>$criteria,
'pagination'=>array(
'route'=>'something/search'
),
'sort'=>array(
'route'=>'something/search'
)
));
This combined with your current CGridView settings should work as expected.

cakephp ajax remote function select input parameter

I try to do two inputs in cakePHP (category, subcategory).
And if i change category input i want ajax to load values to subcategories.
What i can do it?
Im using remote function like this:
$ajax->remoteFunction(
array(
'url' => array( 'controller' => 'categories', 'action' => 'loadSubcategories', AND NOW I WANT PUT HERE CATEGORY ID FROM MY INPUT ),
'update' => 'subcategories'
)
);
<select name="categories" id="categories" onhange="MY REMOTE FUNCTION">
CATEGORIES
</select>
<select name="subcategories" id="categories" onhange="MY REMOTE FUNCTION">
LOAD SUBCATEGORIES BY CATEGORY ID WITH AJAX
</select>
I hope you can understand me :)
All you need is:
a Cake action that outputs a list of items based on the data it receives (shouldn't be hard)
a bit of JS that figures out which categories are selected (which you already have)
another bit of JS that packages that data and sends it to the Cake action
another bit of JS that updates the site with the list of items you received back
I'd take a look at jQuery's AJAX functions to accomplish this task. If you POST the data in a format like this, it's very easily accessible in $this->data in Cake:
{
'data[ModelName][categories]' : $("#category").val()
}

Resources