如何重新过滤 dojo DataGrid?

发布于 2024-10-31 12:37:46 字数 679 浏览 2 评论 0原文

我有一个已经使用 grid.filter(query, rerender) 进行过滤的 DataGrid。如果我添加另一个项目,在调用 save() 后,我会在网格中看到新项目,即使它由于过滤器而不应显示。我在想“好吧,当商店完成保存时,我会再次过滤它。但是在使用相同的查询调用 grid.filter 后,所有行都消失了。有什么想法我可能做错了吗?

过滤网格的代码:

var filterQuery = dijit.byId("filterTextbox").attr("value");
var grid = dijit.byId("grid");
var queryValue = "*";
if(filterQuery != ""){
    queryValue += filterQuery + "*";
}
grid.filter({name: queryValue}, true);

将新项目添加到网格的代码

function addItemToGrid(newItemName){
     var newItem = {name: newItemName};
     var grid = dijit.byId("grid");
     var store = grid.store;
     store.addItem(newItem);
     store.save();
}

I have a DataGrid that I already filtered using grid.filter(query, rerender). If I add another item, after calling save() I see the new item in the grid even though it shouldn't display because of the filter. I'm thinking "ok, I'll just filter it again when the store finishes saving. But after calling grid.filter with the same query all the rows disappear. Any ideas what I might be doing wrong?

Code to filter the grid:

var filterQuery = dijit.byId("filterTextbox").attr("value");
var grid = dijit.byId("grid");
var queryValue = "*";
if(filterQuery != ""){
    queryValue += filterQuery + "*";
}
grid.filter({name: queryValue}, true);

Code to add new items to the grid

function addItemToGrid(newItemName){
     var newItem = {name: newItemName};
     var grid = dijit.byId("grid");
     var store = grid.store;
     store.addItem(newItem);
     store.save();
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

白色秋天 2024-11-07 12:37:46

尝试使用:

store.newItem(newItem);

而不是 store.addItem(newItem);
(addItem 不是将商品添加到商店的标准方法)

Try to use:

store.newItem(newItem);

instead of store.addItem(newItem);
(addItem is not a standard method to add items into store)

遗失的美好 2024-11-07 12:37:46

在 addItemToGrid 函数内部,尝试将 onComplete 侦听器添加到您的 save 方法中,并在 onComplete 函数中对网格进行排序或过滤

store.save({onComplete: function() {
         grid.filter({name: queryValue}, true);
    }
});

Inside of your addItemToGrid function, try adding an onComplete listener to your save method and sort or filter the grid in the onComplete function

store.save({onComplete: function() {
         grid.filter({name: queryValue}, true);
    }
});
铁轨上的流浪者 2024-11-07 12:37:46

我遇到了同样的问题,只能通过运行来修复它借助一些 jQuery,网格过滤器会在后台定期进行过滤。这是一些示例代码;希望这可以帮助其他遇到此问题的人。

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

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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文