【问题标题】:Getting ID of clicked TableRow in Titanium Alloy?获取钛合金中单击的 TableRow 的 ID?
【发布时间】:2013-08-02 12:39:42
【问题描述】:

我是 Titanium 和 Backbone 的新手。我以前使用过 JS 框架(最熟悉的是 Knockout.js),但是 Backbone 以及它与 Alloy 的工作方式需要一些时间来适应。

我想做一些非常简单的事情。我有一个绑定到 TableView 的集合。我要做的就是在单击特定行时获取与特定行关联的数据。

这应该是微不足道的,但所有文档似乎都假设您已经知道如何使用 Alloy!

型号

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

查看

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

控制器

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

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

【问题讨论】:

    标签: backbone.js data-binding titanium titanium-alloy


    【解决方案1】:

    看看我创建的 ti fugutive 应用程序的示例端口。基本思想是将模型的id保存在表格行中,然后点击,获取模型。

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

    表格行是这样构造的

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

    【讨论】:

    • 啊,所以您仍然必须在 XML 中连接您想要检索的属性(在本例中通过 model="{alloy_id}")。我对数据绑定的假设是这种事情是不必要的。视图中模型的表示本质上与数据对象相关联。出现在Alloy中,并非如此。
    • 仍然需要绑定字段,但理论上框架可以确定idAttribute并设置在视图中
    【解决方案2】:

    做这样的事情

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

    你也可以按如下方式获取索引

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

    谢谢

    【讨论】:

    • 这是部分正确的。索引确实有效,但 e.rowData 仅返回 XML 声明中存在的参数。我想返回绑定行的实际模型数据。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多