|
| 1 | +<?php |
| 2 | +namespace demogorgorn\ajax; |
| 3 | + |
| 4 | +use yii\base\Widget; |
| 5 | +use yii\helpers\Html; |
| 6 | +use yii\helpers\Json; |
| 7 | + |
| 8 | +/** |
| 9 | + * AjaxSubmitButton renders an ajax button which is very similar to ajaxSubmitButton from Yii1. |
| 10 | + * |
| 11 | + * Example: |
| 12 | + * |
| 13 | + * ```php |
| 14 | + * <?= Html::beginForm(); ?> |
| 15 | + * <?= Select2::widget([ |
| 16 | + * 'name' => 'country_code', |
| 17 | + * 'data' => Country::getAllCountries(), |
| 18 | + * 'options' => [ |
| 19 | + * 'id' => 'country_select', |
| 20 | + * 'multiple' => false, |
| 21 | + * 'placeholder' => 'Select country...', |
| 22 | + * 'class' => 'uk-width-medium-7-10' |
| 23 | + * ] |
| 24 | + * ]);?> |
| 25 | + * |
| 26 | + * <?php AjaxSubmitButton::begin([ |
| 27 | + * 'label'=>'Проверить', |
| 28 | + * 'ajaxOptions'=> |
| 29 | + * [ |
| 30 | + * 'type'=>'POST', |
| 31 | + * 'url'=>'country/getinfo', |
| 32 | + * 'cache' => false, |
| 33 | + * 'success' => new \yii\web\JsExpression('function(html){ |
| 34 | + * $("#output").html(html); |
| 35 | + * }'), |
| 36 | + * ], |
| 37 | + * 'options' => ['type' => 'submit'], |
| 38 | + * ]); |
| 39 | + * AjaxSubmitButton::end();?> |
| 40 | + * |
| 41 | + * <?= Html::endForm(); ?> |
| 42 | + * ``` |
| 43 | + * |
| 44 | + * @author Oleg Martemjanov <[email protected]> |
| 45 | + */ |
| 46 | + |
| 47 | +class AjaxSubmitButton extends Widget |
| 48 | +{ |
| 49 | + public $ajaxOptions = []; |
| 50 | + |
| 51 | + /** |
| 52 | + * @var array the HTML attributes for the widget container tag. |
| 53 | + */ |
| 54 | + public $options = []; |
| 55 | + |
| 56 | + /** |
| 57 | + * @var string the tag to use to render the button |
| 58 | + */ |
| 59 | + public $tagName = 'button'; |
| 60 | + /** |
| 61 | + * @var string the button label |
| 62 | + */ |
| 63 | + public $label = 'Button'; |
| 64 | + /** |
| 65 | + * @var boolean whether the label should be HTML-encoded. |
| 66 | + */ |
| 67 | + public $encodeLabel = true; |
| 68 | + |
| 69 | + /** |
| 70 | + * Initializes the widget. |
| 71 | + */ |
| 72 | + public function init() |
| 73 | + { |
| 74 | + parent::init(); |
| 75 | + if (!isset($this->options['id'])) { |
| 76 | + $this->options['id'] = $this->getId(); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + public function run() |
| 81 | + { |
| 82 | + parent::run(); |
| 83 | + |
| 84 | + echo Html::tag($this->tagName, $this->encodeLabel ? Html::encode($this->label) : $this->label, $this->options); |
| 85 | + |
| 86 | + if (!empty($this->ajaxOptions)) |
| 87 | + $this->registerAjaxScript(); |
| 88 | + } |
| 89 | + |
| 90 | + protected function registerAjaxScript() |
| 91 | + { |
| 92 | + $view = $this->getView(); |
| 93 | + |
| 94 | + if(!isset($this->ajaxOptions['data']) && isset($this->ajaxOptions['type'])) |
| 95 | + $this->ajaxOptions['data'] = new \yii\web\JsExpression('$(this).parents("form").serialize()'); |
| 96 | + |
| 97 | + $this->ajaxOptions= Json::encode($this->ajaxOptions); |
| 98 | + $view->registerJs("$( '#".$this->options['id']."' ).click(function() { |
| 99 | + $.ajax(". $this->ajaxOptions ."); |
| 100 | + return false; |
| 101 | + });"); |
| 102 | + } |
| 103 | + |
| 104 | +} |
0 commit comments