Last active
September 18, 2015 10:04
-
-
Save maximishchenko/30205c4e801ca9d3ca25 to your computer and use it in GitHub Desktop.
Yii Multiple Delete Records with delete files
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// There and next: | |
// ids - the id's of GridView's checkbox column | |
// <grid-id> - the id's of GridView | |
// View code | |
$this->widget('bootstrap.widgets.TbButton', array( | |
'label'=>'Удалить', | |
'type'=>'danger', | |
'buttonType'=>'ajaxButton', | |
'url'=>Yii::app()->createUrl('/<modulename>/<controllername>/massdelete'), | |
'ajaxOptions'=>array( | |
// Send data by $_POST method | |
'type'=>'POST', | |
// Send id's of checked records | |
"data" => "js:{ids:$.fn.yiiGridView.getSelection('<grid-id>')}", | |
// <div> for update | |
'update'=>'#output', | |
// After success ajax-request reload GridView | |
'success' => "js:function() { $.fn.yiiGridView.update('<grid-id>') }" | |
), | |
'htmlOptions' => array( | |
// If nothing checked echo js.alert with message "Nothing Checked!" | |
'onclick' => ' | |
var id_list_lock=$.fn.yiiGridView.getSelection("<grid-id>","ids"); | |
if (id_list_lock=="") { | |
alert("Нет отмеченных записей!"); | |
}', | |
// js.alert confirmation delete dialog | |
'confirm' => 'Удалить выбранные записи?', | |
), | |
'type'=>null, | |
'size'=>'small', | |
)); | |
// if using Bootstrap extension | |
$count=$this->widget('bootstrap.widgets.TbBadge', array( | |
'type'=>'warning', | |
'label'=>'{count}', | |
), true); | |
$this->widget('bootstrap.widgets.TbGridView',array( | |
'id'=>'<grid-id>', | |
'dataProvider'=>$model->search(), | |
'filter'=>false, | |
'selectableRows' => 2, | |
// Also if using Bootstrap extension | |
'summaryText' => 'Всего записей: '.$count.' ', | |
// Text that displayed when request return null | |
'emptyText' => 'Нет данных для отображения по данному запросу', | |
// css-file for GridView style | |
'cssFile'=>Yii::app()->baseUrl . "/css/<grid-id>.css", | |
'columns'=>array( | |
// ... | |
array( | |
'class'=>'CCheckBoxColumn', | |
// id's of GridView | |
'id'=>'ids', | |
), | |
// ... | |
), | |
)); | |
// Controller Code | |
// Access to massdelete action | |
public function accessRules() | |
{ | |
return array( | |
// ... | |
array('allow', | |
'actions'=>array('massdelete'), | |
// users or roles for access to this action | |
'users'=>array('@'), | |
), | |
// ... | |
); | |
} | |
public function actionMassdelete() | |
{ | |
if(Yii::app()->request->isAjaxRequest) | |
{ | |
if(isset($_POST['ids'])) | |
{ | |
$idx = $_POST['ids']; | |
$model = new Company; | |
$files = Company::model()->findAllByPk($idx); | |
foreach ($files as $data) { | |
// echo $data->company_logo .'<br />'; | |
@unlink(Yii::getPathOfAlias('webroot.images') . "/upload/brand_logo/" . $data->company_logo); | |
} | |
foreach ($idx as $ids) { | |
$model->deleteByPk($ids); | |
} | |
$this->actionAdmin(); | |
} | |
// else | |
// { | |
// throw new CHttpException(404,'Запрашиваемая страница не существует.'); | |
// } | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment