本文介绍了在Titanium Appcelerator中通过id选择动态生成的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我正在使用最新的 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 -> 查看引用的哈希值并以这种方式查找.但是,我在这里打开了一个功能请求 https://jira.appcelerator.org/browse/TIMOB-20286

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选择动态生成的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-07 16:28