I want to use Ajax through bootstrap button in Yii, and want to pass textfield values though that Ajax call.
here's the button code, where should i put the Ajax code for this button.
<?php $this->widget('bootstrap.widgets.TbButton', array('buttonType'=>'button', 'label'=>'ADD')); ?>
try this
<?php $this->widget('bootstrap.widgets.TbButton', array('buttonType'=>'button', 'label'=>'ADD',
'ajaxOptions' => array(
'type' => 'Post',
'url' => $this->createURL('url'),
'data' => Yii::app()->request->csrfTokenName."=".Yii::app()->request->getCsrfToken()."&action=cancel",
'success'=>"js:function(vals){
}",
)
)); ?>
another eg:
$this->widget('bootstrap.widgets.TbButton', array(
'buttonType' => 'ajaxButton',
'label' => 'Label Here',
'type' => 'danger',
'icon' => 'play white',
...
'ajaxOptions' => array(
'success' => '...',
'error' => '...',
'beforeSend' => '...',
)
));
Related
In my view I have
<?= $this->Form->create('Asc201516', array(
'url' => array(
'controller' => 'asc201516s',
'action' => 'submit_asc201516'
),
'class' => 'form-inline',
'onsubmit' => 'return check_minteacher()'
)); ?>
<div class="form-group col-md-3">
<?= $this->Form->input('bi_school_na', array(
'type' => 'text',
'onkeypress' => 'return isNumberKey(event)',
'label' => 'NA',
'placeholder' => 'NA',
'class' => 'form-control'
)); ?>
</div>
<?php
$options = array(
'label' => 'Submit',
'class' => 'btn btn-primary');
echo $this->Form->end($options);
?>
In my Controller, I have
$this->Asc201516->set($this->request->data['Asc201516']);
if ($this->Asc201516->validates()) {
echo 'it validated logic';
exit();
} else {
$this->redirect(
array(
'controller' => 'asc201516s',
'action' => 'add', $semisid
)
);
}
In my Model, I have
public $validate = array(
'bi_school_na' => array(
'Numeric' => array(
'rule' => 'Numeric',
'required' => true,
'message' => 'numbers only',
'allowEmpty' => false
)
)
);
When I submit the form, logically it should not get submitted and print out the error message but the form gets submitted instead and validates the model inside controller which breaks the operation in controller.
You have to check validation in your controller like
$this->Asc201516->set($this->request->data);
if($this->Asc201516->validates()){
$this->Asc201516->save($this->request->data);
}else{
$this->set("semisid",$semisid);
$this->render("Asc201516s/add");
}
You will have your ID there in variable $semisid, or you can set data in $this->request->data = $this->Asc201516->findById($semisid);
I am trying to display a CGridView in a CJuiDialog but facing some problems.
In my view, I create a menu item as follow :
$this->menu = array(
array('label' => Yii::t('app', 'Afficher les participants ayant fourni cette information'),
'url' => array('Participant/ShowParticipantInfo', 'id' => $model->id_info),
'linkOptions' => array(
//'onclick' => "{viewP(); $('#dialogViewP').dialog('open'); return false}",
'ajax' => array(
'type' => 'POST',
'url' =>"js:$(this).attr('href')", //array('Participant/ShowParticipantInfo', 'id' => $model->id_info),
'update' => '#divForForm2',
),
),
);
Then I've created the dialog :
<?php $this->beginWidget('zii.widgets.jui.CJuiDialog', array(
'id' => 'dialogViewP',
'options' => array(
'title' => 'Liste des participants ayant fourni cette information',
'autoOpen' => false,
'modal' => true,
'width' => 500,
'height' => 300,
),
));
?>
<div id="divForForm2"></div>
<?php $this->endWidget('dialogViewP'); ?>
And my controller looks like this one :
public function actionShowParticipantInfo($id){
$rows = Participant::findParticipantInfo($id);
$result = array();
foreach ($rows AS $key => $val){
$result[] = array('id' => $key +1, 'value' => $val['NomComplet']);
}
$arrayDataProvider = new CArrayDataProvider($result, array(
'id' => 'id',
'pagination' => array(
'pageSize' => 10,
),
));
if(Yii::app()->request->isAjaxRequest){
$this->renderPartial('_showparticipant', array(
'arrayDataProvider' => $arrayDataProvider,
), false, true);
echo CHtml::script('$("#dialogViewP").dialog("open")');
Yii::app()->end();
}
}
But but clicking on the menu item, there is any dialog diplayed. I cannot know why. Can somebody help me ?
I think there should not be both 'url' and 'linkOption' attributes in an menu item. Either one.
Thru web dev tools you need to check if you have ajax XHR upon menu item click.
I'd recommend you this extention if you want to realize an ajax manu.
Even if you initiate ajax request the response cannot activate hidden dialog because on success you ONLY update certain div #divForForm2 but the dialog window is still closed: 'autoOpen' => false. I'd recommend incorporate opening dialog upon ajax success.
'ajax' => array(
'type' => 'POST',
'url' => array('Participant/ShowParticipantInfo', 'id' => $model->id_info),
'success' =>'js:{function(data){$("#divForForm2").html(data); $("#dialogViewP").dialog("open");}'
),
Yii infinite scroll extention: "IasPager pager" not working after ajax update on clistveiw.
It's working fine before ajax call but after ajax call when i update listview it's not working.
$this->widget('zii.widgets.CListView', array(
'id' => 'VideoList',
'dataProvider' => $dataProvider,
'itemView' => '_view',
'template' => '{items} {pager}',
'pager' => array(
'class' => 'ext.infiniteScroll.IasPager',
'rowSelector'=>'.row',
'listViewId' => 'VideoList',
'header' => '',
'loaderText'=>'Loading...',
'options' => array('history' => false, 'triggerPageTreshold' => 2, 'trigger'=>'Load more'),
)
)
);
I have found solution of this, it works fine.
<?php
$this->widget('zii.widgets.CListView', array(
'dataProvider'=>$dataProvider,
'itemView'=>'/products/viewProductList',
'summaryText'=>false,
'emptyText'=>"<p> Coming Soon!!</p>",
'id'=>'ajaxProductListView',
'cssFile'=>Yii::app()->baseUrl.'/css/mycustom.css',
'template' => '{items} {pager}',
'ajaxUpdate'=>true,
'pager' => array(
'class' => 'ext.infiniteScroll.IasPager',
'rowSelector'=>'.ademo',
'listViewId' => 'ajaxProductListView',
'header' => '',
'loaderText'=>'Loading',
'options' => array('history' => false, 'triggerPageTreshold' => 1, 'trigger'=>'Load more'),
),
'afterAjaxUpdate'=>"function(id, data) {
$.ias({
'history': false,
'triggerPageTreshold': 1,
'trigger': 'Load more',
'container': '#ajaxProductListView',
'item': '.ademo',
'pagination': '#ajaxProductListView .pager',
'next': '#ajaxProductListView .next:not(.disabled):not(.hidden) a',
'loader': 'Loading...'
});
}",
));?>
I used from CGridView in framework Yii, I want when I click view button , it is opened in a new window
how can I add of "_new" of target ?
Add 'options' => array('target'=>'_new') to CButtonColumn configuration array in CGridView
array(
'class'=>'zii.widgets.grid.CButtonColumn',
'template' => '{view}',
'buttons'=>array(
'view' => array(
'url' => '', // view url
'options' => array('target' => '_new'),
),
),
),
You can provide the html properties by using 'options' property.
<?php
$this->widget('zii.widgets.grid.CGridView', array(
'dataProvider' => $dataProvider,
'columns' => array(
'table_field_1',
'table_field_2',
'table_field_3',
array(
'class' => 'CButtonColumn',
/* ===== Template to set the buutons. Ex: If you dont want delete link, remove {delete} */
//'template' => '{view} {update} {delete}',
'buttons' => array(
'view' => array(
'options' => array('class' => 'newWindow'),
),
),
),
),
));
?>
But, Opening a new window is browser dependent. With target="_blank" and target="_new" link will be opened in new tab in Mozilla, But in IE you will get new window. So user javascript to generate new window.
<?php
$this->widget('zii.widgets.grid.CGridView', array(
'dataProvider' => $dataProvider,
'columns' => array(
'table_field_1',
'table_field_2',
'table_field_3',
array(
'class' => 'CButtonColumn',
/* ===== Template to set the buutons. Ex: If you dont want delete link, remove {delete} */
//'template' => '{view} {update} {delete}',
'buttons' => array(
'view' => array(
'options' => array('class' => 'newWindow'),
),
),
),
),
));
?>
Keep this jQuery in your .js file
<script>
$(document).ready(function()
{
$(".newWindow").click(function(e)
{
e.preventDefault();
var url=$(this).attr('href');
window.open(url, "_blank", "toolbar=no, scrollbars=yes, resizable=yes, top=100, left=100, width=1020, height=500");
});
});
</script>
You can use this
<?php
$this->widget('zii.widgets.grid.CGridView', array(
'dataProvider' => $dataProvider,
'columns' => array(
'table_field_1',
'table_field_2',
'table_field_3',
array(
'class' => 'CButtonColumn',
/* ===== Template to set the buutons. Ex: If you dont want delete link, remove {delete} */
//'template' => '{view} {update} {delete}',
'buttons' => array(
'view' => array(
'options' => array('target' => '_blank'),
),
),
),
),
));
?>
My application has contacts controller with add action which able to post contacts message to the database table comments. It works fine with its validation.
Now I want to use the add view (The contacts form) as a global form to be rendered in all pages of the application.
I know, to do that, I have to make an element contains the form (or the add view) as follows:
elements/contact.ctp
<div class="panel">
<h4><?php echo __('contact'); ?></h4>
<?php echo $form->create('Contact',array('action'=>'index', 'class' => ''));?>
<?php echo $form->input('name', array('size' => 45, 'class' => 'input-text', 'label' => array( 'text' => __('name',true).'<sup>*</sup>'), 'error' => array('class' => 'error', 'wrap' => 'small')));?>
<?php echo $form->input('email', array('size' => 45, 'class' => 'input-text', 'label' => array('text' => __('email',true).'<sup>*</sup>'), 'error' => array('class' => 'error', 'wrap' => 'small')));?>
<?php echo $form->input('subject', array('type' => 'select', 'options' => array(null => __('choose subject',true), 'g' => __('general', true), 'r' => __('report', true)), 'class' => 'input-text', 'label' => array('text' => __('subject', true).'<sup>*</sup>'),'error' => array('class' => 'error', 'wrap' => 'small'))); ?>
<?php echo $form->input('content', array('class' => 'input-text', 'style' => 'height: 140px', 'title' => __('message', true), 'label' => array('text' => __('message',true).'<sup>*</sup>'), 'error' => array('class' => 'error', 'wrap' => 'small')));?>
<?php //echo $fck->load('Contact.message','Mini'); ?>
<span>
<?php
App::import('Vendor','FoxCaptcha', array('file' => 'Fox_captcha.php'));
$cap = new Fox_captcha(120,30,5);
$cap->image_dir = $html->url('/').'img/';
$cap->lines_amount = 13;
$cap->en_reload = $html->image('reload.png', array('alt' => __('reload', true), 'title' => __('reload the captcha', true), 'id' => 'frel', 'style' => 'vertical-align:middle'));
?>
</span>
<div>
<span class="display:inline"><?php echo $cap->make_it('HTML');?></span>
<?php echo $form->input('vcode', array('size' => 45, 'class' => 'small input-text', 'label' => array('text' => __('captcha', true).'<sup>*</sup>'), 'error' => array('class' => 'error', 'wrap' => 'small')));?>
</div>
<?php echo $form->submit(__('send',true), array('class' => 'nice medium radius white button'));?>
<?php echo $form->end();?>
<div class="alert-box warning"><?php echo __('fields label with * are required');?></div>
</div>
The problem is: When I use this form from any page (for example posts/view/22) it submits to contacts/add. I want after submit posts/view/22 is rendered with any validation message triggered.
There is no solution for this requirement without Ajax and JSON. It includes submitting the form with Ajax to the contacts controller index action with event handler component activated and then check isAjax then render JSON output retrieved by the post's view and then populates the results.