dojo dgrid与来自url(json数据)的存储重新加载/刷新按钮

时间:2015-02-26 17:49:01

标签: javascript dojo store dgrid dstore

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

JSON数据来自如下格式的网址:

[{"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([ '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的旧数据网格做这件事,似乎无法理解我在这里缺少的东西。

1 个答案:

答案 0 :(得分:5)

从技术上讲,要做你想做的事,你需要以某种方式更新本地商店。 dstore/RequestMemory本质上是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知道它具有所有商店的数据,否则它只会让那些传递到原始商店,在这种情况下是Request,它会尝试命中服务器 - 除了您的服务可能无法处理远程查询,因此每次都会返回整个数据集。)

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