Skip to content

Instantly share code, notes, and snippets.

@abassanini
Created May 21, 2015 19:41
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save abassanini/6e369175fb012f777c13 to your computer and use it in GitHub Desktop.
Save abassanini/6e369175fb012f777c13 to your computer and use it in GitHub Desktop.
Detailview with delete button working
<?php
namespace app\controllers;
use Yii;
use app\models\Test;
use app\models\TestSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use yii\base\InvalidCallException;
use yii\helpers\Json;
use yii\helpers\Url;
/**
* TestController implements the CRUD actions for Test model.
*/
class TestController extends Controller
{
public function behaviors()
{
return [
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'delete' => ['post'],
],
],
];
}
/**
* Lists all Test models.
* @return mixed
*/
public function actionIndex()
{
$searchModel = new TestSearch;
$dataProvider = $searchModel->search(Yii::$app->request->getQueryParams());
return $this->render('index', [
'dataProvider' => $dataProvider,
'searchModel' => $searchModel,
]);
}
/**
* Displays a single Test model.
* @param integer $id
* @return mixed
*/
public function actionView($id) {
$model=$this->findModel($id);
if ($model->load(Yii::$app->request->post()) && $model->save()) {
Yii::$app->session->setFlash('kv-detail-success', 'Saved record successfully');
return $this->redirect(['view', 'id'=>$model->id]);
} else {
return $this->render('view', ['model'=>$model]);
}
}
public function actionView_from_gii($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]);
}
}
/**
* Creates a new Test model.
* If creation is successful, the browser will be redirected to the 'view' page.
* @return mixed
*/
public function actionCreate()
{
$model = new Test;
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['view', 'id' => $model->id]);
} else {
return $this->render('create', [
'model' => $model,
]);
}
}
/**
* Updates an existing Test model.
* If update is successful, the browser will be redirected to the 'view' page.
* @param integer $id
* @return mixed
*/
public function actionUpdate($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('update', [
'model' => $model,
]);
}
}
/**
* Deletes an existing Test model.
* If deletion is successful, the browser will be redirected to the 'index' page.
* @param integer $id
* @return mixed
*/
public function actionDelete($id) {
$post = Yii::$app->request->post();
Yii::trace('Entering delete action');
if (Yii::$app->request->isAjax && isset($post['custom_param'])) {
if ($this->findModel($id)->delete()) {
echo Json::encode([
'success' => true,
'messages' => [
'kv-detail-info' => 'The record # ' . $id . ' was successfully deleted. <a href="' .
Url::to(['/test']) . '" 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 record # ' . $id . '.'
]
]);
}
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.");
}
/**
* Finds the Test model based on its primary key value.
* If the model is not found, a 404 HTTP exception will be thrown.
* @param integer $id
* @return Test the loaded model
* @throws NotFoundHttpException if the model cannot be found
*/
protected function findModel($id)
{
if (($model = Test::findOne($id)) !== null) {
return $model;
} else {
throw new NotFoundHttpException('The requested page does not exist.');
}
}
}
<?php
use yii\helpers\Html;
use kartik\grid\GridView;
use yii\widgets\Pjax;
/**
* @var yii\web\View $this
* @var yii\data\ActiveDataProvider $dataProvider
* @var app\models\TestSearch $searchModel
*/
$this->title = 'Tests';
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="test-index">
<div class="page-header">
<h1><?= Html::encode($this->title) ?></h1>
</div>
<?php
<?php Pjax::begin(); echo GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'id',
'name',
[
'class' => 'yii\grid\ActionColumn',
'buttons' => [
'update' => function ($url, $model) {
return Html::a('<span class="glyphicon glyphicon-pencil"></span>', Yii::$app->urlManager->createUrl(['test/view','id' => $model->id,'edit'=>'t']), [
'title' => Yii::t('yii', 'Edit'),
]);}
],
],
],
'responsive'=>true,
'hover'=>true,
'condensed'=>true,
'floatHeader'=>true,
'panel' => [
'heading'=>'<h3 class="panel-title"><i class="glyphicon glyphicon-th-list"></i> '.Html::encode($this->title).' </h3>',
'type'=>'info',
'before'=>Html::a('<i class="glyphicon glyphicon-plus"></i> Add', ['create'], ['class' => 'btn btn-success']), 'after'=>Html::a('<i class="glyphicon glyphicon-repeat"></i> Reset List', ['index'], ['class' => 'btn btn-info']),
'showFooter'=>false
],
]); Pjax::end(); ?>
</div>
<?php
namespace app\models;
use Yii;
/**
* This is the model class for table "Test".
*
* @property integer $id
* @property string $name
*/
class Test extends \yii\db\ActiveRecord
{
/**
* @inheritdoc
*/
public static function tableName()
{
return 'Test';
}
/**
* @inheritdoc
*/
public function rules()
{
return [
[['name'], 'string', 'max' => 20]
];
}
/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
'id' => 'ID',
'name' => 'Name',
];
}
}
CREATE TABLE `Test`
(
id int(10) UNSIGNED,
name varchar(20)
)
<?php
use yii\helpers\Html;
use kartik\detail\DetailView;
use yii\helpers\Url;
/**
* @var yii\web\View $this
* @var app\models\Test $model
*/
$this->title = $model->name;
$this->params['breadcrumbs'][] = ['label' => 'Tests', 'url' => ['index']];
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="test-view">
<div class="page-header">
<h1><?= Html::encode($this->title) ?></h1>
</div>
<?= 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'=>[
'params' => ['custom_param'=>true],
'url'=>['delete', 'id' => $model->id],
],
]) ?>
</div>
@eothein
Copy link

eothein commented May 25, 2015

Hey Abassanini

Thanks for the heads up!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment