[英]Load pre-selected values from database to selectize
I am using a JavaScript selectize to allow users to add multiple permissions when creating a role. 我正在使用JavaScript selectize允许用户在创建角色时添加多个权限。 This part wonderfully.
这部分很棒。
The challenge comes in when the user would like to edit a certain role. 当用户想要编辑某个角色时,挑战就来了。 I would like to load the Selectize with the data that they had previously selected when creating the role.
我想用他们在创建角色时先前选择的数据加载Selectize。 Here is my code;
这是我的代码;
Blade 刀
<select id="permissions" name="permission[]" multiple="multiple">
<option value="">Choose a permission</option>
@foreach($permission as $value)
<option value="{{ $value->name }}">{{ $value->name }}</option>
@endforeach
</select>
JavaScript JavaScript的
<script>
$(function() {
$('#permissions').selectize();
});
</script>
Currently, the selectize loads while empty and I am not sure how to tackle this challenge. 当前,选择加载为空时,我不确定如何应对这一挑战。 Kindly assist.
请协助。
try this: 尝试这个:
<select id="permissions" name="permission[]" multiple="multiple">
<option value="">Choose a permission</option>
@foreach($permission as $value)
<option value="{{ $value->name }}" {{ (in_array($value->name, $permissionsArray)) ? 'selected' : '' }} {{ (collect(old('permission'))->contains($value->name)) ? 'selected':'' }}>{{ $value->name }}</option>
@endforeach
</select>
You can use the in_array function of php to check if the permission is already in selected permissions. 您可以使用php的in_array函数检查该权限是否已在所选权限中。
@foreach($permission as $value)
<option {{in_array($value->name, $selectedPermissions) ? 'selected':''}} value="{{ $value->name }}">
{{$value->name}}
</option>
@endforeach
REF: http://php.net/manual/en/function.in-array.php 参考: http : //php.net/manual/en/function.in-array.php
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.