【问题标题】:Titanium underscore adding removing items from array钛下划线添加从数组中删除项目
【发布时间】:2014-08-08 09:59:17
【问题描述】:

我正在使用钛 3.3 和合金 1.4.1。用户应该能够从他的手机通讯录中向所有选定的人(这些人位于称为“联系人”的数组中)发送电子邮件。

用户可以在列表视图中从他的地址簿中选择和取消选择姓名/电子邮件(链接屏幕截图中的黄色背景)。

截图: http://s30.postimg.org/rsa82u9qp/listview_checkbox.png

从地址簿中拉出正确的信息(电子邮件)并在检查复选框时添加到联系人数组。因此通过复选框进行选择是有效的,但是当取消选择复选框时,电子邮件/项目不会从数组中删除。查看最后一个日志条目行:

Ti.API.info(JSON.stringify(contacts) + " 这是匹配数组 结束");

之前被选中但现在被取消选中的电子邮件仍在联系人数组中。

我也发现了这个,但不幸的是它没有帮助。

http://developer.appcelerator.com/question/163878/loop-through-listview-to-grab-items-with-certain-properties#comment-206400

代码:

$.listview.addEventListener('itemclick',function(e){
var item = e.section.getItemAt(e.itemIndex);
if (item.properties.accessoryType == Ti.UI.LIST_ACCESSORY_TYPE_NONE) {
    item.properties.accessoryType = Ti.UI.LIST_ACCESSORY_TYPE_CHECKMARK;
    Ti.API.info(item.textEmail.text + " this is item.textEmail.textinside adding if");   
    var added = item.textEmail.text;
    if (!_.contains(contacts,added)) {contacts.push(added);
}
    Ti.API.info(JSON.stringify(added) + " item 1 added");   
    Ti.API.info(contacts + " this is matches array inside adding if");
}
else {
    item.properties.accessoryType = Ti.UI.LIST_ACCESSORY_TYPE_NONE;
    var removed = item.textEmail.text;
   contacts = _.without(contacts, removed); 
    //contacts.splice(removed);
    Ti.API.info(JSON.stringify(removed) + " item 2 removed");
    Ti.API.info(JSON.stringify(contacts) + " this is contacts in removing if case");
}
e.section.updateItemAt(e.itemIndex, item); 
Ti.API.info(JSON.stringify(contacts) + " this is matches array at the end");
});

任何想法我在这里做错了什么?

【问题讨论】:

    标签: arrays listview titanium underscore.js


    【解决方案1】:

    首先你应该有一个平面数组(联系人):

    var contacts = new Array();
    

    ...

    然后添加/删除 textEmail.text 属性并正确搜索它们

    if (item.properties.accessoryType == Ti.UI.LIST_ACCESSORY_TYPE_NONE)
    {
        item.properties.accessoryType = Ti.UI.LIST_ACCESSORY_TYPE_CHECKMARK;
        var i;
        for(i in item.textEmail.text) //item.textEmail.text is Object
        {
           if(contacts.indexOf(item.textEmail.text[i]) === -1)
           {
              contacts.push(item.textEmail.text[i]);
              Ti.API.info(item.textEmail.text[i] + " item added");  
           }
        }
        Ti.API.info(contacts + " this is matches array inside adding if");
    }
    else
    {
        item.properties.accessoryType = Ti.UI.LIST_ACCESSORY_TYPE_NONE;
        var i;
        for(i in item.textEmail.text)
        {
           var p = contacts.indexOf(item.textEmail.text[i]);
           if(p !== -1)
           {
              contacts.splice(p,1);
              Ti.API.info(item.textEmail.text[i] + " item removed");
           }
        }
        Ti.API.info(contacts + " this is contacts in removing if case");
    }
    

    // 如果 item.textEmail.text 等于 Email Person 对象: { "工作" : ["...", "..."], "家" : ["...", "..."] }

    var i, j;
    
    for(i in item.textEmail.text)
    {
        for(j = 0; j < item.textEmail.text[i].length; j++)
        {
           // Start Add Code
           if(contacts.indexOf(item.textEmail.text[i][j]) === -1)
           {
              contacts.push(item.textEmail.text[i]);
           } // End Add Code
    
           // Start Remove Code
           var p = contacts.indexOf(item.textEmail.text[i]);
           if(p !== -1)
           {
              contacts.splice(p,1);
           } // End Remove Code
       }
    }
    

    【讨论】:

    • Hola Alejandro,非常感谢您的快速回复。我尝试了您发布的 sn-p - 它现在删除了项目,但不是正确的项目。我还看到电子邮件字段(item.textEmail.text)是一个多值字段(有些有家庭和电子邮件地址)。我检查了原始代码(包括删除功能)与(单值)“recordID”和“fullName”字段。
    • Splice 返回被移除的元素,抱歉这个小细节。
    • 我在此链接Person 上看到电子邮件是一个具有数组字符串值的对象,因此您应该将迭代更改为双迭代。我已经改变了我的答案;)
    • 你好亚历杭德罗!你是我今天的官方英雄!奇迹般有效。非常感谢!
    猜你喜欢
    • 2015-07-21
    • 2021-10-03
    • 1970-01-01
    • 2017-09-13
    • 1970-01-01
    • 2017-01-29
    • 2019-11-25
    • 2011-04-26
    • 1970-01-01
    相关资源
    最近更新 更多