选择款式品类,初始化款式品名
库结构:
路由:
$router->get('api', RyhPredictController::class.'@api');
控制器部分代码:
$bulkcategory = PubBulkCategory::getSelectMapOptions(['status'=>0,'type'=>0]);//加工费款式品类
$bulkplace = PubBulkCategory::getSelectMapOptions(['status'=>0,'type'=>1]);//加工费款式品名
public function api (Request $request)
{
$id = $request->get('q');
return PubBulkCategory::where(['parent_id'=>$id])->get(['id', DB::raw('title as text')])->toArray();
}
视图代码:
<div class="panel-body " style="padding-bottom: 0px">
<div class="form-group">
{{-- 大货款式品类 --}}
<div class="col-md-3">
<div class="">
<label for="bulkcate_id" class="asterisk control-label col-sm-5" >款式品类</label>
<div class="col-sm-7">
<select class="form-control bulkcate_id " id="bulkcate_id" name="bulkcate_id" required="1" placeholder="必填项" >
<option value="">请选择</option>
@foreach ( $bulkcategory as $k =>$v)
<option value="{{ $k }}" @if(isset($data->bulkstyle->bulk_cate_id) && $data->bulkstyle->bulk_cate_id == $k) selected @endif>{{ $v }}</option>
@endforeach
</select>
</div>
</div>
</div>
{{-- 大货款式品名 --}}
<div class="col-md-3" >
<div class="">
<label for="bulkplace_id" class="asterisk control-label col-sm-5" >款式品名</label>
<div class="col-sm-7">
<select class="form-control bulkplace_id" id="bulkplace_id" name="bulkplace_id" required="1" placeholder="必填项" >
<option value="">请选择</option>
@foreach( $bulkplace as $k =>$v)
option value="{{ $k }}" @if(isset($data->bulk_place_id) && $data->bulk_place_id == $k) selected @endif>{{ $v }}</option>
@endforeach
</select>
</div>
</div>
</div>
</div>
</div>
<script>
$(function () {
$(".bulkcate_id").select2({"allowClear":true,"placeholder":{"id":"","text":"品类"}});
$(".bulkplace_id").select2({"allowClear":true,"placeholder":{"id":"","text":"品名"}});
$(document).on('change', ".bulkcate_id", function () {
initbulkplace(this.value);
});
})
function initbulkplace(bulkcate_id) {
var bulkplace_id = $(".bulkplace_id");
$.get("/admin/predict/api",{q : bulkcate_id}, function (data) {
bulkplace_id.find("option").remove();
$(bulkplace_id).select2({
placeholder: {"id":"","text":"选择品名"},
allowClear: 1,
data: $.map(data, function (d) {
d.id = d.id;
d.text = d.text;
return d;
}),
}).trigger('change');
});
}
</script>
或者:
============================================
public function api (Request $request)
{
$array = PubBulkCategory::getSelectMapOptions(['parent_id'=>$request->q]);
return $array;
}
function initbulkplace(bulkcate_id) {
var bulkplace_id = $(".bulkplace_id");
$.get("/admin/cost/api",{q : bulkcate_id}, function (data) {
bulkplace_id.find("option").remove();
var arr = [];
$.map(data, function (d,i) {
var a = {
'id':i,
'text':d,
};
arr.push(a);
});
$(bulkplace_id).select2({
placeholder: {"id":"","text":"选择品名"},
allowClear: 1,
data: arr,
}).trigger('change');
});
}