2011-12-15 3 views
1

각 목록 항목에 데이터 태그로 값이 추가 된 정렬되지 않은 항목 목록이 있습니다. 사용자가 목록에서 항목을 선택하면 li가 사라지고 데이터 태그가 별도의 셀로 표에 인쇄되고 모든 데이터가 배열로 저장됩니다. 각 항목을 다른 목록에 추가 할 수 있었지만 표 행으로 추가하기가 어려웠습니다.데이터 태그가있는 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

관련 문제