-
Notifications
You must be signed in to change notification settings - Fork 14
/
Widgets_form.php
119 lines (89 loc) · 3.64 KB
/
Widgets_form.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
<?php
namespace App\Admin\Controllers;
use Illuminate\Support\Facades\Input;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Widgets\Form as WidgetsForm;
use Field\Interaction\FieldTriggerTrait;
use Field\Interaction\FieldSubscriberTrait;
use App\User;
/** 此代码只做展示,不能运行**/
class UserController extends Controller
{
// 使用FieldTriggerTrait,和FieldSubscriberTrait
use HasResourceActions, FieldTriggerTrait, FieldSubscriberTrait;
/**
* Make a form builder.
*
* @return Form
*/
protected function form()
{
/**
* Widget form 没有获取内部fields的接口,所以只能在外部将fields收集起来。
*
*/
$form = new WidgetsForm();
$fields = array();
$f = $form->select('column1', 'select');
$f->options(['1'=>'1', '2'=>'2']);
array_push($fields, $f);
$f = $form->radio('column2', 'radio');
$f->options(['1'=>'enable', '2'=>'disable']);
array_push($fields, $f);
$f = $form->checkbox('column3', 'checkbox');
$f->options(['1'=>'1', '2'=>'2', '3'=>'3']);
array_push($fields, $f);
$f = $form->text('column5', 'text');
array_push($fields, $f);
$form->divider();
$form->html('以下是响应事件的控件');
$f = $form->textarea('column4', 'textarea');
array_push($fields, $f);
// 因为将收集到fields放入createTriggerScript和createSubscriberScript中生成Script
$trigger_script = $this->createTriggerScript($fields);
$subscriber_script = $this->createSubscriberScript($fields, function($builder) {
$builder->subscribe('column1', 'select', function($event) {
return <<<EOT
function (data) {
$('.column4').val('你选择了'+data.text);
}
EOT;
});
$builder->subscribe('column2', 'checked', function ($event) {
return <<< EOT
function (data) {
if (data == '1'){
$('.column4').attr('disabled', false);
}else{
$('.column4').attr('disabled', true);
}
}
EOT;
});
$builder->subscribe('column3', 'checked' , function ($event) {
return <<< EOT
function (data) {
$('.column4').val('你选择了 ' + data);
}
EOT;
});
$builder->subscribe('column3', 'unchecked', function ($event) {
return <<< EOT
function (data) {
$('.column4').val('你选择了 ' + data);
}
EOT;
});
$builder->subscribe('column5', 'input', function ($event) {
return <<< EOT
function (data) {
$('.column4').val(data);
}
EOT;
});
});
$form->scriptinjecter('name_no_care', $trigger_script, $subscriber_script);
return $form;
}
}