2011-12-15 14 views
1

私は、各リスト項目にデータタグとして値を付加した項目の順序付けられていないリストを持っています。ユーザーがリストから項目を選択すると、データがタグとして別のセルとして表に印刷され、すべてのデータが配列に格納されます。私は別のリストに各項目を追加することができましたが、テーブル行として追加するのは難しいです。データタグを含むjQuery配列とテーブルへの印刷

私はそれが 'append'の使用だと信じていますが、liはテーブル内で追加され、現在の配列の追加順でデータが合成されます。

私は、オーダーがクリック>配列に追加>テーブルに印刷する必要があると感じます。

Here is a link to my most recent.

事前にご協力いただきありがとうございます。

var myArraySelected = [] ; 

$(function() { 

var myFunc = function() { 
    myArraySelected = [] ; 
    var userList = $('#selected'); 
    userList.children('li').each(function() { 
     var userID = $(this).attr('data-user'); 
     var userService = $(this).attr('data-role'); 
     var userCategory = $(this).attr('data-category'); 
     var userName = $(this).attr('data-name'); 
     myArraySelected.push({ 
      'id':userID, 
      'name':userName, 
      'role':userService, 
      'category' :userCategory 
     }); 

    }); 
}; 

$('#userList li').click(function() { 
    $(this).fadeOut('slow', function() { // code to run after the fadeOut 
     $(this).append('<tr><td><p>'+ $(this).attr('data-name') + '<br>' + $(this).attr('data-user') + '<br>' + $(this).attr('data-role') + '<br>' + $(this).attr('data-category') + '</p></td></tr>') 
     $(this).appendTo('#selected').fadeIn('slow'); 
     myFunc(); 
    }); 

}); 
myFunc(); 
}); 
+0

ご迷惑をおかけして申し訳ございませんが、 – JMax

答えて

0

ご質問から、クリックしたリストアイテムのデータを表要素に追加しようとしていることを前提としています。現在、リスト項目に追加してから、リスト項目を表に追加しています。リスト項目を使わずにテーブル行をテーブルに追加するだけでどうでしょう。

$(this).append('<tr><td><p>'+ $(this).attr('data-name') + '<br>' + $(this).attr('data-user') + '<br>' + $(this).attr('data-role') + '<br>' + $(this).attr('data-category') + '</p></td></tr>') 
    $(this).appendTo('#selected').fadeIn('slow'); 

変更あなたのアレイに追加するには、その後

$('#selected').append('<tr><td><p>'+ $(this).attr('data-name') + '<br>' + $(this).attr('data-user') + '<br>' + $(this).attr('data-role') + '<br>' + $(this).attr('data-category') + '</p></td></tr>').find('tr:last').fadeIn(); 

そして、あなたがする必要があるすべては右のあなたのコード内の同じ場所にアレイ上にpush別の指標であります:

$('#userList li').click(function() { 

    //cache the `$(this)` selector since it will be used more than once 
    var $this = $(this); 
    $this.fadeOut('slow', function() { // code to run after the fadeOut 

     //append a row onto the `#selected` table using the clicked list-item's data-attributes as data; notice after the append I select the newly appended table row and fade it in (which assumes that it's CSS is making it hidden at first) 
     $('#selected').append('<tr><td><p>'+ $this.attr('data-name') + '<br>' + $this.attr('data-user') + '<br>' + $this.attr('data-role') + '<br>' + $this.attr('data-category') + '</p></td></tr>').find('tr:last').fadeIn(); 

     //now push a new index onto the `MyArraySelceted` array using the data-attributes for the clicked list-item as data 
     myArraySelected.push({ 
      'id'  : $this.attr('data-user'), 
      'name'  : $this.attr('data-name'), 
      'role'  : $this.attr('data-role'), 
      'category' : $this.attr('data-category') 
     }); 

     //now remove the list-item from the UL element 
     $this.fadeOut(function() { 
      $this.remove(); 
     }); 

    }); 

}); 
+0

迅速な対応に感謝します。表の値の印刷は正常に機能しましたが、配列の値は入力されませんでした。私はそれをクリックして "配列に追加してからテーブルに"印刷する必要があると推測しています。 – benblustey

+0

@benblustey '#selected'がテーブルの場合、子' li'要素を検索することはできません。 '#selected'がテーブルの場合、テーブルにリスト項目を追加することで無効なHTMLを作成しています。さらに、テーブルのテーブル行を検索するときに '.children()'を使用しないでください。ほとんどのブラウザは '

'の直接の子として ''を追加するので '.find要素と ''要素は、 ''要素の直接の子要素であるため、これは表をマークアップする適切な方法です。 – Jasper

+0

@benblustey可能であれば、またはjsfiddleをセットアップできるかどうか、あなたのサイトのライブバージョンを見たいと思います。一般的なアドバイスが必要なようです。 – Jasper

関連する問題