Choices.js:将动态数据添加到选择选项中

Posted

技术标签:

【中文标题】Choices.js:将动态数据添加到选择选项中【英文标题】:Choices.js: Add dynamic data into the select option 【发布时间】:2022-01-07 18:37:03 【问题描述】:

我使用Choices.js 进行多选和带有搜索框的选择。我也在使用 angular.js 来填充数据,但它似乎不起作用。谁能帮我动态填充选项。

正在使用 Ajax 检索数据。

这是我尝试过的

<label for="docno" class="col-form-label">Name :</label>
<select id='docno' class="choices form-select" ng-model="docno" ng-change='getDocs()' required="true">
     <option ng-repeat="d in documents" ng-value="d.docno" selected='selected'>d.name</option>
</select>

【问题讨论】:

【参考方案1】:

您可以在选择实例中使用 setValue,如下所示:

var choices = new Choices(document.getGetElementById("docno"));
var myDynamicItems = [
    
        value: 'Value 1',
        label: 'Label 1',
        id: 1
    ,
    
        value: 'Value 2',
        label: 'Label 2',
        id: 2
    
];

inst.choices.setValue(myDynamicItems);

【讨论】:

不幸的是,我的页面要复杂得多,无法使用choices.setValue(),我最终使用Select2,它工作正常!

以上是关于Choices.js:将动态数据添加到选择选项中的主要内容,如果未能解决你的问题,请参考以下文章