PHP code example of zuweie / field-interaction
1. Go to this page and download the library: Download zuweie/field-interaction library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
zuweie / field-interaction example snippets
php artisan vendor:publish --provider="Field\Interaction\InteractionServiceProvider"
// UserController 中的 form 函数。
protected function form(){
// Encore\Admin\Form
$form = new Form(new User());
...
...
// 一些控件的定义
...
...
// 在定义完控件后。。。
// 弄一个触发事件的Script对象。
$triggerScript = $this->createTriggerScript($form);
// 弄-个接收并处理事件的Script对象。
$subscribeScript = $this->createSubscriberScript($form, function($builder){
// 添加事件响应函数
$builder->subscribe('column_listen_to', 'event_will_triggered', function($event){
// 这里填写处理事件的javascript脚本,注意:一定要返回一个完整的 javascript function ,否则报错!!!!
return <<< EOT
// function中的参数data,是事件自带数据,方便做逻辑处理!data会因为事件不同而类型不同,具体可以在chrome中的console中查看。
function(data){
console.log ('catch an event -> {$event}');
// 某个控件对于某个事件做出处理,
$('xxx').doSomething();
//.... 事件处理 ....
}
EOT;
});
});
// 最后把 $triggerScript 和 $subscribeScript 注入到Form中去。
// scriptinjecter 第一个参数可以为任何字符,但不能为空!!!!
$form->scriptinjecter('any_name_but_no_empty', $triggerScript, $subscribeScript);
}
protected function form(){
// Encore\Admin\Widgets\Form
$form = new Form();
// 定义一个数组用于收
$fields = array();
$f = $form->select('xxxx', 'xxx');
array_push($fields, $f);
$f->option([...]);
$f = $form->text('xxxx', 'xxx');
array_push($fields, $f);
// 在定义完控件后。。。
// 弄一个触发事件的Script对象。
$triggerScript = $this->createTriggerScript($fields);
// 弄-个接收并处理事件的Script对象。
$subscribeScript = $this->createSubscriberScript($fields, function($builder){
// 添加事件响应函数
$builder->subscribe('column_listen_to', 'event_will_triggered', function($event){
// 这里填写处理事件的javascript脚本,注意:一定要返回一个完整的 javascript function ,否则报错!!!!
return <<< EOT
// function中的参数data,是事件自带数据,方便做逻辑处理!data会因为事件不同而类型不同,具体可以在chrome中的console中查看。
function(data){
console.log ('catch an event -> {$event}');
// 某个控件对于某个事件做出处理,
$('xxx').doSomething();
//.... 事件处理 ....
}
EOT;
});
});
// 最后把 $triggerScript 和 $subscribeScript 注入到Form中去。
// scriptinjecter 第一个参数可以为任何字符,但不能为空!!!!
$form->scriptinjecter('anyname_but_no_empty', $triggerScript, $subscribeScript);
}
protected function form() {
// UserController form 函数中
$form = new Form(new User());
// 创建触发脚本
$trigger = $this->createTriggerScript($form);
// 创建监听和响应的脚本。
$subscribe = $this->createSubscriberScript($form, function($builder){
$builder->subscribe('username', 'input', function($event){
return <<< EOT
function (data) {
console.log(data);
}
EOT;
});
});
// 使用了tab的form
$form->tab('Basic info', function ($form) {
$form->text('username');
$form->email('email');
})->tab('Profile', function ($form) {
$form->image('avatar');
$form->text('address');
$form->mobile('phone');
})->tab('Jobs', function ($form) use ($trigger, $subscribe) {
$form->text('company');
$form->date('start_date');
$form->date('end_date');
/*** scriptinjecter 函数放在任意一个tab函数中,这里示例代码放在了最后一个tab,也可以放任意前两个tab函数,任君选择 ***/
/*** scriptinjecter 函数只需在任意一个tab中调用一次即可,不能重复调用!***/
$form->scriptinjecter('xxx', $trigger, $subscribe);
});
}