
I've got a page that displays courses, which can have tags. For example, my "Javascript" course may have the tag "web". I want to filter the datasource based on the course tags, but if no tag value is passed through (ie. blank string), I want it to ignore the filter.
Right now what's happening is that passing through a "eq" filter w/ an empty string is pulling back only courses that don't have tags - which does make sense. But if I don't pass through a specific tag, I need to get all courses, tagged or not. I also can't switch from "eq", since it has to be a full match, if a tag is passed in.
Appreciate any help here.
9 Answers, 1 is accepted
How are you filtering the dataSource? If you are using the filter method then you could check if the value is empty string and clear the filter.
Regards,
Daniel
Telerik

I've got it set up on the datasource config right now:
filter: {
field:
"Tag"
,
operator:
"eq"
,
value: filter_field
}
You can also use a condition in this case:
filter: filter_field ? {
field:
"Tag"
,
operator:
"eq"
,
value: filter_field
} : []
Regards,
Daniel
Telerik


Is it possible to do the same check w/ multiple filters? I've tried this:
filter: {
logic:
"and"
,
filters: [
filter_val ? {
field:
"Tag"
,
operator:
"eq"
,
value: filter_val
} : [],
search ? {
field:
"Name"
,
operator:
"contains"
,
value: search
} : []
]
}
Where "search" is the value of a search field (ie. looking for matches in a huge list based on an item's name), but it always returns zero results (the function populating the datasource does return data, so I know it's the filter).
You should check the values before adding them to the array:
var
filter = {
logic:
"and"
,
filters: []
};
if
(filter_val) {
filter.filters.push({
field:
"Tag"
,
operator:
"eq"
,
value: filter_val
});
}
if
(search) {
filter.filters.push({
field:
"Name"
,
operator:
"contains"
,
value: search
});
}
The filters array should contain only valid filters.
Regards,
Daniel
Telerik


I am not sure if I understand the question but a condition is used in both cases:
- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/if...else
- https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Operators/Conditional_Operator
var
filter = {
logic:
"and"
,
filters: []
};
if
(filter_val) {
filter.filters.push({
field:
"Tag"
,
operator:
"eq"
,
value: filter_val
});
}
if
(search) {
filter.filters.push({
field:
"Name"
,
operator:
"contains"
,
value: search
});
}
var
dataSource =
new
kendo.DataSource({
filter: filter,
...
})
Regards,
Daniel
Telerik