I have a CGridView and I'd like to put a button to refresh.
I've tried adding AjaxButton but I couldn't make it.
The grid ID is session-grid.
I have a partial view with only the CGridView.
I managed to get it working but it's duplicated, the original stays there. I am missing anything?
Thanks a lot!
EDIT: Chosen:
<?php
Yii::app()->clientScript->registerScript('initRefresh',<<<JS
$('#update-grid-button').on('click',function(e) {
e.preventDefault();
$('#session-grid').yiiGridView('update');
});
JS
,CClientScript::POS_READY);
$this->widget('bootstrap.widgets.TbButton', array(
'label'=>'Actualizar',
'type'=>'primary',
'icon'=>'repeat white',
'htmlOptions'=>array(
'id'=>'update-grid-button',
'class'=>'pull-right',
)
));
?>
<button id="refresh-button">Refresh!</button>
<?php Yii::app()->clientScript->registerScript('initRefresh',<<<JS
$('#refresh-button').on('click',function(e) {
e.preventDefault();
$('#session-grid').yiiGridView('update');
});
JS
,CClientScript::POS_READY); ?>
You can use $.fn.yiiGridView.update('session-grid');
Example:
$("#my_button" ).click(function() {
$.fn.yiiGridView.update('session-grid');
});
function update(mygrid)
{
var $form = $("<form action=\"\" data-pjax></form>").appendTo($(mygrid));
$form.submit();
}
Related
I have an update form, and within that form there is a table where I want to populate using ajax after filling in a couple of fields. I have tried using ajaxSubmitButton but somehow it just doesn't trigger the action that I want.
Here is my view:
<?php
echo CHtml::ajaxSubmitButton('Insert', array('myController/insertProgress'), array(
'type' => 'POST',
'success' => 'function(){
alert("success");
}',
'data' => array(
'progress' => 'js:$("#progress").val()',
),
)
);
?>
myController:
public function actionInsertProgress() {
$data = $_POST['progress'];
//do stuff here, including echoing the table row
}
When I click the submit button, it doesn't trigger the insertProgress action, but instead the main form action which is actionEdit. It's as if the URL that I provided is being ignored.
The url for this form goes something like this:
(sitename)/(modulename)/myController/edit/id/57
Thank you.
EDIT: I do have another submit button to update the whole form, which triggers the actionEdit action.
EDIT2: this is what the widget produces:
<script type="text/javascript">
/*<![CDATA[*/
jQuery(function($) {
jQuery('body').on('click','#yt0',function(){jQuery.ajax({'type':'POST','data':{'progress':$("#progress").val()},'url':'http://inarac.id/adm/topikkajian/insertProgress','cache':false});return false;});
});
/*]]>*/
</script>
If you are using module you should add the modele path to your link
$myLink = Yii::app()->getBaseUrl(true) .
'/index.php/moduleName/myController/insertProgress';
<?php
echo CHtml::ajaxSubmitButton('Insert',
$myLink, array(
'type' => 'POST',
'success' => 'js:function(){
alert("success");
}',
'data' => array(
'progress' => 'js:$("#progress").val()',
),
)
);
?>
I am building a Yii application and I followed a tutorial to enable the Advanced search in the index file of a table like this:
<?php
/* #var $this TakeController */
/* #var $dataProvider CActiveDataProvider */
$this->breadcrumbs=array(
'Takes',
);
$this->menu=array(
array('label'=>'Create Take', 'url'=>array('create')),
array('label'=>'Manage Take', 'url'=>array('admin')),
);
Yii::app()->clientScript->registerScript('search', "
$('.search-button').click(function(){
$('.search-form').toggle();
return false;
});
$('.search-form form').submit(function(){
$.fn.yiiListView.update('takelistview', {
data: $(this).serialize()
});
return false;
});
");
?>
<h1>Takes</h1>
<?php echo CHtml::link('Advanced Search','#',array('class'=>'search-button')); ?>
<div class="search-form" style="display:none">
<?php $this->renderPartial('_search',array(
'model'=>$model,
)); ?>
</div>
<?php $this->widget('zii.widgets.CListView', array(
'dataProvider'=>$dataProvider,
'itemView'=>'_view',
'id'=>'takelistview',
'sortableAttributes'=>array('id', 'data'')
)); ?>
I would like to know if there is a way to process the output of the advanced search. I know that with $dataprovider I can access all the records for this table, but I want to access only the filtered ones. In particular, I want to get the attribute "data" for each record in order to enable the download for all filtered records. I took a look at $.fn.yiiListView and CListView but I don't understand what to do. Can you help me? Thanks!
the $dataProvider variable belongs to the CDataProvider class, you can access the data using the getdata Function as below.
$data = $dataProvider->getData($refresh);
Set the $refresh flag to true if you want to reload the data from data source
Refer to full data provider documentation for more dataProvider methods
In Yii, the list view used as a search result.
Controller
public function actionSearch()
{
$key=$_GET['Text'];
$criteria = new CDbCriteria();
$criteria->addSearchCondition('username',$key,true,"OR");
$criteria->select = "`username`,`country`";
$data=new CActiveDataProvider('User',
array('criteria'=>$criteria,'pagination'=>array('pageSize'=>5),
));
$this->render('search', array(
'ModelInstance' => User::model()->findAll($criteria),
'dataProvider'=>$data,
));
}
search.php
<?php
//THE WIDGET WITH ID AND DYNAMICALLY MADE SORTABLEATTRIBUTES PROPERTY
$this->widget('zii.widgets.CListView', array(
'id'=>'user-list',
'dataProvider'=>$dataProvider,
'itemView'=>'results',
'template' => '{sorter}{items}{pager}',
));
?>
<?php echo CHtml::radioButtonList('type','',array(
'1'=>'Personal',
'2'=>'Organization'),array('id'=>'type'),array( 'separator' => "<br/>",'style'=>'display:inline')
);
?>
result.php
<?php echo $data->username."<br>"; ?>
<?php echo $data->country; ?>
The user model fields are id, name , country, type, The search result shows the name and country. Now want to filter the results based on the radio button onchange (personal/organisation).
You could try to use $.fn.yiiListView.update method passing list view's id (user-list in your case) and ajax settings as arguments. data property of ajax settings is what can be used to specify GET-parameters that will be passed to your actionSearch to update the list view. So you have to analyze these parameters in the action and alter CDbCriteria instance depending on them.
The following script to bind onchange handler to your radio button list is to be registered in the view:
Yii::app()->clientScript->registerScript("init-search-radio-button-list", "
$('input[name=\"type\"]').change(function(event) {
var data = {
// your GET-parameters here
}
$.fn.yiiListView.update('user-list', {
'data': data
// another ajax settings if desired
})
});
", CClientScript::POS_READY);
You also may consider the following code as an example based on common technique of filtering CGridView results.
By the way, for performance reasons you can render your view partially in the case of ajax update:
$view = 'search';
$params = array(
'ModelInstance' => User::model()->findAll($criteria),
'dataProvider' => $data
);
if (Yii::app()->request->isAjaxRequest)
$this->renderPartial($view, $params);
else
$this->render($view, $params);
Trying to load a block with a datepicker from a view.. but it loads only the textfield without datepicking feature. What am I doing wrong?
The create view
<?php echo $form->dropDownList($model,'operation_type',CHtml::listData(OperationType::model()->findAll(),
'id','name'),array(
'class'=>'span3',
'empty'=>'----- Type -----',
'id'=>'idOpType',
'ajax'=>array(
'type'=>'POST',
'url'=>CController::createUrl('operations/meta'),
//'dataType'=>'json',
'data'=>array('idOpType'=>'js:this.options[this.selectedIndex].innerHTML'),
'success'=>'function(data){
$("#opTypeBlock").html(data);
}',
),
)); ?>
The Controller Action
public function actionMeta(){
$data= new OperationsMeta();
$this->renderPartial('_meta',array('model'=>$data));
}
The view I am trying to load
<p>Select due date</p>
<?php $this->widget('zii.widgets.jui.CJuiDatePicker',array(
'attribute'=>'param',
'options'=>array(
'showAnim'=>'fold',
),
'model'=>$model,
'htmlOptions'=>array(
'style'=>'height:20px;',
'class'=>'inline',
'id'=>'datepickerOpType',
),
));
?>
Try
$this->renderPartial('_meta',array('model'=>$data), false, true);
so it loads JS files.
Use renderAjax() which injects into the rendering result with JS/CSS scripts and files which are registered with the view, as stated here.
A widgets can or not be on a controller view, and some of widget affecting listView.
I have stalled on a afterAjaxUpdate JS event in CListView. The widget - is a product filter, which updating the list view. My problem is when I want to update my filters after update list view.
Of course I can configure update code in List View, but I think is wrong, because this behaviors belongs to filter widget.
I tried this in widget
$("#' . $this->listViewId . '").yiiListView.settings.afterAjaxUpdate = function(id, data) {
console.log(id, data);
};
But ListView js going below and obviously it's a bad solution.
I thinking about some public widget events, so I can address to listview widget through filter widget and put event there.
Maybe someone has faced with related problems or have better ideas?
Thanks.
Try this way to add afterAjaxUpdate method for your CListView.
<?php
Yii::app()->clientScript->registerScript('handle_ajax_function', "
function addLoadingClass(id,result)
{
$('.list-view-loading').show();
$('li.previous').addClass('disable');
$('li.next').addClass('disable');
}
function removeLoadingClass(id,result)
{
$('li.previous').removeClass('disable');
$('li.next').removeClass('disable');
try{
$('.list-view-loading').hide();
}catch(e){
alert(e);
}
}
");
?>
<?php
$this->widget('zii.widgets.CListView', array(
'id'=>'handle',
//'ajaxUpdate'=>false,
'dataProvider'=>$data,
'beforeAjaxUpdate'=>'addLoadingClass',
'afterAjaxUpdate'=>'removeLoadingClass',
'itemView'=>'myview',
));
?>