posted by aryo tejo w on September 15, 2016

As my promise in the title article CgridView update otomatis saat create, update dan delete pada 1 tampilan  # 1 , I will continue to make a trick on the gridview. In the first article, when uploading files, there are 2 events that take place: upload files in a specific folder and save the file name in a particular table.
Now we will discuss how to delete the uploaded data in the table and at the same time to delete the stored files. And it will directly remove the data in its gridview so it always updates

1.Open file in view as upload form
Modified cgridview
(
             'class' => 'CButtonColumn',  
             'template' => '{delete}',
  )
Be as below:
array( 'class' => 'CButtonColumn',
                'template' => '{open}',
                'buttons' => array(
  'open' => array(
                 'url' => 'Yii::app()->createUrl("fileDiupload/conAktif", array("id"=>$data->id))',
                 'imageUrl' => Yii::app()->request->baseUrl . '/images/delete.png',  )
            )
),
 
2. Make the conActive function in its controller
public function actionConAktif($id)
        {
                        $konfirmasi=1;
                        $model=$this->loadModel($id);
                       
        $this->render('conaktif',array(
                                        'model'=>$model,
                                        'konfirmasi'=>$konfirmasi,
                        ));
        }
3. Make conactive views
<?php echo $this->renderPartial('_conformAktif', array(
                'model'=>$model,
                'konfirmasi'=>$konfirmasi,
                )); ?>
 
4.Make view _conformActive '
<?php
 
          if(isset($konfirmasi))
           {
                           ?>
                            <script>
 
                                          hasil=window.confirm("Yakin hapus file/data ini ?");
                                          if(hasil)
                                                        {
                                                          
                                                        window.location.href="../hapus/" + <?php echo $model->id ?>;
                                                        }
                                                        else
                                                        {
                                                                       
                                                                        window.location.href="../../unggah/unggahfile";
                                                        }
                                        </script>
                            <?php  
            }                             ?>       
 
5. After that go back to the controller to create the delete function
public function actionHapus($id)
        {
                        //hapus item data yg terpilih'
                        $model=$this->loadModel($id);
                        $file = $model->nama_file;
                       
                        $folder =Yii::app()->params['uploadDir'];
                         if( $file != null  &&  file_exists( $folder.'/'.$file ) )
                         {
             unlink(getcwd().'/'.$folder.$file); //hapus file yg terpilih datanya
                                         if ($model->delete())
                                         {
                                        if(!isset($_GET['ajax']))
        $this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('unggah/unggahfile'));
                                         }
                         
                        }
                       
        }


Please follow the steps above, if any bugs learn its errors,

Leave a Comment

Fields with * are required.

Please enter the letters as they are shown in the image above.
Letters are not case-sensitive.