Button delete selected?

:blink:

[color="#0000FF"]Example to delete selected please?

[/color]

DataGrid: 2554

DataGrid.png

[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? :lol: following just give you the idea (but do not guarantee it 's right ! :D ):

try : var selectionIds=$.fn.yiiGridView.getSelection(‘you-grid-id’);

then use jquery ajax do that ,

    &#036;.post('&lt;?php &#036;this-&gt;createUrl('batchDelete'); ?&gt;',{ids:selectionIds},function(response){


                  // here use &#036;.parseJSON();  convert the response to validate js json object


                   // or  you can  use &#036;.post(url ,data,callback,'json'); 





           if(response.success){


               &#036;.fn.yiiGridView.update('you-grid-id'); //ajax update the content of gridview 





           }else{


                   //  ..


           }


  });

in your controller:

 MyController::actionBatchDelete(){





      //   rights check &#33; ajax request check &#33;


       if(isset(&#036;_POST['ids'])){


              &#036;ids = &#036;_POST['ids'];


                  &#036;result = true;


              foreach(&#036;ids as &#036;id){


             &#036;model = &#036;this-&gt;laodModel(&#036;id);


                     if(&#036;model-&gt;delete()){


                           &#036;result = &#036;result &amp;&amp; true;


                    }else{


                           &#036;result = &#036;result &amp;&amp; false;


                         }


               }


       }


          echo CJSON::encode(array('success'=&gt;&#036;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! B)