Getting ID of clicked TableRow in Titanium Alloy?

最后都变了- 提交于 2019-12-11 00:04:29

问题


I'm new to Titanium and to Backbone. I've worked with JS frameworks before (most familiar with Knockout.js), but Backbone, and the way it works with Alloy, is taking some getting used to.

I want to do something very simple. I have a collection bound to a TableView. All I want to do is get the data associated with a particular row when it is clicked.

The should be trivial, but all the docs seem to assume you know how to use Alloy already!

Model

exports.definition = {
    config: {
        columns: {
            subject: "text",
            convo_id: "integer",
            created: "text",
            modified: "text"
        },
...

View

<Alloy>
    <Window id="convosView" title="Conversations">
        <ScrollView id="convoScrollList">
            <TableView id="convoList" dataCollection="convos">
                <TableViewRow onClick="rowClick">
                    <View class="convoRow">
                        <Label class="convoTitle" text="{subject}" />
                        <Label class="convoDate" text="{created}" />
                        <View class="rowArrow" />
                    </View>
                </TableViewRow>
            </TableView>
        </ScrollView>
    </Window>
</Alloy>

Controller

var conversations = Alloy.Collections.convos;
conversations.fetch();

function rowClick(e) {
    alert(e.created);
};    

回答1:


take a look at the sample port of the ti fugutive app I created. The basic idea is to save the id of the model in the table row and then on click, fetch the model.

$.table.addEventListener('click', function(_e) {
    var detailController = Alloy.createController('FugitiveDetail', {
        parentTab : $.fugitiveTab,
        data : fugitiveCollection.get(_e.rowData.model)
    });
    $.fugitiveTab.open(detailController.getView());
});

the table row is constructed like this

<Alloy>
    <!-- have to use alloy_id since I did not specify an id in the schema -->
    <TableViewRow id="row" dataId="" model="{alloy_id}">
        <View class="vgroup">
            <Label id="name" text="{name}"/>
            <Label id="address" text="{address}"/>
        </View>
    </TableViewRow>
</Alloy>



回答2:


Do something like this

function rowClick(e) {
    alert(e.rowData);
}; 

you could aslo get the index as follows

function rowClick(e) {
        alert(e.index);
    };

Thanks



来源:https://stackoverflow.com/questions/18017086/getting-id-of-clicked-tablerow-in-titanium-alloy

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!