Search code examples
javascriptdojodojox.grid

Dojo: dojox.grid.enhanced.plugins.Filter - all items are cleared from the grid on filtering


I've got a dojox.grid.EnhancedGrid with a dojox.grid.enhanced.plugins.Filter and a dojo.store.Memory wrapped in a dojo.data.ObjectStore. Whenever I try to filter, all records are removed from the grid. I get a '0 of 0 items shown' message. When i click 'clear filter' the grid remains empty.

Update: The plot thickens. It seems that just sorting the grid by a column clears the grid. I'm trying to get a basic example working here: http://jsfiddle.net/wp64T/4/


Solution

  • I had the same problem and only managed to fix it by running the grid filter periodically in the background with the help of some jQuery. Here is some sample code; hope this helps someone else having problems with this.

    // ADD JQUERY
    <script src="http://code.jquery.com/jquery-latest.js"></script>
    

    .

    // PUT THIS IN THE <HEAD> OF THE PAGE
    <script type="text/javascript">
        $(document).ready(function() {
            function filterTheDataGrid() {
                if (dijit.byId("grid") != undefined) {
                    dijit.byId("grid").filter({color: "Red"});
                }
            }
        // RUN THE filterTheDataGrid FUNCTION EVERY ONE SECOND (1000 MILLISECONDS) //
        // LOWER '1000' FOR FASTER REFRESHING, MAYBE TO 500 FOR EVERY 0.5 SECOND REFRESHES //
        var refreshDataGrid = setInterval(function() {  filterTheDataGrid();    }, 1000);
        }
    </script>
    

    .

    // PUT THIS IN THE <HEAD> OF THE PAGE
    <script type="text/javascript">
        // SETUP THE LAYOUT FOR THE DATA //
        var layoutItems = [[
        {
            field: "id",
            name: "ID",
            width: '5px',
            hidden: true
        },
        {
            field: "color",
            name: "Color",
            width: '80px'
        }
    ]];
    
    // Create an empty datastore //
    var storeData = {
        identifier: 'id',
        label: 'id',
        items: []
    }
    var store3 = new dojo.data.ItemFileWriteStore( {data : storeData} );
    </script>
    

    .

     // PUT THIS IN THE <HTML> OF THE PAGE
    <div id="grid" dojoType="dojox.grid.DataGrid" jsId="grid5" store="store3" structure="layoutItems" query="{ type: '*' }" clientSort="true" rowsPerPage="40"></div>
    

    .

    <script type="text/javascript">
    function addItemToGrid(formdata) {
        // THIS FUNCTION IS CALLED BY A DIALOG BOX AND GETS FORM DATA PASSED TO IT //
        var jsonobj = eval("(" + dojo.toJson(formData, true) + ")");
    
        var myNewItem = {
            id: transactionItemID,
            color: jsonobj.color
        };
        // Insert the new item into the store:
        store3.newItem(myNewItem);
        store3.save({onComplete: savecomplete, onError: saveerror});
    }
    </script>