DataGrid上的Dojo setQuery() - 所有项目都消失了?

时间:2011-11-17 18:39:16

标签: dojo dojox.grid dojox.grid.datagrid

我绞尽脑汁做了很多研究和测试,无法弄清楚发生了什么。

我有一个Dojo数据网格,它通过一些HTML静态声明。使用GUI,我的用户将向DataGrid添加项目,它可以正常工作。但是,我希望在某个点调用一个函数,该函数使用Dojo的setQuery来过滤DataGrid中显示的数据。问题是,一旦我运行setQuery命令,网格中的所有数据都会消失,无论它是否与查询匹配!

以下是一些示例代码:

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

...

<div id="grid" dojoType="dojox.grid.DataGrid" jsId="grid5" store="store3" structure="layoutItems" queryOptions="{deep:true}" query="{}" rowsPerPage="40"></div>

...

function filterGrid() {
    dijit.byId("grid").setQuery({color:"Red"});
}

...

function addItemToGrid(formdata) {
    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});
}

2 个答案:

答案 0 :(得分:0)

通过在一些jQuery 的帮助下在后台定期运行网格FILTER而不是setQuery来管理修复它(不确定setQuery是否也能正常工作,我真的不知道它之间的区别filter和setQuery,但filter正在做我需要做的事情)

这是一些示例代码;希望这有助于其他人遇到此问题:

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

答案 1 :(得分:0)

这是我提出的另一个选项,因此过滤器每x毫秒不会不必要地运行;这基本上使用JavaScript来创建一个新的setInterval,它在500毫秒后运行一次,然后执行clearInterval以便它不再运行。看起来只是在添加了一个不知道的项目之后调用了filterTheDataGrids()函数..我们必须延迟一秒钟,然后调用它:

// PUT THIS IN THE <HEAD> OF THE PAGE
<script type="text/javascript">
    // Declare the global variables
    var refreshDataGrid;
    var refreshDataGridInterval = 500;    // Change this as necessary to control how long to wait before refreshing the Data Grids after an item is added or removed.
</script>

// PUT THIS IN THE <HEAD> OF THE PAGE
<script type="text/javascript">
    function filterTheDataGrids() {
         if (dijit.byId("grid") != undefined) {
              dijit.byId("grid").filter({color: "Red"});
         }
         clearInterval (refreshDataGrid);    // Running the filter just once should be fine; if the filter runs too quickly, then make the global refreshDataGridInterval variable larger
    }
</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});

    // Create setInterval on the filterTheDataGrids function; since simple calling the function won't do; seems to call it too fast or something
    refreshDataGrid = setInterval(function() {  filterTheDataGrids();   }, refreshDataGridInterval);
}
</script>