Jquery 将剑道网格筛选器字符串解析为筛选器对象

Jquery 将剑道网格筛选器字符串解析为筛选器对象,jquery,kendo-ui,telerik,kendo-grid,datasource,Jquery,Kendo Ui,Telerik,Kendo Grid,Datasource,我创建了一个排序的伪网格小部件(基本上只是一个网格头),允许用户根据自己的喜好配置排序和过滤器,然后保存这些配置以备将来使用。问题是它们是作为字符串从数据库返回的。不幸的是,我需要一种将它们转换回javascript对象的方法,以便将它们应用到网格中 我正在使用下面的parameterMap函数将DataSourceRequest对象转换为可以发布到控制器操作并保存在数据库中的内容。该函数只获取从kendoGrid.dataSource返回的对象,并将它们转换为排序查询字符串。那么这个,

我创建了一个排序的伪网格小部件(基本上只是一个网格头),允许用户根据自己的喜好配置排序和过滤器,然后保存这些配置以备将来使用。问题是它们是作为字符串从数据库返回的。不幸的是,我需要一种将它们转换回javascript对象的方法,以便将它们应用到网格中

我正在使用下面的parameterMap函数将DataSourceRequest对象转换为可以发布到控制器操作并保存在数据库中的内容。该函数只获取从
kendoGrid.dataSource
返回的对象,并将它们转换为排序查询字符串。那么这个,

    // Get datasource of the filter grid, so we can save the search applied to it.
    var f = $("#filter-grid").data("kendoGrid").dataSource;

    // Set the filtering/sorting applied to the filter grid in the model being saved.
    e.model.Request = e.sender.dataSource.transport.parameterMap({
        filter: f.filter(),
        group: f.group(),
        page: f.page(),
        pageSize: f.pageSize(),
        sort: f.sort()
    });
返回以下内容:

    Object {
        sort: "InvoiceNumber-asc", 
        page: 1, 
        pageSize: 100, 
        group: "", 
        filter: "Auditor~startswith~'Gabe'~and~Auditor~endswith~'Newell'"
    }
KendoUI中是否提供了允许将查询字符串解析为javascript对象的javascript函数?我基本上想反转
parameterMap()
函数的结果。我的最终目标是避免将我自己解析这些字符串的方法回滚到对象中,而剑道套件中的某些东西似乎应该为我解决这个问题

Telerik文档确实提供了一种方法来精确地执行我要查找的内容(),但这在控制器中是可用的,我需要在页面上完成。如果我能找到类似于他们基于jQuery的框架中提供的MVC扩展的东西,那么实现起来就容易多了


提前感谢。

在Telerik论坛上询问后,显然Telerik jQuery框架中没有提供这样做的方法。所以,我开始实施了一个

如果有人好奇的话,这里有一个链接。它只支持过滤字符串和排序字符串,因为我还没有解析组字符串的功能

筛选字符串:

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"
        };
    };
函数parseFilterString(filterString){
//样本过滤器:“(Auditor~startswith~'Gabe'~和~Auditor~endswith~'Newell')~和~(Company~包含~'Valve'~和~Company~neq~'EA');
//从字符串中删除所有的字符。
filterString=filterString.replace(/[']/g',);
//使用~作为分隔符,将字符串拆分为字符串数组。
var ss=filterString.split(“~”)//ss代表“split string”。我很聪明。
var F=[];//用于存储所有已解析的筛选器。
var fIndex=-1;//用于跟踪筛选器索引。
var cIndex=0;//用于跟踪复合筛选器对象中的筛选器索引。
var isComposite=false;//用于指示当前是否正在分析复合筛选器。
对于(变量i=0;i-1){//如果我们要启动一个复合对象,请创建一个复合对象并将其添加到已解析的过滤器中。
推({
过滤器:[],
逻辑:“”
});
fIndex++;//我们向数组中添加了一个对象,因此增加了计数器。
F[fIndex]
F[fIndex].filters.push({
字段:ss[i]。替换(“(”,“),
操作员:“,
值:“”
});
cIndex=0;//我们将第一个筛选器添加到复合对象中,因此设置计数器。
isComposite=true;
}
否则,如果(isComposite){//如果我们正在分析复合过滤器对象中的第二个过滤器,则将该字段添加到子过滤器中。
F[fIndex].filters.push({
字段:ss[i],
操作员:“,
值:“”
});
cIndex++;//我们将第二个过滤器添加到复合对象中,因此递增计数器。
}
else{//按常规添加字段。
推({
字段:ss[i],
操作员:“,
值:“”
});
fIndex++;//我们向数组中添加了一个对象,因此增加了计数器。
}
}
如果(i%4==1){//运算符。
if(isComposite){
F[fIndex].filters[cIndex].operator=ss[i];
}
否则{
F[fIndex].运算符=ss[i];
}
}
如果(i%4==2){//值。
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];
}
否则{
F[fIndex].value=ss[i];
}
}
如果(i%4==3){//逻辑。
if(isComposite){
F[fIndex].logic=ss[i];//将逻辑添加到复合筛选器对象。
}
//如果过滤器不是复合的,那么逻辑总是“and”。因此,如果是这种情况,我们什么也不做。
}
}
返回{
过滤器:F,
逻辑:“和”
};
};
排序字符串:

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;
    };
函数parseSortString(sortString){
//示例多级排序:“审核员asc~公司描述~发票asc”;
//使用~作为分隔符,将字符串拆分为字符串数组。
var ss=sortString.split(“~”)//ss代表“split string”。我很聪明。
var S=[];//包含排序对象的数组。
对于(变量i=0;iif (filter == "") {
    return {};
}


if (filter.indexOf("(")  == -1) {
    filter = "(" + filter + ")";
}


var pattern = /(\()([^\(\)]+)(\))/g;
//var pattern = /(.*)/g;
var filterSub = { logic: null, filters: [] };
var filterObject = {};


function parser() {
    var matchParam = filter.match(pattern);
    if (matchParam != null) {

        matchParam.forEach(function (e) {

            var item = e.split('(').join('').split(')').join('');
            var logic = item.indexOf("~or~") > -1 ? "or" : "and";
            var items = item.indexOf("~or~") > -1 ? item.split('~or~') : item.split('~and~');
            filterSub = { logic: null, filters: [] };
            items.forEach(function (c, i) {
                var obj = filterObject[c];
                if (obj == null) {
                    var cArr = c.split("~");
                    obj = {
                        field: cArr[0],
                        operator: cArr[1],
                        value: (cArr[2] || "").split("'").join("")
                    };
                }

                if (items.length == 1) {
                    filterSub = obj;
                } else if (items.length >= 2) {
                    if (filterSub.filters.length == 2) {
                        filterSub = {
                            logic: logic,
                            filters: [obj, filterSub]
                        }
                    } else {
                        filterSub.logic = logic;
                        filterSub.filters.push(obj);
                    }
                } 
            });

            var length = Object.keys(filterObject).length;
            var name = "####" + length;
            filterObject[name] = filterSub;
            filter = filter.replace(e, name);
        });

        if (matchParam.length > 0) {
            parser();
        }
    }
};


parser();

return filterSub;