Kendo UI - 在读取数据源时指定参数名称 [英] Kendo UI - Specify parameter name on dataSource read
问题描述
使用Kendo UI,我使用自动完成框尝试从我的服务器检索数据.它正在访问具有以下签名的 ASP.NET MVC 控制器.
With Kendo UI, I am using an autocomplete box to try and retrieve data from my server. It is hitting an ASP.NET MVC controller with the following signature.
public ActionResult aspect(string term){
// ...
}
这意味着请求需要在 url 中有正确的参数.现在我遇到的问题是我找不到在 dataSource 机制中指定它的方法.我已经阅读了关于 parameterMap 的文档数十次,但它对我来说完全没有任何意义.
This means that the request needs to have the correct parameter in the url. Now the issue I am running into is that I cannot discover a way to specify this in the dataSource mechanics. I have read the documentation on parameterMap dozens of times and it makes absolutely no sense to me in any way.
由于有问题的页面实际上在任何时候都有 10-15 个自动完成文本框,每个文本框都使用动态标识动态创建,这一事实使情况变得更加复杂.
This is complicated further by the fact that the page in question actually has 10-15 autocomplete text boxes at any one time, each dynamically created with dynamic identity.
我目前使用的代码如下;
The code I am using so far is as follows;
$(".autocomplete").kendoAutoComplete({
dataTextField: "Name",
dataSource: {
type: "json",
transport: {
read: {
url: "/search/aspect"
}
}
}
});
那么我能告诉它如何命名它传递的参数吗?
So is there anything I can do to tell it how to name the parameter it passes?
为了更清楚我想要做什么,如果我在 jQuery 中这样做,我会使用 ...
To make it more clear what I am trying to do, if I were doing this in jQuery, I would use ...
$.ajax({ url: '/search/aspects', data: { term: (insert the data here) } });
但由于所有这些工作方式,没有设置选择器"来获取自动完成输入,因此我无法从输入表单元素中检索其值.
But because of the way all of this works, there is no set "selector" to get the autocomplete input, so I cannot retrieve its value from the input form element.
推荐答案
首先,通过设置这个选项来启用服务器端过滤:
First, enable server-side filtering by setting this option:
dataSource: {
serverFiltering: true,
然后将该值作为参数之一传递到 transport.parameterMap
函数中.
Then the value is passed as one of the parameters into the transport.parameterMap
function.
如果你像这样记录传入参数映射函数的对象:
If you were to log the object passed in to the parameterMap function like this:
$(".autocomplete").kendoAutoComplete({
dataTextField: "Name",
dataSource: {
serverFiltering: true,
type: "json",
transport: {
read: {
url: "/search/aspect"
},
parameterMap: function (data, action) {
console.log(data);
}
}
}
});
然后你会得到一个看起来像这样的对象:
then you would get an object that looks like this:
{
"filter":{
"logic":"and",
"filters":[
{
"value":"something",
"operator":"contains",
"field":"Name",
"ignoreCase":true
}
]
}
}
因此,您可以通过执行以下操作来使用它来获取输入到自动完成框中的值:
So you can use this to get the value entered into the AutoComplete box by doing:
$(".autocomplete").kendoAutoComplete({
dataTextField: "Name",
dataSource: {
serverFiltering: true,
type: "json",
transport: {
read: {
url: "/search/aspect"
},
parameterMap: function (data, action) {
if(action === "read") {
return {
term: data.filter.filters[0].value
};
} else {
return data;
}
}
}
}
});
这篇关于Kendo UI - 在读取数据源时指定参数名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!