在 Telerik 论坛上询问后,显然 Telerik jQuery 框架中没有提供这样做的方法。所以,我继续实施了一个。
如果有人好奇,这里是jsfiddle 的链接。它只支持过滤字符串和排序字符串,因为我还没有用于解析组字符串。
过滤字符串:
function parseFilterString(filterString) {
// sample filter: "(Auditor~startswith~'Gabe'~and~Auditor~endswith~'Newell')~and~(Company~contains~'Valve'~and~Company~neq~'EA')";
// Remove all of the ' characters from the string.
filterString = filterString.replace(/[']/g, '');
// Split the string into an array of strings, using the ~ as a delimiter.
var ss = filterString.split("~"); // ss stands for "split string". I'm clever.
var F = []; // Used to store all of the parsed filters.
var fIndex = -1; // Used to track filter index.
var cIndex = 0; // Used to track filter index within a composite filter object.
var isComposite = false; // Used to indicate if a composite filter is currently being parsed.
for (var i = 0; i < ss.length; i++) {
if (i % 4 == 0) { // Field.
if (ss[i].indexOf('(') > -1) { // If we're starting a composite object, create a composite object and add it to the parsed filters.
F.push({
filters: [],
logic: ""
});
fIndex++; // We added an object to the array, so increment the counter.
F[fIndex]
F[fIndex].filters.push({
field: ss[i].replace('(', ''),
operator: "",
value: ""
});
cIndex = 0; // We added the first filter to the composite object, so set the counter.
isComposite = true;
}
else if (isComposite) { // If we're parsing the second filter in a composite filter object, then add the field to the child filter.
F[fIndex].filters.push({
field: ss[i],
operator: "",
value: ""
});
cIndex++; // We added the second filter to the composite object, so increment the counter.
}
else { // Add the field as normal.
F.push({
field: ss[i],
operator: "",
value: ""
});
fIndex++; // We added an object to the array, so increment the counter.
}
}
if (i % 4 == 1) { // Operator.
if (isComposite) {
F[fIndex].filters[cIndex].operator = ss[i];
}
else {
F[fIndex].operator = ss[i];
}
}
if (i % 4 == 2) { // Value.
if (ss[i].indexOf(')') > -1) {
F[fIndex].filters[cIndex].value = ss[i].replace(')', '');
isComposite = false;
}
else if (isComposite) {
F[fIndex].filters[cIndex].value = ss[i];
}
else {
F[fIndex].value = ss[i];
}
}
if (i % 4 == 3) { // Logic.
if (isComposite) {
F[fIndex].logic = ss[i]; // Add the logic to the composite filter object.
}
// If the filter is not composite, the logic will always be "and". So, we just don't do anything if that's the case.
}
}
return {
filters: F,
logic: "and"
};
};
排序字符串:
function parseSortString(sortString) {
// sample multi-level sort: "Auditor-asc~Company-desc~Invoice-asc";
// Split the string into an array of strings, using the ~ as a delimiter.
var ss = sortString.split("~"); // ss stands for "split string". I'm clever.
var S = []; // Array containing sort objects.
for (var i = 0; i < ss.length; i++) {
var sort = ss[i].split('-'); // Split sort string into field and direction.
S.push({
compare: undefined, // This field exists in the sort objects, but is always undefined (as far as I can tell). I added it anyways, to minimize potential future issues.
dir: sort[1], // Direction.
field: sort[0] // Field.
});
}
return S;
};