$form->select('product_category_id','产品类别')->options(ProductCategory::where('parent_id',0)->pluck('title','id'))->load('product_category_er_id', '/admin/product_category_er');
$form->select('product_category_er_id','产品下一级类别');
public function product_category_er(){
$id=request('q');
return ProductCategory::where('parent_id',$id)->get(['id', DB::raw('title as text')]);
}
此处应该写为下面的:
return ProductCategory::where('parent_id','=', $parent_id)->get(['id', DB::raw('title as text')]);
不应写为:
return ProductCategory::where('parent_id','=', $parent_id)->pluck('title ', 'id');
laravel-admin解决表单SELECT联动时,编辑默认没选上
首先我们找到select的js,路径:跟目录/vendor/encore/laravel-admin/src/Form/Field下的Select.php文件,找到下面代码:
$script = <<<EOT
$(document).on('change', "{$this->getElementClassSelector()}", function () {
var target = $(this).closest('.fields-group').find(".$class");
$.get("$sourceUrl?q="+this.value, function (data) {
target.find("option").remove();
$(target).select2({
data: $.map(data, function (d) {
d.id = d.$idField;
d.text = d.$textField;
return d;
})
}).trigger('change');
});
});
EOT;
并修改成以下代码:
$script = <<<EOT
$(document).on('change', "{$this->getElementClassSelector()}", function () {
var target = $(this).closest('.fields-group').find(".$class");
$.get("$sourceUrl?q="+this.value, function (data) {
target.find("option").remove();
$(target).select2({
data: $.map(data, function (d) {
d.id = d.$idField;
d.text = d.$textField;
return d;
})
}).trigger('change');
});
});
$('{$this->getElementClassSelector()}').trigger('change');
EOT;
我们在原有代码中加入这句:
$('{$this->getElementClassSelector()}').trigger('change');