Laravel-admin 中列的操作-开关有 BUG


这里设置了value,判断选中使用的value,但是提交确实on和off

温馨提示:QQ搜索laravel群搜不到,因为含有av这个词

让PHP再次伟大
《L01 基础入门》
我们将带你从零开发一个项目并部署到线上,本课程教授 Web 开发中专业、实用的技能,如 Git 工作流、Laravel Mix 前端工作流等。
《G01 Go 实战入门》
从零开始带你一步步开发一个 Go 博客项目,让你在最短的时间内学会使用 Go 进行编码。项目结构很大程度上参考了 Laravel。
最佳答案
  • 是laravel-admin的bug
  • 想办法修改源码里面这个方法: SwitchField@render
  • 要修改的地方,我写了注释
<?php  namespace Encore\Admin\Form\Field;  use Encore\Admin\Form\Field; use Illuminate\Support\Arr;  class SwitchField extends Field {     public function render()     {         $this->script = <<<EOT  $('{$this->getElementClassSelector()}.la_checkbox').bootstrapSwitch({     size:'{$this->size}',     onText: '{$this->states['on']['text']}',     offText: '{$this->states['off']['text']}',     onColor: '{$this->states['on']['color']}',     offColor: '{$this->states['off']['color']}',     onSwitchChange: function(event, state) {         //这一行是源码         $(event.target).closest('.bootstrap-switch').next().val(state ? 'on' : 'off').change();         //修改成这样,value就是0、1了         $(event.target).closest('.bootstrap-switch').next().val(state ? '{$this->states['on']['value']}' : '{$this->states['off']['value']}').change();     } });  EOT;          return parent::render();     } }
4年前 评论
讨论数量: 4

就描述的现象来说,这个并看不出问题来。

states 的结构,在 grid 和 form 里面都是这样用的, on , off 按它的默认约定,都是可以正常转换的。

由图可知,请求已经成功触发了。

关注点转移一下,找到哪个方法响应了这个请求,那个方法内部是否妥善处理了这个 status 的值了。

4年前 评论
勇敢的心 (楼主) 4年前
勇敢的心 (楼主) 4年前

switch 控件,需要在 grid 和 form 里面同时使用才有效

4年前 评论

protected function form()添加

 $form->number('status','标签');   //当然可以换成switch

grid编辑的
都是调用form(),因此form()必须存在要编辑的字段。

4年前 评论
  • 是laravel-admin的bug
  • 想办法修改源码里面这个方法: SwitchField@render
  • 要修改的地方,我写了注释
<?php  namespace Encore\Admin\Form\Field;  use Encore\Admin\Form\Field; use Illuminate\Support\Arr;  class SwitchField extends Field {     public function render()     {         $this->script = <<<EOT  $('{$this->getElementClassSelector()}.la_checkbox').bootstrapSwitch({     size:'{$this->size}',     onText: '{$this->states['on']['text']}',     offText: '{$this->states['off']['text']}',     onColor: '{$this->states['on']['color']}',     offColor: '{$this->states['off']['color']}',     onSwitchChange: function(event, state) {         //这一行是源码         $(event.target).closest('.bootstrap-switch').next().val(state ? 'on' : 'off').change();         //修改成这样,value就是0、1了         $(event.target).closest('.bootstrap-switch').next().val(state ? '{$this->states['on']['value']}' : '{$this->states['off']['value']}').change();     } });  EOT;          return parent::render();     } }
4年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!