对 jQueryUI 自动完成源数据使用不同的 属性 名称
Using different property name for jQueryUI autocomplete source data
jQueryUI 自动完成需要如下来源:
[ { label: "Choice1", value: "value1" }, ... ]
对于我的应用程序,我的网络服务器正在使用 cURL 从 API 获取数据。 label
和 value
应该是一样的,所以我只需要两者之一。服务器 returns 一个 id
变量,我可以在 select
回调中访问它并且不会造成任何问题。最后,它没有使用 属性 名称 "label" 或 "value",而是使用 属性 名称 "name"。比如我的数据是:
[ { name: "Choice1", id: "1" }, ... ]
问题:我是否需要在将 API 数据发送到自动完成之前对其进行迭代,或者我可以将自动完成配置为使用 属性 名称 "name" 而不是 属性 姓名 "label" 或 "value"?
您可以使用 source
:
的函数来执行此操作
Function: The third variation, a callback, provides the most flexibility and can be used to connect any data source to Autocomplete. The callback gets two arguments:
A request
object, with a single term property, which refers to the value currently in the text input. For example, if the user enters "new yo" in a city field, the Autocomplete term will equal "new yo".
A response
callback, which expects a single argument: the data to suggest to the user. This data should be filtered based on the provided term, and can be in any of the formats described above for simple local data. It's important when providing a custom source callback to handle errors during the request. You must always call the response callback even if you encounter an error. This ensures that the widget always has the correct state.
一个例子:
$( ".selector" ).autocomplete({
source: function(req, resp){
// Get your curl data
}
});
如果您编辑您的问题并包含一些示例代码,我可以改进我对您的回答。
jQueryUI 自动完成需要如下来源:
[ { label: "Choice1", value: "value1" }, ... ]
对于我的应用程序,我的网络服务器正在使用 cURL 从 API 获取数据。 label
和 value
应该是一样的,所以我只需要两者之一。服务器 returns 一个 id
变量,我可以在 select
回调中访问它并且不会造成任何问题。最后,它没有使用 属性 名称 "label" 或 "value",而是使用 属性 名称 "name"。比如我的数据是:
[ { name: "Choice1", id: "1" }, ... ]
问题:我是否需要在将 API 数据发送到自动完成之前对其进行迭代,或者我可以将自动完成配置为使用 属性 名称 "name" 而不是 属性 姓名 "label" 或 "value"?
您可以使用 source
:
Function: The third variation, a callback, provides the most flexibility and can be used to connect any data source to Autocomplete. The callback gets two arguments:
A
request
object, with a single term property, which refers to the value currently in the text input. For example, if the user enters "new yo" in a city field, the Autocomplete term will equal "new yo".A
response
callback, which expects a single argument: the data to suggest to the user. This data should be filtered based on the provided term, and can be in any of the formats described above for simple local data. It's important when providing a custom source callback to handle errors during the request. You must always call the response callback even if you encounter an error. This ensures that the widget always has the correct state.
一个例子:
$( ".selector" ).autocomplete({
source: function(req, resp){
// Get your curl data
}
});
如果您编辑您的问题并包含一些示例代码,我可以改进我对您的回答。