首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Mootools滤波

Mootools滤波
EN

Stack Overflow用户
提问于 2012-01-04 14:23:34
回答 1查看 335关注 0票数 0

如何使用mootools在div容器中筛选(替换empty()函数)?问题:如何将下面的代码替换为:

如果friend_id在容器中和JSON中不存在,则

  1. 从容器(<div id="fc_contacts" class="fc_contacts">)中删除id="fc_ID"中不存在的JSON字符串
  2. 中不存在的新块到容器中(<div id="fc_contacts" class="fc_contacts">),如果friend_id在容器中不存在,则不要做任何事情。

请登记密码。

我的代码如下:

代码语言:javascript
复制
{"users": [{"friend_id":"62","name":"admin","username":"admin"},{"friend_id":"66","name":"other","username":"other"}],"total": "1","total_online":"1"}

    onSuccess: function(f){ /*Periodical function*/                                 
        for(var i=0; i< f.users.length;i++){
            if(f.users[i].friend_id){                                                           
                friends.push(chat.render('add_contact',f.users[i]));
            } 
        }

        /*  Question here: How can I replace my code below to:
            1. Remove from container(<div id="fc_contacts" class="fc_contacts">) blocks with id="fc_ID" which not exist in JSON string
            2. Add new block to container(<div id="fc_contacts" class="fc_contacts">) if if doesn't exist in container 
            3. If friend_id is in container and in the JSON - do nothing.       
        */

        $('fc_contacts').empty(); /*Replace this*/
        $('fc_contacts').addContacts(friends.join(''), 'bottom');   /*ELSE - ADD NEW*/ //el.empty();

    }


    Element.implement({
        addContacts: function(html,where){
            return this.grab(new Element('<div>', {
                'html': html,
                'class': 'fc_contacts_container'
            }),where);          
        }
    });


    <div id="fc_contacts" class="fc_contacts">
        <div class="fc_contacts_container">
            <div class="fc_contact clear_fix" id="fc_62">
                <!--OTHER HTML!->
            </div>
        </div>
        <div class="fc_contacts_container">
            <div class="fc_contact clear_fix" id="fc_66">
                <!--OTHER HTML!->
            </div>
        </div>
    </div>

谢谢!

P.S

它适用于JQUERY (但我需要它与Mootools兼容):

代码语言:javascript
复制
var ids = [];   
for(var i=0; i<f.users.length;i++){                             
ids.push('fc-' + f.users[i].friend_id);
}
jQuery('#fc_contacts').append(friends.join('')).children().filter(function(i) {
    return ids.indexOf(this.id) === -1;
}).remove();
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-01-09 18:19:13

user889349,我会这样做:

代码语言:javascript
复制
$('fc_contacts').getElements("div.fc_contact").each(function(c) {
    var id = c.get("id");
    if(!f.users.some(function(u){return id == 'fc_'+u.friend_id;}))
        c.destroy();
});
f.users.each(function(u) {
    if(!$('fc_'+u.friend_id))
        $('fc_contacts').addContacts(chat.render('add_contact',u), 'bottom');
});

分两步…

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8728400

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档