police666 / yii2-modal-ajax
适用于 bootstrap 4 的 Modal-ajax
1.0
2018-09-04 15:27 UTC
Requires
- yiisoft/yii2: ~2.0.0
This package is not auto-updated.
Last update: 2024-09-20 02:17:09 UTC
README
这是一个围绕 Yii2 Bootstrap Modal 的包装器,用于通过 AJAX 在其中使用 ActiveForm。
安装
安装此扩展的首选方式是通过 composer。
运行以下命令
$ php composer.phar require police666/yii2-modal-ajax:dev-master
或
"police666/yii2-modal-ajax": "dev-master"
将以下内容添加到您的 composer.json 文件的 require 部分中。
使用方法
控制器
通过添加 AJAX 渲染和保存功能扩展您的基本逻辑
public function actionCreate() { $model = new Company(); if ($model->load(Yii::$app->request->post())) { if ($model->save()) { return $this->redirect(['view', 'id' => $model->id]); } } return $this->render('create', [ 'model' => $model, ]); }
到
public function actionCreate() { $model = new Company(); if ($model->load(Yii::$app->request->post())) { if ($model->save()) { if (Yii::$app->request->isAjax) { // JSON response is expected in case of successful save Yii::$app->response->format = \yii\web\Response::FORMAT_JSON; return ['success' => true]; } return $this->redirect(['view', 'id' => $model->id]); } } if (Yii::$app->request->isAjax) { return $this->renderAjax('create', [ 'model' => $model, ]); } else { return $this->render('create', [ 'model' => $model, ]); } }
视图
use police\yii2\ModalAjax; echo ModalAjax::widget([ 'id' => 'createCompany', 'title' => 'Create Company', 'toggleButton' => [ 'label' => 'New Company', 'class' => 'btn btn-primary pull-right' ], 'url' => Url::to(['/partner/default/create']), // Ajax view with form to load 'ajaxSubmit' => true, // Submit the contained form as ajax, true by default // ... any other yii2 bootstrap modal option you need ]);
网格中的使用
索引视图 - 创建(单个 Modal 模式)
use police\yii2\ModalAjax; echo ModalAjax::widget([ 'id' => 'createCompany', 'title' => 'Create Company', 'toggleButton' => [ 'label' => 'New Company', 'class' => 'btn btn-primary pull-right' ], 'url' => Url::to(['/partner/default/create']), // Ajax view with form to load 'ajaxSubmit' => true, // Submit the contained form as ajax, true by default 'size' => ModalAjax::SIZE_LARGE, 'options' => ['class' => 'header-primary'], 'autoClose' => true, 'pjaxContainer' => '#grid-company-pjax', ]);
索引视图 - 更新(多 Modal 模式)
带有事件的 Modal Ajax
use police\yii2\ModalAjax; echo ModalAjax::widget([ 'id' => 'updateCompany', 'selector' => 'a.btn', // all buttons in grid view with href attribute 'options' => ['class' => 'header-primary'], 'pjaxContainer' => '#grid-company-pjax', 'events'=>[ ModalAjax::EVENT_MODAL_SHOW => new \yii\web\JsExpression(" function(event, data, status, xhr, selector) { selector.addClass('warning'); } "), ModalAjax::EVENT_MODAL_SUBMIT => new \yii\web\JsExpression(" function(event, data, status, xhr, selector) { if(status){ if(selector.data('scenario') == 'hello'){ alert('hello'); } else { alert('goodbye'); } $(this).modal('toggle'); } } ") ] ]); //Grid example with data-scenario Pjax::begin([ 'id' => 'grid-company-pjax', 'timeout' => 5000, ]); echo GridView::widget([ 'dataProvider' => $dataProvider, 'columns' => [ ...................... // Action buttons // <a class="btn" href="/site/update?id=10" title="Edit ID-10" data-scenario="hello">Hello</a> // <a class="btn" href="/site/update?id=20" title="Edit ID-20" data-scenario="goodbye">Goodbye</a> ...................... ], ]); Pjax::end();
插件事件
在基本的 Twitter Bootstrap modal 事件之上,还会触发以下事件
kbModalBeforeShow (ModalAjax::EVENT_BEFORE_SHOW)
此事件在加载表单视图之前触发。此事件还可用以下附加参数
xhr: XMLHttpRequest,用于此事务的 jQuery XML Http Request 对象。settings: object,此事务的 jQuery AJAX 设置。
$('#createCompany').on('kbModalBeforeShow', function(event, xhr, settings) { console.log('kbModalBeforeShow'); });
kbModalShow (ModalAjax::EVENT_MODAL_SHOW)
在成功加载表单视图后触发此事件。此事件还可用以下附加参数
data: object,通过服务器响应发送的数据对象。status: string,jQuery AJAX 成功的文本状态。xhr: XMLHttpRequest,用于此事务的 jQuery XML Http Request 对象。selector: object,多 Modal 提交后嵌入逻辑的 jQuery 选择器。
$('#createCompany').on('kbModalShow', function(event, data, status, xhr, selector) { console.log('kbModalShow'); });
kbModalBeforeSubmit (ModalAjax::EVENT_BEFORE_SUBMIT)
在表单提交之前触发此事件。此事件还可用以下附加参数
xhr: XMLHttpRequest,用于此事务的 jQuery XML Http Request 对象。settings: object,此事务的 jQuery AJAX 设置。
$('#createCompany').on('kbModalBeforeSubmit', function(event, xhr, settings) { console.log('kbModalBeforeSubmit'); });
kbModalSubmit (ModalAjax::EVENT_MODAL_SUBMIT)
在表单成功提交后触发此事件。此事件还可用以下附加参数
data: object,通过服务器响应发送的数据对象。status: string,jQuery AJAX 成功的文本状态。xhr: XMLHttpRequest,用于此事务的 jQuery XML Http Request 对象。selector: object,多 Modal 提交后嵌入逻辑的 jQuery 选择器。
$('#createCompany').on('kbModalSubmit', function(event, data, status, xhr, selector) { console.log('kbModalSubmit'); // You may call pjax reloads here });