3

首先,我使用的是 dojo 1.1.0 和 dgrid 0.4.0。我正在创建一个带有一些 dgrids 的页面。一个网格尤其需要从 url 加载 JSON 数据并显示它。目前使用 RequestMemory 存储可以正常工作。然而,这是一个“记忆”存储。虽然这对其他一些人来说可能是微不足道的,但我需要找到一种方法来加载数据,因为它现在正在完成,然后在屏幕上添加一个刷新按钮,调用必要的函数/方法来从 url 重新加载数据和重新填充 dgrid。

JSON 数据来自格式如下的 url:

[{"id":1,"name":"trans1","username":"trans1","status":"Available","inactive":"no","checkedout":"false","details":"","assignedNum":"0","completedNum":"0","avgTime":"00:00","aaOn":true},{"id":2,"name":"trans2","username":"trans2","status":"Available","inactive":"no","checkedout":"false","details":"","assignedNum":"0","completedNum":"0","avgTime":"00:00","aaOn":true},{"id":3,"name":"trans3","username":"trans3","status":"Available","inactive":"no","checkedout":"false","details":"","assignedNum":"0","completedNum":"0","avgTime":"00:00","aaOn":false},{"id":4,"name":"trans4","username":"trans4","status":"Available","inactive":"no","checkedout":"false","details":"","assignedNum":"0","completedNum":"0","avgTime":"00:00","aaOn":false},{"id":5,"name":"trans5","username":"trans5","status":"Available","inactive":"no","checkedout":"false","details":"","assignedNum":"0","completedNum":"0","avgTime":"00:00","aaOn":false},{"id":6,"name":"trans6","username":"trans6","status":"Available","inactive":"no","checkedout":"false","details":"","assignedNum":"0","completedNum":"0","avgTime":"00:00","aaOn":false}]

您将在下面看到的 Require 实际上包含的不仅仅是这个网格......因此还有更多。

我不需要更新本地商店,也不想监视 url 的更改。我只是希望能够按需从 url 重新加载数据。

这是我目前用于初始加载网格(工作)的代码和我需要开始工作的刷新按钮。

require([ 'dojo/store/Observable' ,'dijit/Dialog', 'dijit/form/Select', 'dijit/form/MultiSelect', 'dijit/form/TextBox', 'dijit/layout/TabContainer', 'dijit/layout/ContentPane','dojo/request', 'dojo/request/xhr', 'dijit/form/ToggleButton', 'dijit/form/Button', 'dojo/parser', 'dojo/_base/declare', 'dgrid/Grid', 'dgrid/Selection', 'dgrid/Editor', 'dgrid/extensions/ColumnHider', 'dgrid/extensions/Pagination', 'dstore/RequestMemory', 'dijit/form/Select', 'dijit/registry','dojox/data/XmlStore', 'dojo/domReady!'], function (Observable, Dialog, Select, MultiSelect, TextBox, TabContainer, ContentPane, request, xhr, ToggleButton, Button, parser, declare, Grid, Selection, Editor, ColumnHider, Pagination, RequestMemory, Select, registry, XmlStore) {


//workers dgrid
    var workersStore = new RequestMemory({ target: '/autoAssign/getWorkers.aa?TASKTYPE=transport&INACTIVE=FALSE' });        
    var workerGrid = new (declare([ Grid, Selection, Pagination, Editor, ColumnHider ]))({
        collection: workersStore,
        className: 'dgrid-autoheight',
        id: 'workerGrid',
        rowsPerPage: 6,
        columns: {
            name: 'Name',
            username: {
                label: 'username',
                hidden: true
            },
            status: 'Status',
            assignedNum: 'Assigned',
            completedNum: 'Completed',
            avgTime: 'Average',
            aaOn: {
		      label: 'Auto Assign',
		      editor: 'checkbox',
                  }
        }
    }, 'gridNode');
    workerGrid.on("dgrid-datachange",function(evt) {
       var row = workerGrid.row(evt); 
       if (evt.cell.column.id == 'aaOn') {
           var promise = request('/autoAssign/setUserAaStatus.aa?USERNAME='+row.data.username+'&TASKTYPE=transport&STATUS='+evt.value);
       }
    });
    workerGrid.startup();


//Add refresh Button
      var addRefreshButton = new Button({
        label: "Refresh",
        style: 'float:right;',
        onClick: function(){
           var promise = workersStore.fetch();
           var result = promise.then(function(data){
               workerGrid.set("collection", workersStore);
               workerGrid.refresh();
               alert("refresh pushed");
           });
        }
    }, "refresh").startup(); 


}

[{"id":1,"name":"trans1","username":"trans1","status":"Available","inactive":"no","checkedout":"false","details":"","assignedNum":"0","completedNum":"0","avgTime":"00:00","aaOn":true},{"id":2,"name":"trans2","username":"trans2","status":"Available","inactive":"no","checkedout":"false","details":"","assignedNum":"0","completedNum":"0","avgTime":"00:00","aaOn":true},{"id":3,"name":"trans3","username":"trans3","status":"Available","inactive":"no","checkedout":"false","details":"","assignedNum":"0","completedNum":"0","avgTime":"00:00","aaOn":false},{"id":4,"name":"trans4","username":"trans4","status":"Available","inactive":"no","checkedout":"false","details":"","assignedNum":"0","completedNum":"0","avgTime":"00:00","aaOn":false},{"id":5,"name":"trans5","username":"trans5","status":"Available","inactive":"no","checkedout":"false","details":"","assignedNum":"0","completedNum":"0","avgTime":"00:00","aaOn":false},{"id":6,"name":"trans6","username":"trans6","status":"Available","inactive":"no","checkedout":"false","details":"","assignedNum":"0","completedNum":"0","avgTime":"00:00","aaOn":false}]

任何帮助将不胜感激。我以前使用 Dojo 的旧数据网格执行此操作,但似乎无法理解我在这里缺少的内容。

4

1 回答 1

6

从技术上讲,要做你想做的事,你确实需要以某种方式更新本地商店。 dstore/RequestMemory本质上是 and 的组合dstore/Requestdstore/Cache其中立即执行单个fetch请求,然后缓存存储字段所有未来的获取操作。为了强制商店从服务器刷新,你基本上需要告诉商店它的缓存不再有效,然后重新向服务器请求所有物品。这相当于以下内容:

workersStore.invalidate(); // Invalidate the cache
workersStore.fetch(); // Perform a new request for all items
workerGrid.refresh();

一旦执行了该fetch操作,Cache存储将理解它可以从其内存存储中提取所有请求。

(这个fetch调用很重要——如果你不在fetch上面调用,你的网格可能不会像你期望的那样工作。dgrid 的 OnDemandGrid 和 Pagination 模块使用 执行范围查询fetchRange,除非 Cache 知道它拥有存储的所有数据,否则它只会让这些传递到原始存储,在这种情况下是请求,它将尝试访问服务器 - 除非您的服务可能不具备处理范围查询的能力,因此它每次只会返回整个数据集。)

诚然,我认为这比使用 RequestMemory 的人需要知道的信息要多,所以我可能会为某种 API 提交问题,以便刷新和/或更新target。上面的 3 行代码虽然是 TL;DR。

于 2015-02-27T02:07:22.470 回答