Yii Framework Forum: Kartik's DetailView not deleting - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Kartik's DetailView not deleting Rate Topic: -----

#1 User is offline   abassanini 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 5
  • Joined: 21-February 15
  • Location:Ecuador

Posted 19 May 2015 - 11:23 PM

Hello

I'm trying to use DetailView from Kartik, but using the delete icon/button doesn't work.

Here is what I'm trying:
Controller
class TestController extends Controller
{
    public function actionView($id)
    {
        $model = $this->findModel($id);
 
        if ($model->load(Yii::$app->request->post()) && $model->save()) {
			return $this->redirect(['view', 'id' => $model->id]);
        } else {
        	return $this->render('view', ['model' => $model]);
		}
    }
 
    public function actionDelete($id)
    {
        $this->findModel($id)->delete();
        return $this->redirect(['index']);
    }
}


View
use kartik\detail\DetailView;
use yii\helpers\Url;
 
    echo DetailView::widget([
            'model' => $model,
            'condensed'=>false,
            'hover'=>true,
            'mode'=>Yii::$app->request->get('edit')=='t' ? DetailView::MODE_EDIT : DetailView::MODE_VIEW,
            'panel'=>[
            'heading'=>$this->title,
            'type'=>DetailView::TYPE_INFO,
        ],
        'attributes' => [
            'id',
            'name',
        ],
        
    	'deleteOptions'=>[
        'url'=>['delete', 'id' => $model->id],
        'data'=>[
        	'confirm'=>Yii::t('app', 'Are you sure you want to delete this item?'),
        	'method'=>'post',
        ],
        ], 
        'enableEditMode'=>true,
    ])


Using firefox inspector I've noticed that when I click the delete button, it produces a POST request to view, not to the delete action (actionDelete) specified in the url param in deleteOptions.

I'm using Yii Version 2.0.4 and the instructions from Krajee Webtips (cannot insert link because the forum marks the post as spam :huh: ) but doesn't work.


Any clues?
--
Saludos - Regards,
Aldo

Linux User # 136068
0

#2 User is offline   abassanini 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 5
  • Joined: 21-February 15
  • Location:Ecuador

Posted 21 May 2015 - 11:47 AM

I finally made it works!

Problem #1: Kartik's Detailview version

Checked the vendor/kartik-v/yii2-detail-view/README.md
I was using 1.7.0. Apparently it had a bug that with the delete button. It didn't work
Solution: upgrade to 1.7.1. Complete instructions at GitHub:

Add to the require section of your composer.json file:
"kartik-v/yii2-detail-view": "dev-master"


And then run:
php composer.phar update


Problem #2: Webtip omissions

Using Krajee Webtips tutorial, I found this issues:
In the controller's view section, change the 'deleteOptions' to:

    	'deleteOptions'=>[
			'params' => ['custom_param'=>true],
        	         'url'=>['delete', 'id' => $model->id],
    	],


Add Json to the use section:

use yii\helpers\Json;


I didn't notice this error, because the delete action was called via ajax, so I wasn't receiving the exception page error. I used the debugger to find it out.

With that, it worked. All the rest of the webtip was correct, except that the delete icon didn't work from the index page/view
For that issue, add this to the else section of the delete action:

public function actionDelete($id) {
    	$post = Yii::$app->request->post();

    	if (Yii::$app->request->isAjax && isset($post['custom_param'])) {
    		if ($this->findModel($id)->delete()) {
    			...
    		} else {
    			...
    		}
    		return;
    	} elseif (Yii::$app->request->post()) {
    		$this->findModel($id)->delete();
    		return $this->redirect(['index']);
    	}
    	throw new InvalidCallException("You are not allowed to do this operation. Contact the administrator.");
    }


The complete code, posted on Gist

Hopes this help to anyone
--
Saludos - Regards,
Aldo

Linux User # 136068
0

#3 User is offline   eothein 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 15
  • Joined: 02-January 15

Posted 25 May 2015 - 04:19 PM

Hello Abassani

Thanks for the great update which is indeed necessary in order to get the thing working. I'm still having a problem though: when I press the delete button, it deletes the model (which is fine) but I get a red flash message stating "Internal Server Error".

I'm not sure why this is. Debugging only reveals that the Controller stops at the echo part of the code, but nothing else...

Any suggestions?

This is my code:
public function actionDelete() {
        $post = Yii::$app->request->post();
        Yii::trace('Entering delete action');
        if (Yii::$app->request->isAjax && isset($post['klantdelete'])) {
            $id = $post['id'];
            if ($this->findModel($id)->delete()) {
               
                echo Json::encode([
                    'success' => true,
                    'messages' => [
                        'kv-detail-info' => 'The book # ' . $id . ' was successfully deleted. <a href="' . 
                            Url::to(['/klant/view']) . '" class="btn btn-sm btn-info">' .
                            '<i class="glyphicon glyphicon-hand-right"></i>  Click here</a> to proceed.'
                    ]
                ]);
            } else {
                echo Json::encode([
                    'success' => false,
                    'messages' => [
                        'kv-detail-error' => 'Cannot delete the klant # ' . $id . '.'
                    ]
                ]);
            }
            return;
        } else {
            $request = Yii::$app->request;
            $id = $request->get('klant_id');
            $this->findModel($id)->delete();
            return $this->redirect(Url::previous());
        }
        throw new InvalidCallException("You are not allowed to do this operation. Contact the administrator.");
    }


and the view
    echo DetailView::widget([
        'model' => $model,
        'attributes' => $attributes,
        'mode' => 'view',
        'panel' => [
            'heading' => 'Klantgegevens voor ' . $model->klant_naam,
            'type' => DetailView::TYPE_INFO,
        ],
        'deleteOptions' => [ // your ajax delete parameters
            'params' => ['id' => $model->klant_id, 'klantdelete' => true],
            'url' => ['delete','id' => $model->klant_id]
        ],
        'alertMessageSettings' => [
            
        ],
        'bordered' => $bordered,
        'striped' => $striped,
        'condensed' => $condensed,
        'responsive' => $responsive,
    ]);
    ?>

0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users