我 Select 了2在进行AJAX调用后显示数据.

 <script>
    $(document).ready(function () {
        $("#citySelect").select2({
            theme: 'bootstrap-5',
            placeholder: "Search for your city",
            dropdownAutoWidth: true,
            width: 'auto',
            minimumInputLength: 3,
            allowClear: true,
            async: true,
            ajax: {
                url:
                    function (params) {
                        return '@GatewaySettings.Value.Url' + '/api/location/cities/' + params.term;
                    },
                contentType: "application/json; charset=utf-8",
                type: 'GET',
                processResults: function (result) {
                    return {
                        results: $.map(result, function (item) {
                            return { id: item.id, text: item.city };
                        }),
                    };
                }
            }
        });
    });
</script>

我能搜索到一份城市列表

我希望主动搜索结果显示为城市+‘,’+州+‘+邮政编码(例如,洛杉矶,加利福尼亚州90001),并在 Select 后只显示城市(即洛杉矶).

我正在try 按照文档进行操作,但总是得到一个未定义的结果.

enter image description here

Here is the output from the console enter image description here

我只是不知道如何使用templating.

  <script>
    $(document).ready(function () {
        $("#citySelect").select2({
            theme: 'bootstrap-5',
            placeholder: "Search for your city",
            dropdownAutoWidth: true,
            width: 'auto',
            minimumInputLength: 3,
            allowClear: true,
            async: true,
            templateResult: function (item) {
                console.log(item);
                return item.city + ', ' + item.state + ' ' + item.zip;
            },
            templateSelection: function (item) {
                return item.city
            },
            ajax: {
                url:
                    function (params) {
                        return '@GatewaySettings.Value.Url' + '/api/location/cities/' + params.term;
                    },
                contentType: "application/json; charset=utf-8",
                type: 'GET',
                cache: true,
                processResults: function (result) {
                    return {
                        results: $.map(result, function (item) {
                            return { id: item.id, text: item.city };
                        }),
                    };
                }
            }
        });
    });
</script>

如有任何帮助,欢迎光临

添加后来自控制台的结果

console.log("RESULTS", JSON.stringify(result, null, 2));

RESULTS [
  {
    "id": 5039,
    "city": "Chandlers Valley",
    "state": "Pennsylvania",
    "zip": null
  },
  {
    "id": 14327,
    "city": "Chandlersville",
    "state": "Ohio",
    "zip": null
  },
  {
    "id": 15915,
    "city": "Chandler",
    "state": "Indiana",
    "zip": null
  },
  {
    "id": 19293,
    "city": "Chandler",
    "state": "Minnesota",
    "zip": null
  },
  {
    "id": 22001,
    "city": "Chandlerville",
    "state": "Illinois",
    "zip": null
  },
  {
    "id": 26294,
    "city": "Chandler",
    "state": "Oklahoma",
    "zip": null
  },
  {
    "id": 26686,
    "city": "Chandler",
    "state": "Texas",
    "zip": null
  },
  {
    "id": 29715,
    "city": "Chandler",
    "state": "Arizona",
    "zip": null
  },
  {
    "id": 29716,
    "city": "Chandler",
    "state": "Arizona",
    "zip": null
  },
  {
    "id": 29717,
    "city": "Chandler",
    "state": "Arizona",
    "zip": null
  }
]

推荐答案

你的$.map()大部分是多余的,也排除了你似乎想使用的属性.

您真正需要做的就是将数组嵌套在一个results属性下,这样Select2就可以使用它.

templateResult: ({ city, state, zip }) =>
  `${city}, ${state} ${zip ?? ""}`.trim(),
templateSelection: ({ city }) => city,
ajax: {
  url: ({ term }) =>
    "@GatewaySettings.Value.Url" +
    `/api/location/cities/${encodeURIComponent(term)}`, // encode the term
  processResults: (results) => ({ results }),
}

一些关于ajax个属性的说明...

  • 默认方法(类型)为GET
  • 默认的cache属性为true
  • GET请求没有内容,因此不需要contentType

Jquery相关问答推荐

Flask,如何为ajax调用返回成功状态码

在 CSS3 或 jQuery 中将元素的宽度从 0% 设置为 100% 动画,它和它的包装器只需要它们需要的宽度,没有预先设置的宽度

jQuery - 向下滚动时缩小的粘性标题

ASP.Net 母版页和文件路径问题

如何在 HTML选项标签上显示工具提示?

我可以使用 jQuery 打开下拉列表吗

未捕获的 TypeError:data.push 不是函数

jQuery - 多个 :not Select 器

使用 Jquery 更改页面标题

$(document).on("click"... 不工作?

JQuery - 如何根据值 Select 下拉项

使用 jQuery 获取 div 的可见高度

带有 jQ​​uery 的饼图

如何重新加载/刷新 jQuery 数据表?

更改 div 的内容 - jQuery

使用 jQuery Select 空文本输入

jQuery .each() 索引?

自调用函数前的分号?

如何使用 jQuery 或仅使用 Javascript 将按钮重定向到另一个页面

使用 Chosen 插件更改 Select 中的 Select