问题描述
我正在使用最新的Tianium Appcelerator,并且我的项目正在使用Alloy。
I am working with the latest Tianium Appcelerator and my project is using Alloy.
我有一个 TableView
ID为: tblResults
在我的控制器中,我用以下行填充此表视图:
In my controller, I populate this table view with rows like this:
// Dummy data
var results = [];
results.push({
title: 'Hello World',
value: '123456'
});
results.push({
title: 'Bye World',
value: '654321'
});
// Build result data
var resultData = [];
for (var i = 0; i < results.length; i++) {
resultData.push(createResultRow(
results[i].title,
results[i].value
));
}
// Method to create result row
function createResultRow(myTitle, myValue) {
var tableRow = Titanium.UI.createTableViewRow({
height: 160
id: 'row-'+ myValue
});
var tableRowView = Titanium.UI.createView({
layout: 'horizontal'
});
var myButton = Titanium.UI.createButton({
title: myTitle,
btnValue: myValue
});
myButton.addEventListener('click', function(e) {
handleButtonClick(e);
});
tableRowView.add(myButton);
tableRow.add(tableRowView);
return tableRow;
}
// Set table data
$.tblResults.setData(resultData);
// Method to handle button click
function handleButtonClick(e) {
if (e.source && e.source.btnValue) {
// how to select row having a id: 'row-'+ e.source.btnValue ???
}
}
这将执行以下操作,生成一个虚拟数组对象。然后使用它,在具有视图的行中填充表格视图,其中有一个按钮。
What this will do is, generate a dummy array of objects. Then using that, populate the table view with row that has a view, within it there is a button.
我要实现的是,单击按钮时,我想选择具有以下ID的表行:
What I am trying to achieve is, when the button is clicked, I want to select the table row having the id like this:
'row-'+ e.source.btnValue
用纯javascript / jquery DOM样式,我会做这样的事情:
in pure javascript/jquery DOM style, I would have done something like this:
$('#row-'+ e.source.btnValue)
如何在Titanium Appcelerator中实现呢?是否有某种类似于jQuery的元素选择器功能?
How can I achieve this in Titanium Appcelerator? Is there a element selector functionality of some sort like in jQuery?
推荐答案
这是我们通常不要求的功能目前支持,但应该支持。现在,您必须保留id的哈希->查看引用并以此方式查找。但是,我在这里
This is a very often requested feature that we don't currently support, but should. Right now, you'd have to keep a hash of id -> view reference and look it up that way. However, I opened a Feature Request here https://jira.appcelerator.org/browse/TIMOB-20286
这篇关于在Titanium Appcelerator中按ID选择动态生成的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!