简体   繁体   English

检索Selectize.js的值

[英]Retrieving values for Selectize.js

I am using Selectize.js to create a tagging like experience, for adding users to jobs, using the current users of the system. 我正在使用Selectize.js创建类似体验的标记,以便使用系统的当前用户将用户添加到作业中。

In my view 在我看来

Following the documentation, I have a select box with the multiple attribute which I populate with the users on the system. 根据文档,我有一个带有multi属性的选择框,该框由系统上的用户填充。 I have also declared that this input is an array by adding square braces. 我还通过添加方括号声明此输入为数组。

<select name="job_administrator[]" multiple id="selectize" class="form-control{{ $errors->has('job_administrator') ? ' is-invalid' : '' }}">
    <option value="">Select a user </option>
    @foreach($users as $user)
    <option value="{{ $user->id }}">{{ $user->full_name }} - {{ $user->access_level }}</option>
    @endforeach
</select>

Then, I initialize the plugin 然后,我初始化插件

$('#selectize').selectize({
    placeholder: 'Select user',
    plugins: ['remove_button'],
    delimiter: ',',
    persist: false,
})

In my Controller 在我的控制器中

/**
 * Handle adding job administrators to jobs
 *
 * @param Array $data
 * @return void
 */
public function addAdministrators(Array $data, int $id)
{
    $vacancy = JobPost::where('id', $id)->first();

    if(!empty($data['job_administrator'])){
        $jobAdmins = $data['job_administrator'];

        // Grab the IDs for the tags in the array
        $admins = Admin::whereIn('id', $jobAdmins)->get()->pluck('id');

        $vacancy->administrators()->sync($admins);
    } else {
        // If there were no tags, remove them from this model instance
        $vacancy->administrators()->sync(array());
    }

}

My question is: on the editing screen is it possible to prefill the selected items when using Selectize? 我的问题是:使用Selectize时,是否可以在编辑屏幕上预填充所选项目?

Yes you can, just add the selected attribute to the option. 是的,您可以,只需将选定的属性添加到选项中即可。 You can do something like this. 你可以做这样的事情。

<option value="{{ $user->id }}" {{$selected->contains($user->id) ? 'selected' : ''}}>{{ $user->full_name }} - {{ $user->access_level }}</option>

The $selected is then a collection of ids. 然后,$ selected是ID的集合。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM