Hi,
I have an OData Datasource for an Autocomplete Box. The textfield of this box contains entries which have an article number and article description as a combined string. Now I want to achieve if the user types in a numeric value, that it filters by startswith and if the user enters a string, that it gets filtered by contains.
Does anybody have an idea how to get this?
Thanks!
4 Answers, 1 is accepted
Hi,
finally got it working by myself. But as you can see, it seems, that there is a bug in the data.filter property. As i set the operator to "contains" it inserted substringof in the url (which is not supported by MS Webapi). Also the field and property were reversed, so I had to implement a little swap function:
parameterMap: function (data, type)
{
var value = data.filter.filters[1].value;
var pm;
if (value != undefined)
{
if (isNaN(value))
{
data.filter.filters[1].operator = "contains";
var swap = data.filter.filters[1].field;
data.filter.filters[1].field = "'" + data.filter.filters[1].value + "'";
data.filter.filters[1].value = swap;
pm = kendo.data.transports.odata.parameterMap(data);
pm.$filter = pm.$filter.replace("substringof", "contains").replace("'", '').replace("'", '');
}
else
{
data.filter.filters[1].operator = "startswith";
vm.$log.debug("startswith");
pm = kendo.data.transports.odata.parameterMap(data);
}
}
delete pm.$inlinecount;
delete pm.$format;
return pm;
}
Hello Benjamin,
We are glad that you have managed to achieve the functionality you are looking for.
Regarding the note about filter contains operator. It led me to believe you are using transport type set to odata instead of odata-v4 which is needed in order to connect to WebApi OData controller. The code which is calling the base parameterMap should call the odata-v4 parameterMap:
kendo.data.transports[
"odata-v4"
].parameterMap.call(
this
, data, type);
Rosen
Telerik
Hi,
thanks for your reply. Yes we already use odata-v4. Here is the complete code:
initArtikelACDataSource()
{
var vm = this;
this.ArtikelACDataSource = kendo.data.DataSource.create(
{
type: "odata-v4",
serverFiltering: true,
filter:
{
logic: "and",
filters:
[
{
field: "ArtNr",
operator: "lt",
value: 100000
}
]
},
transport:
{
read:
{
url: this.webApiServer + 'odata/vwLagerArtikel',
type: "GET",
dataType: "json",
beforeSend: (req) =>
{
req.setRequestHeader('Authorization', 'Bearer ' + this.token);
}
},
parameterMap: function (data, type)
{
var value = data.filter.filters[1].value;
var pm;
if (value != undefined)
{
if (isNaN(value))
{
data.filter.filters[1].operator = "contains";
var swap = data.filter.filters[1].field;
data.filter.filters[1].field = "'" + data.filter.filters[1].value + "'";
data.filter.filters[1].value = swap;
pm = kendo.data.transports.odata.parameterMap(data);
pm.$filter = pm.$filter.replace("substringof", "contains").replace("'", '').replace("'", '');
}
else
{
data.filter.filters[1].operator = "startswith";
pm = kendo.data.transports.odata.parameterMap(data);
}
}
delete pm.$inlinecount;
delete pm.$format;
return pm;
}
}
});
}
Hello Benjamin,
Although, you have set odata-v4 to the transport, you are still calling the odata transport's parameterMap. As I have mentioned in my previous message you should change the call to kendo.data.transports.odata.parameterMap(data) to kendo.data.transports["odata-v4"].parameterMap.call(this, data, type);
Regards,
Rosen
Telerik