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

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.

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.

Dropdown in Yii cGrid - 2 variables

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

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.

cakephp updating elements

I have an index view which has some elements on it .
index controller code;
$userID = $this->Authsome->get('id');
$qnotes = $this->Qnote->getnotes($userID);
$this->set('qnotes', $qnotes)
$this->render();
elements have been added to the page using
index view code
<?php echo $this->element('lsidebar'); ?>
now the Issue is I also Have an add controller.
add controller code
function add() {
if(!empty($this->data)) {
unset($this->Qnote->Step->validate['qnote_id']);
$this->Qnote->saveAll($this->data);
$this->Session->setFlash('New Note Template has been added.','flash_normal');
}
}
now what I am trying to achieve is once I add a Qnote i want the element('lsidebar') updated
for the new Qnote.
I am Using the Ajax helper. found at http://www.cakephp.bee.pl/
also Here the add qnote View Code :
<?php echo $ajax->submit(
'Submit', array(
'url' => array(
'controller'=>'qnotes',
'action'=>'add')
));
I know its sound like a noob question . can Somebody point me in the right direction atleast.
I have tried everything i could think off. I bet the solution something easy which i didnt think off
help :)
If you want to dynamically update a sidebar with information that is submitted via ajax, there should be a "success" option in your ajax post that would allow you to fire a specific javascript action when the post is finished (or succeeds). You should write a small javascript ajax function to reload the contents of your sidebar when the post succeeds.
See this other stackoverflow answer: CakePHP ajax form submit before and complete will not work for displaying animated gif

How to use ajax on a field widget submit button in Drupal 7 without validating the rest of the form

I'm making a new widget for taxonomy term references where a submit button makes an ajax call back to drupal to alter the form. Here's what I have:
$element['my_module_wrapper']['add'] = array(
'#type' => 'submit',
'#value' => t('Add'),
'#ajax' => array(
'callback' => 'my_module_ajax',
'wrapper' => $field_name . '_my_module_container',
),
);
I have the ajax call working properly, but it validates the whole form, and throws errors when unrelated fields that are required aren't filled out. How can I stop this?
Also possibly related, it doesn't seem to call my hook_field_widget_form() function when ajax is called... is this stemming from the same problem?
Have you tried making your field #type = 'button' instead of 'submit'? As far as I know, a submit button will always run through the for validate array whether it's an ajax form item or not.
Look at the example from Poll module given here: http://api.drupal.org/api/drupal/developer--topics--forms_api_reference.html/7#ajax
The full function shows how it's done with '#limit_validation_errors'.

Resources