【发布时间】:2017-08-10 17:28:21
【问题描述】:
感谢之前的一些帮助,我让 Dojo dgrid 工作了;甚至想出了如何将它与我的休息服务中的数据联系起来。
现在我添加了一个输入框,一个按钮,所有的逻辑都发生在按钮点击上。但是第二次单击该按钮时,即使输入字段中的输入值相同,也会出现错误。
错误:
TypeError: Cannot read property 'element' of undefined in StoreMixin.js:33
我读了这个How To reset the OnDemandGrid,但是是否有必要检查网格是否存在并执行不同的逻辑?我不能每次都“更新”一个新的吗?
代码:
<div data-dojo-type="dijit/layout/ContentPane" data-dojo-props='title:"CustomersGrid"'>
<label for="lastnameStartsWith">Lastname Starts With:</label>
<input id="lastnameStartsWith" type="text" name="lastnameStartsWith" value="Wag"
data-dojo-type="dijit/form/TextBox"
data-dojo-props="trim:true, propercase:true" />
<br />
<br />
<button id="queryStudentsButton" data-dojo-type="dijit/form/Button"
data-dojo-type="dijit/form/Button"
data-dojo-props="iconClass:'dijitIconTask'">
<span>Query</span>
<script type='dojo/on' data-dojo-event='click'>
require([
'dstore/RequestMemory',
'dstore/Memory',
'dgrid/OnDemandGrid'
], function (RequestMemory, Memory, OnDemandGrid) {
var url = '../students/' + dojo.byId('lastnameStartsWith').value;
console.log("query students for dataGrid latsnameStartsWith:" + dojo.byId('lastnameStartsWith').value);
require(['dojo/request'], function(request){
request.get(url,
{headers: {"Content-Type": 'application/json',
"username": securityConfig.username,
"password": securityConfig.password}}
)
.then(function(response){
//console.log("string response=" + response);
var respJSON = JSON.parse(response);
var respDataForDGrid = respJSON.recordset;
console.log("got respJSON back, num rows= " + respDataForDGrid.length);
//================================================
// Create an instance of OnDemandGrid referencing the store
console.log("Debug1");
var grid2 = new OnDemandGrid({
collection: new Memory({ data: respDataForDGrid }),
columns: {
student_id: 'ID',
student_firstname: 'First Name',
student_lastname: 'Last Name',
student_city: 'City',
student_state: 'State',
student_zip: 'Zip'
}
}, 'grid2');
console.log("Debug2");
grid2.startup();
console.log("Debug3");
},
function(error){
console.log("Error=" + error);
//dom.byId('studentFeedback').value += response;
});
});
});
</script>
</button>
<h2>My demoGrid - From JSON RestService (Database)</h2>
<div id='grid2'></div>
</div>
第 2 部分 -
我尝试在此页面上混合使用您的代码和代码: How To reset the OnDemandGrid
if (grid2Registered){
console.log("reuse existing grid");
grid2Registered.set('collection', memStore);
// refresh: clear the grid and re-queries the store for data.
grid2Registered.refresh();
}
else{...
清除网格并重新查询存储中的数据。如果 keepScrollPosition 在实例或选项上为 true 传递给刷新,将尝试保留当前 滚动位置。 OnDemandList 从刷新返回一个承诺,它 当视图中的项目完成渲染时解决。承诺解决 使用呈现的 QueryResults。
【问题讨论】:
-
如果您已经创建了网格,如果您的结构/列保持不变,则始终建议(至少这是我所做的)用新商店刷新网格。这样,您就可以避免每次有查询时创建网格的开销。
-
那么你将如何实现呢?留个柜台?或者检查如何检查我的 grid2 是否以前定义过?
-
如果您采用模板化方法,即在 html 中定义小部件,您可以使用 data-dojo-attach-point 来引用网格。如果您采用编程方法,您可以为网格分配一个 id(请注意,您使用的每个 dijit 的 id 必须是唯一的)并将其称为 dijit.byId('yourId')。