我已在我的应用程序中实现了bootstrap multi-select多选。我在编辑表单中预先选择值时遇到问题。下面是我使用的js代码:
<script>
$(document).ready(function() {
$('#tincludeds').multiselect({
includeSelectAllOption: true,
selectAllJustVisible: false,
enableFiltering: true,
numberDisplayed: 1,
maxHeight: 600,
buttonWidth: '400px',
select: {!! json_encode($tour->tIncludeds()->allRelatedIds()) !!}
});
});
</script>
但脚本无效。我在这里错过了什么吗?以下是{{ dd(json_encode($tour->tIncludeds()->allRelatedIds())) }}
我视图中多选字段的Html代码:
<div class="form-group">
<label for="tincluded">Includeds</label>
<select class="form-control" id="tincludeds" multiple="multiple" name="tincludeds[]" class="tincluded">
@foreach($tincludeds as $included)
<option value="{{ $included->id }}">{{ $included->name }}</option>t
@endforeach
</select>
@if ($errors->has('tincludeds'))
<span class="help-block">{{$errors->first('tincludeds')}}</span>
@endif
</div>
答案 0 :(得分:0)
您可以在代码中执行以下操作:
<?php
$listofIds = $tour->tIncludeds()->allRelatedIds(); // array with list of ids
?>
<div class="form-group">
<label for="tincluded">Includeds</label>
<select class="form-control" id="tincludeds" multiple="multiple" name="tincludeds[]" class="tincluded">
@foreach($tincludeds as $included)
<option value="{{ $included->id }}" <?php echo in_array($included->id, $listofIds) ? 'selected' : ''?> >{{ $included->name }}</option>
@endforeach
</select>
@if ($errors->has('tincludeds'))
<span class="help-block">{{$errors->first('tincludeds')}}</span>
@endif
</div>