javascriptinfragisticsignite-uiiggrid

Infragistics filtering numbers using igTextEditor


I want to filter a column which is actually contains numbers but the numbers are displayed with this format 2MM, 1K, etc. When the user wants to filter the data in that column I would prefer to give him the option to search using the displayed format e.x. 2MM and not using the actual number putting trailing zeros. In order to achieve this I used an unbound column along with a hidden column and I have set the datatype to string. I replaced the string filters with the numeric filters in css, but when I try to call the filter method with the 'greaterThanOrEqualTo' for example i get the following exception infragistics.datasource.js:36 Uncaught Error: The filter condition that was passed was not recognized: greaterThanOrEqualTo. Is there any way to overcome this issue?

<!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>

This code is dummy used to check if the described functionality can be achieved.


Solution

  • You see this error because the condition 'greaterThanOrEqualTo' is not applicable for a string data type.

    What I can suggestion you as an approach is not to use an unbound column, but intercept the filtering operation and convert the filter expression. This approach will also keep the number data type filtering conditions.

    Here are the steps:

    1. Change the filter editor type, so that the user can type alphabet characters. This is done in the igGrid.rendered event like this:

    Code:

    ui.owner.headersTable().find(".ui-iggrid-filterrow td.ui-iggrid-filtercell:eq(1)>span.ui-igedit").igEditor("option", "type", "text");
    
    1. Handle the igGridFiltering.dataFiltering event and modify the ui.newExpressions variable which holds the filtering expressions.

    Here is the code:

    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;
    }
    

    And the whole page:

    <!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>