简体   繁体   English

动态加载选项 - JQuery Select2

[英]Load option dynamically - JQuery Select2

Currently , I am trying to load dynamic options to select2 JQuery dropdown.目前,我正在尝试将动态选项加载到 select2 JQuery 下拉列表中。 These data are stored in another function, when user search for specific data it will call the other function and get data if exist.这些数据存储在另一个函数中,当用户搜索特定数据时,它将调用另一个函数并获取数据(如果存在)。 I don't want to use ajax because I am not calling an api request , it just a function written in C# and I am calling this function in Javascript.我不想使用 ajax,因为我没有调用 api 请求,它只是一个用 C# 编写的函数,我在 Javascript 中调用这个函数。 However I am not being able to display the matched options and the only way to call data dynamically is through ajax.但是我无法显示匹配的选项,动态调用数据的唯一方法是通过 ajax。

 $("#lnkIssue").select2({
            placeholder: 'Start typing...',
            minimumInputLength: 1,
            theme: 'bootstrap4',
            width: '100%',
            closeOnSelect: true,
            tags: false,
            allowClear: false,
            multiple: false,
            results:customFunction(params)
)}  


function(params)
{
   // Implementation
   return results = {id:'example-1',text:'example-2'}
}  // it didn't work

Is there a way to return results to select2 other than ajax function ?除了ajax函数之外,有没有办法将结果返回给select2?

Yes, just pass the function into the data property是的,只需将函数传递给data属性

$("#lnkIssue").select2({
    placeholder: 'Start typing...',
    minimumInputLength: 1,
    theme: 'bootstrap4',
    width: '100%',
    closeOnSelect: true,
    tags: false,
    allowClear: false,
    multiple: false,
    data: getData()
  })

const getData = () => {
  return [{
    id: 'example-1',
    text: 'example-2'
  }]
}

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

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