Dashcode 列表 - OnClick
我在 Dashcode 中创建了一个项目,插入了一个列表控件,启用了动态列表,创建了一个 JSON 对象并将其与该控件关联。 代码看起来像这样...
var stations = {
_rowData: ["Mitchelton", "Gaythorne", "Albion", "Central",
"Burpengary", "Petrie", "Morayfield", "Caboolture", "Ferny Grove"],
numberOfRows: function() { return this._rowData.length; },
prepareRow: function(rowElement, rowIndex, templateElements) {
if (templateElements.label) {
templateElements.label.innerText = this._rowData[rowIndex];
}
rowElement.onclick = function(event) { alert("Row "+rowIndex); };
}
正如您所看到的,当选择一个项目时,它将打印 rowIndex,但我想显示该索引处的实际值。
不幸的是,这个(例如)“this._rowData[2]”不起作用,它似乎无法找到“_rowData”对象。
I have created a project in Dashcode, inserted a List control, enabled a dynamic list, created a JSON object and associated it with the control. The code looks like this ...
var stations = {
_rowData: ["Mitchelton", "Gaythorne", "Albion", "Central",
"Burpengary", "Petrie", "Morayfield", "Caboolture", "Ferny Grove"],
numberOfRows: function() { return this._rowData.length; },
prepareRow: function(rowElement, rowIndex, templateElements) {
if (templateElements.label) {
templateElements.label.innerText = this._rowData[rowIndex];
}
rowElement.onclick = function(event) { alert("Row "+rowIndex); };
}
As you can see when an item is selected it will print the rowIndex, but I would like to display the actual value at that index.
Unfortunately this (for example) "this._rowData[2]" doesnt work, it doesn't seem to be able to find the "_rowData" object.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这应该有效:
问题是在
rowElement.onclick
事件处理程序中,this
引用了 DOM 元素rowElement
,而不是您的站
对象。 我们通过创建一个指向stations
对象的变量(我将其命名为_this
,但您可以随意命名)来解决该问题,然后使用该变量每当我们想要访问事件处理程序内的stations
对象时。有关
this
关键字的详细信息,请参阅以下两篇文章:This should work:
The problem is that within the
rowElement.onclick
event handler,this
refers to the DOM elementrowElement
, not yourstations
object. We fix the problem by creating a variable (I've named it_this
, but you can call it whatever you want) that points to thestations
object and then using this variable whenever we want to access thestations
object inside the event handler.See the following two articles for more information on the
this
keyword: