laravel-admin select 二階連動
#Model 內加入不要更新時間
$table->bigIncrements(‘id’);
$table->integer(‘m_id’)->default(0)->comment(‘母選單’);
$table->string(‘cityname’)->comment(‘城市名稱’);
$table->enum(‘active’, [1, 0])->default(1)->comment(‘狀態’);
$table->unsignedInteger(‘sort’)->default(1)->comment(‘排序’);
$table->timestamps();
public $timestamps = false;
Route::get(‘/cmenu/city’, ‘CmenuController@city’);
#CmenuController.php
function city(Request $request){
$projectId = $request->get(‘q’);
return Cmenu::where(‘m_id’,$projectId)->get([‘id’, DB::raw(‘cityname as text’)]);
}
$form->select(‘zone’, __(‘市’))->options(
Cmenu::all()->where(‘m_id’,’=’,’0′)
->pluck(‘cityname’,’id’))
->load(‘city’, ‘/english/public/cmenu/city’);
#load 內的路徑要打全名,我在這卡關
$form->select(‘city’,’鎮’);
\vendor\encore\laravel-admin\src\Form\Field\Select.php
修改下列的內容
$script = <<
$(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({
placeholder: $placeholder,
allowClear: $strAllowClear,
data: $.map(data, function (d) {
d.id = d.$idField;
d.text = d.$textField;
return d;
})
}).trigger(‘change’);
});
});
$(‘{$this->getElementClassSelector()}’).trigger(‘change’); <--加這一句,子選單就會連動 EOT;