问题
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