Infragistics 使用 igTextEditor 过滤数字

Infragistics filtering numbers using igTextEditor

我想过滤一个实际上包含数字但数字以 2MM、1K 等格式显示的列。当用户想要过滤该列中的数据时,我更愿意给他选择使用显示的格式 e.x 进行搜索。 2MM 并且不使用放置尾随零的实际数字。为了实现这一点,我使用了一个未绑定的列和一个隐藏的列,并将数据类型设置为字符串。我用 css 中的数字过滤器替换了字符串过滤器,但是当我尝试使用 'greaterThanOrEqualTo' 调用过滤器方法时,我得到以下异常 infragistics.datasource.js:36 Uncaught Error: The传递的筛选条件未被识别:greaterThanOrEqualTo。有什么办法可以克服这个问题吗?

<!DOCTYPE html>
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title>Ignite UI sample</title>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js" type="text/javascript"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.11/jquery-ui.min.js" type="text/javascript"></script> 
<script src="http://cdn-na.infragistics.com/igniteui/2014.2/latest/js/infragistics.loader.js"></script>

<script type="text/javascript">
var data = [];
for (var i = 0; i < 100; i++) {
    //data.push({ "type": "Type " + i, "child": { "name": "child " + i, "age": i}});
    //data.push({ "type": "Type " + i, "child": { "name": "child " + i}});
    data.push({ "type": "Type " + i, "child": i});
}
$.ig.loader({
    scriptPath: "http://cdn-na.infragistics.com/igniteui/2014.2/latest/js/",
    cssPath: "http://cdn-na.infragistics.com/igniteui/2014.2/latest/css/",
    resources: "igGrid.Filtering,igCombo"
});

$.ig.loader(function () {
    $("#grid1").igGrid({
        width: "700px",
        autoCommit: true,
        renderCheckboxes: true,
        columns: [
            { headerText: "type", key: "type", dataType: "string"},
            { headerText: "child", key: "child", dataType: "object", hidden: true, formatter: function(val) { 
            if(val === 0) 
                return "zero";
            else if(val === 1) 
                return "one";
            else if(val === 2)
                return "two";
            else if(val === 3)
                return "three";
            else 
                return "four";
            }},
            { headerText: "child", key: "child_name", unbound: true, dataType: "string", formula: function (row) {
                    return row.child;
            }, formatter: function(val) { 
            if(val == 1) 
                return "one";
            else if(val === 2)
                return "two";
            else if(val === 3)
                return "three";
            else 
                return "four";
            }}
        ],
        autoGenerateColumns: false,
        dataSource: data,
        height: "300px",
        features: [
            {                   
                name: "Filtering",
                dataFiltering: handler,
                //dropDownClosing: setSelectedFilter,
                columnSettings: [
                    { 
                        columnKey: "child_name", 
                        allowFiltering: true,

                    } 
                ]
            }
        ]
    });


    $('div#grid1_dd_child_name ul').replaceWith("<ul class='ui-menu ui-widget ui-widget-content ui-iggrid-filterddlist ui-corner-all'><li class='ui-iggrid-filterddlistitemicons ui-state-default'><span class='ui-iggrid-filtericoncontainer'><span class='ui-iggrid-filtericon ui-iggrid-filtericonclear'></span></span><span class='ui-iggrid-filterddlistitemcontainer'> Clear Filter </span></li><li class='ui-iggrid-filterddlistitemicons ui-state-default'><span class='ui-iggrid-filtericoncontainer'><span class='ui-iggrid-filtericon ui-iggrid-filtericonequals'></span></span><span class='ui-iggrid-filterddlistitemcontainer'> Equals </span></li><li class='ui-iggrid-filterddlistitemicons ui-state-default'><span class='ui-iggrid-filtericoncontainer'><span class='ui-iggrid-filtericon ui-iggrid-filtericondoesnotequal'></span></span><span class='ui-iggrid-filterddlistitemcontainer'> Does not equal </span></li><li class='ui-iggrid-filterddlistitemicons ui-state-default'><span class='ui-iggrid-filtericoncontainer'><span class='ui-iggrid-filtericon ui-iggrid-filtericongreaterthan'></span></span><span class='ui-iggrid-filterddlistitemcontainer'> Greater than </span></li><li class='ui-iggrid-filterddlistitemicons ui-state-default'><span class='ui-iggrid-filtericoncontainer'><span class='ui-iggrid-filtericon ui-iggrid-filtericonlessthan'></span></span><span class='ui-iggrid-filterddlistitemcontainer'> Less than </span></li><li class='ui-iggrid-filterddlistitemicons ui-state-default'><span class='ui-iggrid-filtericoncontainer'><span class='ui-iggrid-filtericon ui-iggrid-filtericongreaterthanorequalto'></span></span><span class='ui-iggrid-filterddlistitemcontainer'> Greater than or equal to </span></li><li class='ui-iggrid-filterddlistitemicons ui-state-default'><span class='ui-iggrid-filtericoncontainer'><span class='ui-iggrid-filtericon ui-iggrid-filtericonlessthanorequalto'></span></span><span class='ui-iggrid-filterddlistitemcontainer'> Less than or equal to </span></li></ul>");
    $('tr[data-role="filterrow"] td:nth-child(2) span input').replaceWith("<input class='ui-igedit-field ui-iggrid-filtereditor' style='text-align: left; height: 23px; width: 317px;'>")


    $('tr[data-role="filterrow"] td:nth-child(2) span input').igTextEditor({
        width: 200, 
        nullText: "Equals...",
        valueChanged: equals
    });

    $('#grid1_dd_child_name ul li').click(function() {

        var text = $(this).text();
        if(text === ' Greater than or equal to ') {
            var cond = 'greaterThanOrEqualTo';
            $('tr[data-role="filterrow"] td:nth-child(2) span input').igTextEditor({
                width: 200, 
                nullText: text + '...',
                valueChanged: cond
            });
            $("#grid1").igGridFiltering("filter", ([{fieldName: "child_name", expr: 1, cond: 'greaterThanOrEqualTo'}]));    
        } else  if(text === ' Clear Filter ') {
            var cond = 'greaterThanOrEqualTo';
            $('tr[data-role="filterrow"] td:nth-child(2) span input').igTextEditor({
                width: 200, 
                nullText: text + '...',
                valueChanged: cond
            });
        }       
    });

    });

function equals(e, args) {
    if (args.value !== null) {
        if(args.value == "one") 
            args.value = 1;
        else if(args.value === "two")
            args.value = 2;
        else if(args.value === "three")
            args.value = 3;
        else 
            args.value = 4;

        var selectedValue = args.value;
        $("#grid1").igGridFiltering("filter", ([{fieldName: "child_name", expr: selectedValue, cond: "greaterThanOrEqualTo"}]));
    } else {
        $("#grid1").igGridFiltering("filter", ([{fieldName: "child_name", expr: "", cond: "equals"}]));
    }
};

function handler(event, args) {
    if(args.columnKey === "child_name" && args.newExpressions.length !== 0) {
    if(args.newExpressions[0].expr === "one")
        args.newExpressions[0].expr = 1;
    }
}

</script>
</head>
<body>
<table id="grid1">
</table>
</body>
</html>

此代码是虚拟的,用于检查是否可以实现所描述的功能。

您看到此错误是因为条件 'greaterThanOrEqualTo' 不适用于 string 数据类型。

我可以建议你的方法是不要使用未绑定的列,而是拦截过滤操作并转换过滤表达式。这种方法也会保留数字数据类型过滤条件。

步骤如下:

  1. 更改过滤器编辑器类型,以便用户可以键入字母字符。这是在 igGrid.rendered 事件中完成的,如下所示:

代码:

ui.owner.headersTable().find(".ui-iggrid-filterrow td.ui-iggrid-filtercell:eq(1)>span.ui-igedit").igEditor("option", "type", "text");
  1. 处理igGridFiltering.dataFiltering事件并修改保存过滤表达式的ui.newExpressions变量。

代码如下:

function handler(event, ui) {
    for (var i = 0; i < ui.newExpressions.length; i++) {
        if (ui.newExpressions[i].fieldName === "child") {
            ui.newExpressions[i].expr = getExpr(ui.newExpressions[i].expr);
        }
    }
}

function getExpr(val) {
    if(val == "one") 
        return 1;
    else if(val === "two")
        return 2;
    else if(val === "three")
        return 3;
    else 
        return 4;
}

整个页面:

<!DOCTYPE html>
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title>Ignite UI sample</title>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js" type="text/javascript"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.11/jquery-ui.min.js" type="text/javascript"></script> 
<script src="http://cdn-na.infragistics.com/igniteui/2014.2/latest/js/infragistics.loader.js"></script>

<script type="text/javascript">
var data = [];
for (var i = 0; i < 100; i++) {
    //data.push({ "type": "Type " + i, "child": { "name": "child " + i, "age": i}});
    //data.push({ "type": "Type " + i, "child": { "name": "child " + i}});
    data.push({ "type": "Type " + i, "child": i});
}
$.ig.loader({
    scriptPath: "http://cdn-na.infragistics.com/igniteui/2014.2/latest/js/",
    cssPath: "http://cdn-na.infragistics.com/igniteui/2014.2/latest/css/",
    resources: "igGrid.Filtering,igCombo"
});

$.ig.loader(function () {
    $("#grid1").igGrid({
        width: "700px",
        autoCommit: true,
        renderCheckboxes: true,
        columns: [
            { headerText: "type", key: "type", dataType: "string"},
            { headerText: "child", key: "child", dataType: "number", hidden: false, formatter: function(val) { 
            if(val === 0) 
                return "zero";
            else if(val === 1) 
                return "one";
            else if(val === 2)
                return "two";
            else if(val === 3)
                return "three";
            else 
                return "four";
            }}
        ],
        autoGenerateColumns: false,
        dataSource: data,
        height: "300px",
        features: [
            {                   
                name: "Filtering",
                dataFiltering: handler
            }
        ],
        rendered: function (evt, ui) {
            // this will work only for versions up to 15.1
            ui.owner.headersTable().find(".ui-iggrid-filterrow td.ui-iggrid-filtercell:eq(1)>span.ui-igedit").igEditor("option", "type", "text");
        }
    });
});

function handler(event, ui) {
    for (var i = 0; i < ui.newExpressions.length; i++) {
        if (ui.newExpressions[i].fieldName === "child") {
            ui.newExpressions[i].expr = getExpr(ui.newExpressions[i].expr);
        }
    }
}

function getExpr(val) {
    if(val == "one") 
        return 1;
    else if(val === "two")
        return 2;
    else if(val === "three")
        return 3;
    else 
        return 4;
}
</script>
</head>
<body>
<table id="grid1">
</table>
</body>
</html>