demogorgorn/yii2-ajax-submit-button
Composer 安装命令:
composer require demogorgorn/yii2-ajax-submit-button
包简介
AjaxSubmitButton renders an ajax button which is very similar to ajaxSubmitButton from Yii1.
关键字:
README 文档
README
=====================================
This is the powerful AjaxSubmitButton widget that renders an ajax button which is very similar to ajaxSubmitButton from Yii1 for Yii 2, but has many useful functions.
Basic Example
Example of usage of the widget with a custom widget (in this case Select2 widget).
The view:
use demogorgorn\ajax\AjaxSubmitButton; <?php echo Html::beginForm('', 'post', ['class'=>'uk-width-medium-1-1 uk-form uk-form-horizontal']); ?> <?= Select2::widget([ 'name' => 'country_code', 'data' => Country::getAllCountries(), 'options' => [ 'id' => 'country_select', 'multiple' => false, 'placeholder' => 'Choose...', 'class' => 'uk-width-medium-7-10'] ]); ?> <?php AjaxSubmitButton::begin([ 'label' => 'Check', 'ajaxOptions' => [ 'type'=>'POST', 'url'=>'country/getinfo', 'success' => new \yii\web\JsExpression('function(html){ $("#output").html(html); }'), ], 'options' => ['class' => 'customclass', 'type' => 'submit'], ]); AjaxSubmitButton::end(); ?> <?php echo Html::endForm(); ?> <div id="output"></div>
Please note: that #output is a div element which will be updated.
In controller:
public function actionGetinfo() { if(!isset($_POST['country_code']) || empty($_POST['country_code'])) return; $code = $_POST['country_code']; return $this->renderAjax('resultwidget', ['code' => $code]); }
Example of usage with ActiveForm and client validation
The view:
$form = ActiveForm::begin([ 'id' => 'add-form', 'options' => ['class' => 'form-inline'], ]); ... AjaxSubmitButton::begin([ 'label' => 'Add', 'useWithActiveForm' => 'add-form', 'ajaxOptions' => [ 'type' => 'POST', 'success' => new \yii\web\JsExpression("function(data) { if (data.status == true) { closeTopbar(); } }"), ], 'options' => ['class' => 'btn btn-primary', 'type' => 'submit', 'id' =>'add-button'], ]); AjaxSubmitButton::end(); ActiveForm::end()
As you can see it's quite easy to use the widget with ActiveForm - enough to set the ActiveForm's id to the 'useWithActiveForm' variable. (In this case id is 'add-form', without '#' symbol!).
Client validation
As I said before the widget can be used with ActiveForm and client validation enabled. If you wish to disable it, just set ActiveForm's 'enableClientValidation' to false.
$form = ActiveForm::begin([ 'id' => 'filters-form', 'enableClientValidation' => false ]);
Preloader use
It's possible to use the widget with your own custom preloader.
<?php AjaxSubmitButton::begin([ 'label' => 'Check', 'ajaxOptions' => [ 'type'=>'POST', 'url'=>'country/getinfo', 'beforeSend' => new \yii\web\JsExpression('function(html){ ... show preloader... }'), 'success' => new \yii\web\JsExpression('function(html){ ... hide preloader ... }'), ], 'options' => ['class' => 'customclass', 'type' => 'submit'], ]); AjaxSubmitButton::end(); ?>
File Uploads
AjaxSubmitButton fully supports file uploads. For example (look at the comments):
<?php AjaxSubmitButton::begin([ 'label' => 'Сохранить', 'useWithActiveForm' => 'add-form', 'ajaxOptions' => [ 'type' => 'POST', 'processData' => false, // Don't process the files 'contentType' => false, // Set content type to false as jQuery will tell the server its a query string request 'data' => new \yii\web\JsExpression("new FormData($('#add-form')[0])"), // Do not stringify the form 'success' => new \yii\web\JsExpression("function(data) { if (data.status == true) { // process result } }"), ], 'options' => ['class' => 'btn btn-primary', 'type' => 'submit', 'id' =>'add-button'], ]); AjaxSubmitButton::end(); ?>
Widget's options
| Variable | Description | Type |
|---|---|---|
| ajaxOptions | ajax options | Array |
| options | HTML attributes and other options of the widget's container tag | Array |
| tagName | the tag to use to render the button (by default is 'button'. You can specify, for example, 'a' tag) | String |
| label | button's label | String |
| icon | button's icon | String (e.g. 'fa fa-download') |
| iconPosition | button icon position | const (ICON_POSITION_LEFT or ICON_POSITION_RIGHT) |
| encodeLabel | whether the label should be HTML-encoded | Boolean |
| clickedButtonVarName | js object name. It is unused when useWithActiveForm is enabled | String |
| useWithActiveForm | whether the button should not be used with ActiveForm. the id of ActiveForm to use the button with | Boolean / String |
Installation
The preferred way to install this extension is through composer.
Either run
php composer.phar require demogorgorn/yii2-ajax-submit-button "*"
or add
"demogorgorn/yii2-ajax-submit-button": "*"
to the require section of your composer.json file and run composer update.
demogorgorn/yii2-ajax-submit-button 适用场景与选型建议
demogorgorn/yii2-ajax-submit-button 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 137.28k 次下载、GitHub Stars 达 27, 最近一次更新时间为 2014 年 06 月 13 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「ajax」 「extension」 「widget」 「button」 「submit」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 demogorgorn/yii2-ajax-submit-button 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 demogorgorn/yii2-ajax-submit-button 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 demogorgorn/yii2-ajax-submit-button 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A custom URL rule class for Yii 2 which allows to create translated URL rules
Yii2 LightBox image galary widget uses Lightbox v2.10.0 by Lokesh Dhakar
The Yii2 extension uses jQuery jquery.carousel-1.1.min.js and makes image carousel from php array of structure defined.
Selenium4 (WebDriver) driver for Mink framework
laravel middleware to allow ajax only calls
TYPO3 CMS extension to create gallery content element with preset crop ratios and pagination
统计信息
- 总下载量: 137.28k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 29
- 点击次数: 21
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2014-06-13