我正在尝试在编辑期间保持选择选项值。在那里我使用javascript作为动态依赖下拉列表。但是现在我面临一个问题,即在编辑期间选择选项值。有人会帮我解决这个问题吗? edit.blade.php
-
<form class="form-material form-horizontal" action="{{ route('admin.commercial.orders.update', $order->id) }}" method="POST">
@method('PUT')
@csrf
<div class="row">
<div class="col-md-6">
<div class="form-group">
<label class="col-md-12" for="name">Buyer Name: </label>
<div class="col-md-12">
<select class="form-control select2" name="name" id="buyerName">
<option value="">Select Buyer</option>
</select>
@if ($errors->has('name'))
<small class="error">{{ $errors->first('name') }}</small>
@endif
</div>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label class="col-md-12" for="job_name">Job Name/Title: </label>
<div class="col-md-12">
<select class="form-control select2" name="job_name" id="jobName">
<option value="">Select Job</option>
</select>
@if ($errors->has('job_name'))
<small class="error">{{ $errors->first('job_name') }}</small>
@endif
</div>
</div>
</div>
</div>
</form>
javascript for dependent dropdown -
<script type="text/javascript">
$(function () {
$(".select2").select2();
});
/** Dependent Dropdowns**/
$(function() {
dynamicDropdown('/admin/commercial/get-buyers', '#buyerName');
$('#buyerName').change(function() {
let url = `/admin/commercial/get-buyer-jobs/${this.value}`;
let target = '#jobName';
dynamicDropdown(url, target);
});
});
function dynamicDropdown(url, selector) {
$.get(url, function (data) {
let $select = $(selector);
$select.find('option').not(':first').remove();
let options = [];
$.each(data, function(index, item) {
options.push(`<option value="${item.id}">${item.name}</option>`);
})
$select.append(options);
});
}
</script>
我的edit function
是 -
public function edit($id)
{
$order = BuyerOrder::find($id);
return view('admin.marchendaising.commercials.orders.edit', compact('order'));
}
答案 0 :(得分:1)
将其添加到javascript的底部;
$("#buyerName").val("{{old('name')}}");
$("#jobName").val("{{old('job_name')}}");`
答案 1 :(得分:0)
您可以尝试这个想法:
public function fetchAllAllotments()
{
$pipeline = array(
array('$match' => array('id' => new MongoDB\BSON\ObjectID($this->id))),
array('$unwind'=>'$TaskDetails'),
array(
'$lookup' => array(
'from' => 'studentTbl',
'localField' => 'TaskDetails.StudentId',
'foreignField' => '_id',
'as' => 'StudentsDetails'
)
)
);
try
{
$cursor = $this->collection->aggregate($pipeline);
}
catch (Exception $e) {
}
return $cursor->toArray();
}
或强>
<select name="buyer">
@foreach(['buyer1', 'buyer2', 'buyer3'] as $buyer)
<option vlaue="{{ $buyer }}"{{ (old('buyer') == $buyer) ? ' selected' : '' }}>{{ $buyer }}</option>
@endforeach
</select>