【问题标题】:Select2 js Plugin Not able to select any optionSelect2 js插件无法选择任何选项
【发布时间】:2023-03-28 11:59:02
【问题描述】:

我正在使用Select2.js(最新版本)在我的应用程序中实现标记化标记。它工作正常,除了 我无法从建议中选择任何项目

我看到很少有人提到我们需要在配置中包含“id”。它似乎不适合我。

我的代码是:

$("#interest").select2({ ajax: {
            url: "get-interests",
            dataType: 'json',
            delay: 250,
            data: function (params) {
              return {
                q: params.term, // search term
                page: params.page
              };
            },
            processResults: function (data, page) {
              // parse the results into the format expected by Select2.
              // since we are using custom formatting functions we do not need to
              // alter the remote JSON data
              return {
                results: data
              };
            },
            cache: true
          },
          escapeMarkup: function (markup) { return markup; }, // let our custom formatter work
          placeholder:{
              id: "-1",
              text: "Add your interest.."
          } ,
          tags: true,
          tokenSeparators: [',', ' '],
          id: function(data) { 
              alert(JSON.stringify(data));
              return data.interestId; 
          },
          templateResult: function(post) {
              // return html markup for individual result item
              var markup ="";
              if(String(post.description) !== 'undefined') {
                 markup = '<p><b>' + post.text + '</b>  : ' + post.description + '</p>';
              } else {
                 markup = '<p><b>' + post.text + '</b></p>';
              }
              return markup;
           },
           formatSelection: function(post) {
              // This shows up in the select box
              return post.text;
           }

我在上面的配置中做错了什么?

【问题讨论】:

    标签: javascript jquery ajax html jquery-select2


    【解决方案1】:

    与您在代码中添加的注释相反,对于 Select2 4.0,您确实需要向 processResults 函数添加代码,以将返回的 JSON 数据转换为具有 id 属性的对象。通常,对象还应该具有 text 属性,但如果您提供 templateResulttemplateSelection 函数,它们就不必这样做。

    我看到很少有答案提到我们需要包括 “id”在我们的配置中。它似乎不适合我。

    这些答案对于以前版本的 Select2 是正确的,但在 Select2 v4.0 中,不再支持 id 函数。请参阅"4.0 Anouncement" 页面上的“严格执行 id 和 text 属性”部分:

    您还可以删除formatSelection 函数。对于 Select2 4.0,它现在应该命名为 templateSelection。这意味着它不会被你调用,但你可能没有注意到,因为你的 formatSelection 函数只是在做默认情况下所做的事情。


    processResults 函数应该返回一个具有results 属性的对象,该属性设置为一个对象数组。这些对象都需要有一个id 属性(但它们也可以有其他属性)。

    您没有显示返回的data 的样子,但从您的idtemplateResult 函数来看,它似乎是具有以下属性的对象数组:interestIdtextdescription。在这种情况下,您的 processResults 函数可能如下所示:

    // This builds a new array of new objects.
    processResults: function(data, page) {
        return {
            results: $.map(data, function(post) {
                return {
                    id: post.interestId,
                    text: post.text,
                    description: post.description
                };
            })
        };
    },
    

    或者这个:

    // This just adds an `id` property to the objects in the existing array.
    processResults: function(data, page) {
        $.each(data, function(i, post) {
            post.id = post.interestId;
        });
        return { results: data };
    }, 
    

    【讨论】:

    • processResult 中,data 是一个 JSON 对象,所以我需要从中获取 id 和 text 并创建一个新对象。我说得对吗?..如果你能给我看一些工作示例或为我写一个小小提琴,那将非常有帮助。谢谢
    • @user3035305 - 我已添加到我的答案中,以显示processResults 函数的外观。
    猜你喜欢
    • 1970-01-01
    • 2019-04-19
    • 2015-05-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-18
    相关资源
    最近更新 更多