【问题标题】:Unable to add the datalist to tableRow in alloy titanium无法将数据列表添加到合金钛中的 tableRow
【发布时间】:2014-02-18 11:36:28
【问题描述】:

// 控制器.js

 $.tblRow_CurrentRow = [];

 var currentList = [
    {plan:' Plan1',date:'18 Feb 2014',tier:'one'},
    {plan:'Plan2',date:'18 Dec 2013',tier:'two'},
    {plan:'Plan3',date:'20 Feb 2014',tier:'three'},
  var data = [];


    for(var i=0;i < currentList.length ;i++){

    if(i%2 == 0){

        $.tblRow_CurrentRow[i].backgroundColor = "#FFFFFF";
    }

    else{

        $.tblRow_CurrentRow[i].backgroundColor = "#ABC012";
    }

    // Set text data to the label 
    $.lbl_Plan.text = currentList[i].plan;
    $.lbl_Date.text = currentList[i].date;
    $.lbl_Tier.text = currentList[i].tier;

    // Adding label to the TableRow!
    $.tblRow_CurrentRow[i].add($.lbl_Plan);
    $.tblRow_CurrentRow[i].add($.lbl_Date);
    $.tblRow_CurrentRow[i].add($.lbl_Tier); 

            data.push($.tblRow_CurrentRow[i]);
 }

$.tbl_TabeleView.data = data;

// 控制器.xml

<TableView id = "tbl_TabeleView" >

    <TableViewRow id= "tblRow_CurrentRow">

        <Label id = "lbl_Plan"></Label>
        <Label id = "lbl_Date"></Label>
        <Label id = "lbl_Tier"></Label>

    </TableViewRow>

</TableView>

在钛合金中将 dataList 添加到 tableviewRow 的问题。

错误信息 message = "'undefined' 不是对象(评估 '$.tblRow_CurrentRow[i].add')";

【问题讨论】:

    标签: javascript titanium titanium-alloy


    【解决方案1】:

    在您的视图中,您将 $.tblRow_CurrentRow 定义为单个 TableViewRow。您不能将其视为控制器中的对象数组。我修复了您的代码以使其正常工作,因此您可以看到它应该是怎样的:

    index.js:

    $.tblRow_CurrentRow = [];
    
     var currentList = [
        {plan:' Plan1',date:'18 Feb 2014',tier:'one'},
        {plan:'Plan2',date:'18 Dec 2013',tier:'two'},
        {plan:'Plan3',date:'20 Feb 2014',tier:'three'},
    ];
    
    var data = [];
    
    
    for (var i=0; i < currentList.length; i++){
        $.tblRow_CurrentRow[i] = $.UI.create('TableViewRow', {
            backgroundColor: (i % 2 === 0 ? '#FFFFFF' : '#ABC012'),
            layout: 'vertical',
        });
    
        // Set text data to the label
        $.tblRow_CurrentRow[i].add(
            $.UI.create('Label', { text: currentList[i].plan } )
        );
        $.tblRow_CurrentRow[i].add(
            $.UI.create('Label', { text: currentList[i].date } )
        );
        $.tblRow_CurrentRow[i].add(
            $.UI.create('Label', { text: currentList[i].tier } )
        );
    
        data.push($.tblRow_CurrentRow[i]);
    }
    
    $.tbl_TabeleView.data = data;
    
    $.getView().open();
    

    index.xml:

    <Alloy>
        <Window>
            <TableView id="tbl_TabeleView" />
        </Window>
    </Alloy>
    

    基本上,视图模板只包含简单的 Window 和空的 TableView,我们将在其中添加行。在您的 for 循环中,我正在创建 TableViewRow 对象设置不同的背景,然后添加您需要的所有 3 个标签。

    垂直布局让我们可以很好地在另一个下显示标签。
    方法 $.UI.create('TableViewRow') 只是 Ti.UI.createTableViewRow(); 的更好版本。如果您感到困惑,您可以替换它们。

    【讨论】:

    • 谢谢丹妮拉!它对我真的有帮助!不要混淆!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-07
    • 2023-03-25
    • 2013-09-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多