sergmoro1 / yii2-modal-crud
在模态窗口中添加和更新模型。
v1.0.0
2019-08-31 04:47 UTC
Requires
- php: >=5.4.0
- yiisoft/yii2: *
This package is auto-updated.
Last update: 2024-08-29 04:54:23 UTC
README
安装
安装此扩展的首选方式是通过composer。
运行以下命令之一:
composer require --prefer-dist sergmoro1/yii2-modal-crud
或者将以下内容添加到您的composer.json文件中的require部分:
"sergmoro1/yii2-modal-crud": "^1.0"
。
使用方法
例如,有一个名为Property
的模型,包含两个字段:id
和name
。
###控制器
namespace backend\controllers; use sergmoro1\modal\controllers\ModalController; use common\models\Property; use common\models\PropertySearch; class PropertyController extends ModalController { public function newModel() { return new Property(); } public function newSearch() { return new PropertySearch(); } }
###视图
只需关注index.php
。其他都是普通的。
<?php use Yii; use yii\helpers\Html; use yii\grid\GridView; use yii\bootstrap\Modal; // bind actions $this->registerJs('var popUp = {"id": "property", "actions": ["update"]};', yii\web\View::POS_HEAD); sergmoro1\modal\assets\PopUpAsset::register($this); $this->title = Yii::t('app', 'Properties'); echo Modal::widget([ 'id' => 'property-win', 'toggleButton' => false, 'header' => $this->title, 'footer' => '<button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>'. '<button type="button" class="btn btn-primary">Save</button>', ]); ?> <div class="property-index"> // create action <p> <?= Html::a('glyphicon glyphicon-plus', ['create'], [ 'id' => 'property-add', 'data-toggle' => 'modal', 'data-target' => '#property-win', 'class' => 'btn btn-success', ]) ?> </p> <div class="table-responsive"> <?= GridView::widget([ 'dataProvider' => $dataProvider, 'filterModel' => $searchModel, 'columns' => [ 'id' 'name', // update, delete actions [ 'class' => 'yii\grid\ActionColumn', 'template' => '{update} {delete}', 'buttons' => [ 'update' => function ($url, $model) { return Html::a( '<span class="glyphicon glyphicon-pencil"></span>', $url, [ 'class' => 'update', 'data-toggle' => 'modal', 'data-target' => '#property-win', ] ); }, ], ], ], ]); ?> </div> </div>
完整代码可在sergmoro1/yii2-lookup找到。