gpt4 book ai didi

javascript - ExtJS 列渲染器

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:34:07 25 4
gpt4 key购买 nike

我的问题是 ExtJS 4 中 GridPanel 中列的渲染器函数。渲染器的记录应该从我的商店加载我的列表的一个元素,它确实如此;但它总是加载列表的相同元素。

这是我的代码的相关片段。

首先是我的商店:

var nStore = Ext.create('Ext.data.Store', {
storeId: 'people',
fields: ['team', 'name', 'liste', 'sums'],
data: [{
team: 'TestTeam', name: 'TestPerson',
liste: ['F', 'F', 'F', 'S', 'N', 'F', 'S',
'S', 'S', 'F', 'F', 'F', 'S', 'A', 'N',
'S', 'S', 'S', 'S', '', '', 'N', 'N',
'N', 'S', 'S', 'N', 'S', 'F', 'N', 'N'],
sums: [[7, 4, 0, 0, 0, 0, 0], [3, 0, 0, 0, 0]]
}]
});

然后我的渲染器所在的列数组:

var counter = 0;
for (var i = 0; i < Object.kws.length; i++) {
var DayOfMonth = [];
var DayOfWeek = [];
for (var j = 0; j < Object.kws[i].kwDays.length; j++) {
var c = counter;
DayOfMonth[j] = {
text: '<span style="font-size: 87%;">'
+ Object.kws[i].kwDays[j].day
+ '.<br>'
+ Object.kws[i].kwDays[j].dayOfWeek
+ '.</span>', sortable: false,
draggable: false, menuDisabled: true,
resizable: false, width: 40, align: 'center',
renderer: function (value, meta, record) {
return record.data.liste[c];

}
};
counter++;
}
}

代码经过改编和删节。

这是一个picture of the result :

The Result

通常情况下,网格中的单元格应显示我的商店中列表的计数元素。

谁能帮助我理解我做错了什么?

最佳答案

问题是:

In JavaScript, functions enclose variables which were defined in a scope outside of their own in such a way that they have a "living" reference to the variable, not a snapshot of its value at any particular time.

Understanding variable capture by closures in Javascript/NodeHow do JavaScript closures work?

由于在调用使用“c”的方法之前“c”或“计数器”已完全递增,因此“c”的值始终是 for 循环中获得的最高值。要解决此问题,您需要在创建渲染器函数时捕获“c”的值。此代码说明了问题以及如何捕获值以实现预期效果:

var counter = 0;
var dayOfMonths = [];
var dayOfMonthCaptured = [];
for (var i = 0; i < 10; i++) {
var c = counter;
console.log(c);
dayOfMonths.push(function () {
var index = c;
//since counter is incremented fully before this method is called, you get the last value
console.log(index);
});
//here we pass the current value of c to a new function and capture its current value
dayOfMonthCaptured.push((function (index) {
return function () {
console.log(index);
};
})(c));
counter++;
}
//this won’t work
for (day in dayOfMonths) {
dayOfMonths[day]();
}
console.log("—————————");
for (day in dayOfMonthCaptured) {
dayOfMonthCaptured[day]();
}

关于javascript - ExtJS 列渲染器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27295830/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com