-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAjaxButton.php
74 lines (65 loc) · 1.88 KB
/
AjaxButton.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
<?php
namespace demogorgorn\uikit;
use demogorgorn\uikit\Button;
use yii\helpers\Html;
use yii\helpers\Json;
/**
* AjaxButton renders an ajax button in UIKit stylу and is very similar to ajaxSubmitButton from Yii1.
*
* Example:
*
* ```php
* <?= Html::beginForm(); ?>
* <?= Select2::widget([
* 'name' => 'country_code',
* 'data' => Country::getAllCountries(),
* 'options' => [
* 'id' => 'country_select',
* 'multiple' => false,
* 'placeholder' => 'Select country...',
* 'class' => 'uk-width-medium-7-10'
* ]
* ]);?>
*
* <?php AjaxButton::begin([
* 'label'=>'Проверить',
* 'ajaxOptions'=>
* [
* 'type'=>'POST',
* 'url'=>'country/getinfo',
* 'cache' => false,
* 'success' => new \yii\web\JsExpression('function(html){
* $("#output").html(html);
* }'),
* ],
* 'options' => ['type' => 'submit'],
* ]);
* AjaxButton::end();?>
*
* <?= Html::endForm(); ?>
* ```
*
* @author Oleg Martemjanov <[email protected]>
* @since 2.0
*/
class AjaxButton extends Button
{
public $ajaxOptions = [];
public function run()
{
parent::run();
if (!empty($this->ajaxOptions))
$this->registerAjaxScript();
}
protected function registerAjaxScript()
{
$view = $this->getView();
if(!isset($this->ajaxOptions['data']) && isset($this->ajaxOptions['type']))
$this->ajaxOptions['data'] = new \yii\web\JsExpression('$(this).parents("form").serialize()');
$this->ajaxOptions= Json::encode($this->ajaxOptions);
$view->registerJs("$( '#".$this->options['id']."' ).click(function() {
$.ajax(". $this->ajaxOptions .");
return false;
});");
}
}