[color="#0000FF"]Example to delete selected please?
[/color]
DataGrid: 2554
[color="#0000FF"]Thank you![/color]
[color="#0000FF"]Example to delete selected please?
[/color]
DataGrid: 2554
[color="#0000FF"]Thank you![/color]
please describe in detail?
example of function to delete the selected records?
Function to [color="#FF0000"]<?php echo CHtml::ajaxSubmitButton(‘Delete Item(s)’, CController::createUrl(‘example/DeleteAjax’)); ?>[/color]
<?php echo CHtml::ajaxSubmitButton('Delete Item(s)', CController::createUrl('example/DeleteAjax')); ?>
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'usuario-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
array(
'name' => 'Id',
'class' => 'CCheckBoxColumn',
'selectableRows' => '2',
// 'checked' => 'false',
'header'=>'#',
),
'Id',
'IdPessoa',
'Usuario',
'Senha',
'IdGrupo',
'Status',
/*
'UltimoAcesso',
'DataValidade',
'DataCadastro',
*/
array(
'class'=>'CButtonColumn',
),
),
)); ?>
i guess you want do a batch delete? following just give you the idea (but do not guarantee it 's right ! ):
try : var selectionIds=$.fn.yiiGridView.getSelection(‘you-grid-id’);
then use jquery ajax do that ,
$.post('<?php $this->createUrl('batchDelete'); ?>',{ids:selectionIds},function(response){
// here use $.parseJSON(); convert the response to validate js json object
// or you can use $.post(url ,data,callback,'json');
if(response.success){
$.fn.yiiGridView.update('you-grid-id'); //ajax update the content of gridview
}else{
// ..
}
});
in your controller:
MyController::actionBatchDelete(){
// rights check ! ajax request check !
if(isset($_POST['ids'])){
$ids = $_POST['ids'];
$result = true;
foreach($ids as $id){
$model = $this->laodModel($id);
if($model->delete()){
$result = $result && true;
}else{
$result = $result && false;
}
}
}
echo CJSON::encode(array('success'=>$result));
}
In view/admin
<?php echo CHtml::button('Delete',array('class'=>'deleteSelected-button'));?>
Yii::app()->clientScript->registerScript('Delete',"
$('.deleteSelected-button').click(function(){
// get the ids
var ids = $.fn.yiiGridView.getSelection('user-profile-grid');
if('' == ids)
{
alert('Please select at least one record to delete');
return false;
}
else if(window.confirm('Are you sure you want to delete selected item(s)?'))
{
// we have array, lets split them into a string separating
// values with commas
ids = 'ids='+ids.join(',');
// now just call the ajax
$.ajax({
url: '".Yii::app()->createUrl('/admin/UserProfile/DeleteSelected/')."',
data: ids,
success: function(data){
$.fn.yiiGridView.update('user-profile-grid', {
data: $(this).serialize()
});
},
error: function(){
// what i do on error=?
}});
}
return false; // if you want to avoid default button action
});",CClientScript::POS_READY);
// Change gridview id and controller action as necessary
In Controller
public function actionDeleteSelected($ids)
{
$del_ids = explode(',', $ids);
foreach ($del_ids as $id)
{
$model = $this->loadModel($id);
if ($model->delete())
{
// success
}
else
{
// fail
}
}
}
// Thats it
Aruna,
The control did not receive.
View = OK!
Controller = No action.
my code:VIEW
<?php echo CHtml::button('Apagar selecionados',array('class'=>'deleteSelected-button'));?>
<?php
Yii::app()->clientScript->registerScript('Delete',"
$('.deleteSelected-button').click(function(){
// get the ids
var ids = $.fn.yiiGridView.getSelection('usuario-grid');
if('' == ids)
{
alert('Por favor, selecione pelo menos um registro para apagar');
return false;
}
else if(window.confirm('Tem certeza de que deseja excluir o(s) registo(s) selecionado(s)?'))
{
// we have array, lets split them into a string separating
// values with commas
ids = 'ids='+ids.join(',');
// now just call the ajax
$.ajax({
url: '".Yii::app()->createUrl('/admin/usuario/DeleteSelected/')."',
data: ids,
success: function(data){
$.fn.yiiGridView.update('usuario-grid', {
data: $(this).serialize()
});
},
error: function(){
// what i do on error=?
}});
}
return false; // if you want to avoid default button action
});",CClientScript::POS_READY);
$pageSize=Yii::app()->user->getState('pageSize',Yii::app()->params['defaultPageSize']=10);
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'usuario-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'selectableRows' => '2',
/* 'pager'=>array(
'header'=>'Paginas: ',
'firstPageLabel'=>'Primeiro',
'prevPageLabel'=>'Anterior',
'nextPageLabel'=>'Proximo',
'lastPageLabel'=>'Ultimo',
), */
'columns'=>array(
array(
'class' => 'CCheckBoxColumn',
'name' => 'Id',
'value'=>'$data->Id',
'id' => 'Id',
// 'selectableRows' => '2',
// 'checked' => 'false',
// 'header'=>'#',
),
'Id',
'IdPessoa',
'Usuario',
'Senha',
'IdGrupo',
'Status',
array(
//'header'=>'Ult. Acesso',
'name' => 'UltimoAcesso',
'value' => 'date("d/m/Y",strtotime($data->UltimoAcesso))',
'headerHtmlOptions'=>array('width'=>'75px'),
),
/*
'DataValidade',
'DataCadastro',
*/
array(
'class'=>'CButtonColumn',
'header'=>'Listar'.CHtml::dropDownList('pageSize',$pageSize,array(10=>10,20=>20,50=>50,100=>100,
150=>150,300=>300,500=>500, 999999=>'Todos'),array(
'onchange'=>"$.fn.yiiGridView.update('usuario-grid',{ data:{pageSize: $(this).val() }})",
)),
),
),
));
?>
my code:CONTROLLER
public function actionDeleteSelected($ids)
{
$del_ids = explode(',', $ids);
foreach ($del_ids as $id)
{
$model = $this->loadModel($id);
if ($model->delete())
{
// success
Alert("ok");
}
else
{
Alert("off");
// fail
}
}
}
Hi,
Remove admin part from url, because my one is inside a module. Try with below
‘/usuario/DeleteSelected/’
Thanks
Aruna
Aruna, yiqing95,
Thank!
Perfect!