2012-01-04 9 views
0

mootoolsを使用してdivのコンテナにフィルタリング(empty()の機能を置き換えます)するにはどうすればよいですか?ここ 質問:どのように私はに以下の私のコードを置き換えることができますしていない場合場合はJSON文字列 Mootoolsのフィルタリング

  • には存在しないid="fc_ID"

    1. コンテナ(<div id="fc_contacts" class="fc_contacts">)から外したブロックは、コンテナ(<div id="fc_contacts" class="fc_contacts">)に新しいブロックを追加します。コンテナ内に存在する場合
    2. friend_idがコンテナにあり、JSONにある場合は何もしません。

    コードをチェックしてください。

    以下の私のコード:

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

    ありがとう!

    PS

    それはjQueryを使って動作します(しかし、私はそれはMooToolsはと互換性がある必要があります):ここ

    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(); 
    
  • 答えて

    1

    user889349、 は、私はそれを行うだろうかです:

    $('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'); 
    }); 
    

    2つのステップで...