dojo dgrid with store from url (json data) reload/refresh 按钮

dojo dgrid with store from url (json data) reload/refresh button

首先,我使用的是 dojo 1.1.0 和 dgrid 0.4.0。我正在创建一个带有几个 dgrids 的页面。特别是一个网格需要从 url 加载 JSON 数据并显示它。目前使用 RequestMemory 存储可以正常工作。然而,这是一家 "memory" 商店。虽然这对其他一些人来说可能微不足道,但我需要找到一种方法来加载数据,因为它正在完成,然后在屏幕上添加一个刷新按钮,调用必要的 functions/methods 从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}]

您将在下面看到的要求实际上包含的不仅仅是这个网格...因此还有所有额外内容。

我不需要更新本地商店,也不想监视 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 的旧数据网格执行此操作,但似乎无法理解我在这里缺少的内容。

从技术上讲,要执行您想要的操作,您确实需要以某种方式更新本地商店。 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 和分页模块使用 fetchRange,除非 Cache 知道它拥有商店的所有数据,否则它只会让那些通过原始商店,在这种情况下是 Request,它将尝试访问服务器 - 除了你的服务可能没有配备处理范围查询,所以它每次都会 return 整个数据集。)

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