forked from prawee/yii2-widgets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ButtonAjax.php
90 lines (78 loc) · 1.97 KB
/
ButtonAjax.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
<?php
/* 2014-11-05
* @author Prawee Wongsa <[email protected]>
* @reference http://www.yiiframework.com/wiki/690/render-a-form-in-a-modal-popup-using-ajax/
* @version 1.0
*/
namespace prawee\widgets;
use yii\base\Widget;
use yii\helpers\Html;
use yii\helpers\Url;
use yii\web\HttpException;
class ButtonAjax extends Widget
{
/*
* id for javascript
* @since 1.0
*/
public $id;
/* name of button */
public $name='ButtonAjax';
/*
* @route array
* getting content from this route
* ['create'],
* ['update','id'=>1]
*/
public $route=[];
/*
* options of button such as
* @value route of content
* @id id of button for javascript
* @class css class
*/
public $options=[];
/*
* id of modal
* @since 1.0
*/
public $modalId='#main-modal';
/*
* content of modal
* @since 1.0
*/
public $modalContent='#main-content-modal';
public function init()
{
parent::init();
if(empty($this->id)){
$this->id='btn-modal-'.$this->getId();
}
if(empty($this->route)){
throw new HttpException(404, 'please setting route options.');
}
if(empty($this->options['class'])){
$this->options['class']='btn btn-default';
}
if($this->options){
$this->options= array_merge($this->options,[
'id'=>$this->id,
'value'=>Url::to($this->route),
]);
}
}
public function run(){
$this->registerAssets();
return Html::button($this->name,$this->options);
}
protected function registerAssets()
{
$view = $this->getView();
$js ='$("#'.$this->id.'").click(function(){
$("'.$this->modalId.'").modal("show")
.find("'.$this->modalContent.'")
.load($(this).attr("value"));
});';
$view->registerJs($js);
}
}