简体   繁体   English

select2 中的默认选择选项,带有远程数据的多个 select

[英]Default selected option in select2 multiple select with remote data

I am trying to add the default selected option to multiple select2 with remote data.我正在尝试将默认选择的选项添加到具有远程数据的多个 select2 中。 But the title is set instead of the option text.但是设置了标题而不是选项文本。 This is my code:这是我的代码:

Html Html

<select class="form-control select_medicines" id="select_medicines" name="medicines[]" multiple="multiple">
    @foreach($medicines as $medicine)
        <option value="{{$medicine->id}}" selected="selected">{{$medicine->fa_name . "(" . $medicine->en_name . ")"}}</option>
    @endforeach
</select>

Js JS

$(document).ready(function() {
$(".select_medicines").select2({
    ajax: {
        url: 'url',
        dataType: 'json',
        delay: 250,
        data: function (params) {
            return {
                search: params.term, // search term
                page: params.page
            };
        },
        processResults: function (data, params) {
            params.page = params.page || 1;

            return {
                results: $.map(data.data, function(obj) {
                    return {
                        id: obj.id,
                        fa_name: obj.fa_name,
                        en_name: obj.en_name,
                        is_for_diabetes: obj.is_for_diabetes
                    };
                }),
                pagination: {
                    more: (params.page * 15) < data.total_count
                }
            };
        },
        cache: true
    },
    placeholder: 'search',
    width: '100%',
    minimumInputLength: 3,
    escapeMarkup: function(markup) {
        return markup;
    },
    templateResult: formatRepo,
    templateSelection: formatRepoSelection
});

function formatRepo (repo) {
    if (repo.loading) {
        return repo.text;
    }

    var markup = "<div class='select2-result-repository clearfix'>" +
        "<div class='select2-result-repository__meta'>" ;

    markup += "<div class='mt-0 mb-2 text-dark'>";

    if (repo.is_for_diabetes === "1"){
        markup+= "<i class='mdi mdi-star mr-1'></i>";
    }

    markup += repo.fa_name + "(" + repo.en_name + ")";
    markup += "</div>";

    markup += "</div></div>";
    return markup;
}

function formatRepoSelection (repo) {
    return repo.fa_name || repo.en_name;
}
});

and this is the result:这是结果: 在此处输入图像描述

the title of option is set but text of option is empty.选项的标题已设置,但选项的文本为空。 how can I set text of option?如何设置选项文本? default selected options must be shown like "select after search"默认选择的选项必须显示为“搜索后选择” 在此处输入图像描述

I try to add value like {id: 12, text: metformin} to select but the default options were not selected and the remote data did not work for the options and only showed the data in Json instead of the remote data.我尝试向 select 添加像 {id: 12, text: metaformin} 这样的值,但未选择默认选项,远程数据不适用于选项,仅显示 Json 中的数据而不是远程数据。

I did this as another solution but no option was selected.我这样做是另一种解决方案,但没有选择任何选项。

Controller Controller

$medicines_id = $user->medicines()->pluck('medicines.id')->all();
$medicines_fa_name = $user->medicines()->pluck('medicines.fa_name')->all();

HTML HTML

<select class="form-control select_medicines" id="select_medicines" name="medicines[]" multiple="multiple"></select>
@if(count($medicines_id) > 0)
    @foreach($medicines_id as $k => $medicine_id)
        <input value='{{json_encode("{id: $medicine_id, text: $medicines_fa_name[$k]}")}}' class="checked_medicines" readonly hidden>
    @endforeach
@endif

These lines were added to the JavaScript file.这些行已添加到 JavaScript 文件中。

JS JS

$('.checked_medicines').each(function () {
    var val = $(this).val();
    var json  = JSON.parse(val);
    //json = {id: 12, text: گلی پیزاید}
    $(".select_medicines").select2('data',json);
});

I don't know if I understand you well.我不知道我是否理解你。 Are you trying to store JSON data inside the value attribute of the option tag?您是否尝试将 JSON 数据存储在选项标签的 value 属性中?

<select class="form-control select_medicines" id="select_medicines" name="medicines[]" multiple="multiple">
    @foreach($medicines as $medicine)
        <option value='{{json_encode($medicine)}}' selected="selected">{{$medicine->fa_name . "(" . $medicine->en_name . ")"}}</option>
    @endforeach
</select>

Be careful to use single quotes on the value attribute to avoid mixing it with the dobleqoutes of the JSON string注意在 value 属性上使用单引号,以避免将其与 JSON 字符串的双引号混淆

after 4month I found my answer. 4个月后,我找到了答案。 the problem was in "formatRepoSelection" function for select 2. I must change it to问题出在“formatRepoSelection”function for select 2 中。我必须将其更改为

function formatRepoSelection (repo) {
    return repo.fa_name || repo.en_name || repo.text;
}

I had deleted the "repo.text" code and the problem was that the label was not displayed.我删除了“repo.text”代码,问题是没有显示 label。 Now after adding this part, the problem is solved.现在添加这部分后,问题就解决了。

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

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