내 목록 정렬이 jQuery에서 제대로 작동하지 않습니다.

데니스

내 데이터 속성에 대한 jQuery에서 4 가지 방법 정렬을 만들었습니다. 그러나 정렬 옵션을 클릭하면 항상 한 번 AZ에서 알파벳순으로 정렬됩니다. 정렬에서 다른 시간을 클릭하면 아무 작업도 수행되지 않습니다.

클릭 한 항목을 확인하기 위해 콘솔 로그를 수행하면 다음과 같은 결과가 나타납니다.

za
az

또는

1-10
za
az

따라서 항상 AZ에서 정렬됩니다. 그러나 솔직히 이유를 모릅니다.

다음은 내 HTML의 예입니다.

<div class="sort">
    <span class="sort__item" id="js-az">A-Z</span>
    <span class="sort__item" id="js-za">Z-A</span>
    <span class="sort__item" id="js-1-10">1-10</span>
    <span class="sort__item" id="js-10-1">10-1</span>
</div>

<div class="row js-songs-sortable" id="all-song-list">
    <div class="col-md-4 col-sm-12" data-count="5" data-name="Strange Days">
        content
    </div>
    <div class="col-md-4 col-sm-12" data-count="1" data-name="Rock Bottom">
        content
    </div>
    <div class="col-md-4 col-sm-12" data-count="1" data-name="Moutain">
        content
    </div>
    <div class="col-md-4 col-sm-12" data-count="3" data-name="Mad Sun">
        content
    </div>
    <div class="col-md-4 col-sm-12" data-count="10" data-name="Another Ugly Tune">
        content
    </div>
</div>

그리고 이것은 내 jQuery 코드입니다.

jQuery('body').on('click', '.sort #js-az', function(){
    jQuery(".js-songs-sortable > div").sort(sort_li).appendTo('.js-songs-sortable');

    function sort_li(a,b){
        return (jQuery(b).data('name')) < (jQuery(a).data('name')) ? 1 : -1;    
    }
});

jQuery('body').on('click', '.sort #js-za', function(){
    jQuery(".js-songs-sortable > div").sort(sort_li).appendTo('.js-songs-sortable');

    function sort_li(b,a){
        return (jQuery(b).data('name')) < (jQuery(a).data('name')) ? 1 : -1;    
    }
});

jQuery('body').on('click', '.sort #js-1-10', function(){
    jQuery(".js-songs-sortable > div").sort(sort_li).appendTo('.js-songs-sortable');

    function sort_li(a,b){
        return (jQuery(b).data('count')) < (jQuery(a).data('count')) ? 1 : -1;    
    }
});

jQuery('body').on('click', '.sort #js-10-1', function(){
    jQuery(".js-songs-sortable > div").sort(sort_li).appendTo('.js-songs-sortable');

    function sort_li(b,a){
        return (jQuery(b).data('count')) < (jQuery(a).data('count')) ? 1 : -1;    
    }
});

실시간 미리보기는 https://mpdb.space/mp-songs/ 에서 찾을 수 있습니다.

칼리마

코드의 문제는 사용하고 .data있지만 .attr().

jQuery('body').on('click', '.sort #js-az', function() {
  jQuery(".js-songs-sortable > div").sort(sort_li).appendTo('.js-songs-sortable');

  function sort_li(a, b) {
    return (jQuery(b).attr('data-name')) < (jQuery(a).attr('data-name')) ? 1 : -1;
  }
});

jQuery('body').on('click', '.sort #js-za', function() {
  jQuery(".js-songs-sortable > div").sort(sort_li).appendTo('.js-songs-sortable');

  function sort_li(b, a) {
    return (jQuery(b).attr('data-name')) < (jQuery(a).attr('data-name')) ? 1 : -1;
  }
});

jQuery('body').on('click', '.sort #js-1-10', function() {
  jQuery(".js-songs-sortable > div").sort(sort_li).appendTo('.js-songs-sortable');

  function sort_li(a, b) {
    return (parseInt(jQuery(b).attr('data-count'))) < parseInt((jQuery(a).attr('data-count'))) ? 1 : -1;
  }
});

jQuery('body').on('click', '.sort #js-10-1', function() {
  jQuery(".js-songs-sortable > div").sort(sort_li).appendTo('.js-songs-sortable');

  function sort_li(b, a) {
    return parseInt(jQuery(b).attr('data-count')) < parseInt(jQuery(a).attr('data-count')) ? 1 : -1;
  }
});
.sort {
  border-radius: 5px;
  background-color: #eee;
  display: inline-block;
  margin-bottom: 1rem;
}

.sort span {
  padding: 1rem;
  cursor: pointer;
  transition: all 0.3s;
  display: inline-block;
}

.sort span:hover {
  background-color: rgba(0, 0, 0, 0.1);
}

.row>div {
  padding: 0.5rem;
}

.row>div::after {
  content: "data-count= " attr(data-count) " data-name= " attr(data-name);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="sort">
  <span class="sort__item" id="js-az">A-Z</span>
  <span class="sort__item" id="js-za">Z-A</span>
  <span class="sort__item" id="js-1-10">1-10</span>
  <span class="sort__item" id="js-10-1">10-1</span>
</div>

<div class="row js-songs-sortable" id="all-song-list">
  <div class="col-md-4 col-sm-12" data-count="5" data-name="Strange Days">

  </div>
  <div class="col-md-4 col-sm-12" data-count="1" data-name="Rock Bottom">

  </div>
  <div class="col-md-4 col-sm-12" data-count="1" data-name="Moutain">

  </div>
  <div class="col-md-4 col-sm-12" data-count="3" data-name="Mad Sun">

  </div>
  <div class="col-md-4 col-sm-12" data-count="10" data-name="Another Ugly Tune">

  </div>
</div>

또한 유지 관리 및 가독성을 위해 코드를 약간 변경할 수 있습니다. 이 예를 참조하십시오.

jQuery('body').on('click', '.sort__item', function() {
  const id = jQuery(this).attr("id");

  jQuery(".js-songs-sortable > div").sort(function(a, b) {
    let firstEl = jQuery(a);
    let secondEl = jQuery(b);
    let $return = 0;

    switch (id) {
      case "js-az":
        $return = (firstEl.attr('data-name') > secondEl.attr('data-name')) ? 1 : -1;
        break;

      case "js-za":
        $return = (firstEl.attr('data-name') < secondEl.attr('data-name')) ? 1 : -1;
        break;

      case "js-1-10":
        $return = (parseInt(firstEl.attr('data-count')) > parseInt(secondEl.attr('data-count'))) ? 1 : -1;
        break;

      case "js-10-1":
        $return = (parseInt(firstEl.attr('data-count')) < parseInt(secondEl.attr('data-count'))) ? 1 : -1;
        break;
    }

    return $return;
  }).appendTo('.js-songs-sortable');
});
.sort {
  border-radius: 5px;
  background-color: #eee;
  display: inline-block;
  margin-bottom: 1rem;
}

.sort span {
  padding: 1rem;
  cursor: pointer;
  transition: all 0.3s;
  display: inline-block;
}

.sort span:hover {
  background-color: rgba(0, 0, 0, 0.1);
}

.row>div {
  padding: 0.5rem;
}

.row>div::after {
  content: "data-count= " attr(data-count) " data-name= " attr(data-name);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="sort">
  <span class="sort__item" id="js-az">A-Z</span>
  <span class="sort__item" id="js-za">Z-A</span>
  <span class="sort__item" id="js-1-10">1-10</span>
  <span class="sort__item" id="js-10-1">10-1</span>
</div>

<div class="row js-songs-sortable" id="all-song-list">
  <div class="col-md-4 col-sm-12" data-count="5" data-name="Strange Days">

  </div>
  <div class="col-md-4 col-sm-12" data-count="1" data-name="Rock Bottom">

  </div>
  <div class="col-md-4 col-sm-12" data-count="1" data-name="Moutain">

  </div>
  <div class="col-md-4 col-sm-12" data-count="3" data-name="Mad Sun">

  </div>
  <div class="col-md-4 col-sm-12" data-count="10" data-name="Another Ugly Tune">

  </div>
</div>

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

내 연결 목록이 제대로 작동하지 않습니다.

분류에서Dev

Jquery 애니메이션이 목록 항목에서 제대로 작동하지 않습니다.

분류에서Dev

jquery 데이터 테이블의 내림차순 정렬이 제대로 작동하지 않습니다.

분류에서Dev

목록 항목 정렬이 제대로 작동하지 않음

분류에서Dev

목록 항목 정렬이 제대로 작동하지 않음

분류에서Dev

JQuery 정렬이 제대로 작동하지 않습니다.

분류에서Dev

목록 항목 내부의 버튼이 제대로 작동하지 않습니다.

분류에서Dev

목록에서 단어에 대한 임의 선택이 제대로 작동하지 않습니다 (Python / Tkinter).

분류에서Dev

GridLayout 내에서 TextView 래핑이 제대로 작동하지 않습니다.

분류에서Dev

타이머가 내 코드에서 제대로 작동하지 않습니다.

분류에서Dev

정렬되지 않은 목록이 내 이미지 아래로 이동하지 않습니다.

분류에서Dev

인라인 블록에서 세로 정렬이 작동하지 않습니다.

분류에서Dev

전투 로직에서 배열 목록이 제대로 작동하지 않는 것 같습니다.

분류에서Dev

내 탐색에서 드롭 다운 목록의 배경색이 제대로 표시되지 않습니다.

분류에서Dev

내 탐색에서 드롭 다운 목록의 배경색이 제대로 표시되지 않습니다.

분류에서Dev

jquery로 빌드 된 HTML 드롭 다운 목록이 제대로 작동하지 않습니다.

분류에서Dev

SwiftUI : 섹션이있는 목록에서 탐색이 제대로 작동하지 않습니다.

분류에서Dev

.click이 jquery에서 제대로 작동하지 않습니다.

분류에서Dev

`super ()`키워드가 목록 이해에서 제대로 작동하지 않습니다.

분류에서Dev

Android에서 정렬 배열 목록이 작동하지 않습니다.

분류에서Dev

Jquery Sortable Horizontal 목록이 고정 항목에서 작동하지 않습니다.

분류에서Dev

jquery-ias 플러그인이 내 웹 사이트에서 작동하지 않습니다. 더 많은 항목을로드하면 작동합니다.

분류에서Dev

md-no-focus-style이 내 설정에서 제대로 작동하지 않습니다.

분류에서Dev

내 jQuery가 제대로 작동하지 않습니다.

분류에서Dev

스윙-목록이 paintComponent 메소드 내에서 작동하지 않는 문제

분류에서Dev

jquery .on ( 'click')이 새로 고침 div 내에서 작동하지 않습니다.

분류에서Dev

내 탐색이 제대로 작동하지 않습니다.

분류에서Dev

내 'if'문이 제대로 작동하지 않습니다.

분류에서Dev

내 strcpy 구현이 제대로 작동하지 않습니다.

Related 관련 기사

  1. 1

    내 연결 목록이 제대로 작동하지 않습니다.

  2. 2

    Jquery 애니메이션이 목록 항목에서 제대로 작동하지 않습니다.

  3. 3

    jquery 데이터 테이블의 내림차순 정렬이 제대로 작동하지 않습니다.

  4. 4

    목록 항목 정렬이 제대로 작동하지 않음

  5. 5

    목록 항목 정렬이 제대로 작동하지 않음

  6. 6

    JQuery 정렬이 제대로 작동하지 않습니다.

  7. 7

    목록 항목 내부의 버튼이 제대로 작동하지 않습니다.

  8. 8

    목록에서 단어에 대한 임의 선택이 제대로 작동하지 않습니다 (Python / Tkinter).

  9. 9

    GridLayout 내에서 TextView 래핑이 제대로 작동하지 않습니다.

  10. 10

    타이머가 내 코드에서 제대로 작동하지 않습니다.

  11. 11

    정렬되지 않은 목록이 내 이미지 아래로 이동하지 않습니다.

  12. 12

    인라인 블록에서 세로 정렬이 작동하지 않습니다.

  13. 13

    전투 로직에서 배열 목록이 제대로 작동하지 않는 것 같습니다.

  14. 14

    내 탐색에서 드롭 다운 목록의 배경색이 제대로 표시되지 않습니다.

  15. 15

    내 탐색에서 드롭 다운 목록의 배경색이 제대로 표시되지 않습니다.

  16. 16

    jquery로 빌드 된 HTML 드롭 다운 목록이 제대로 작동하지 않습니다.

  17. 17

    SwiftUI : 섹션이있는 목록에서 탐색이 제대로 작동하지 않습니다.

  18. 18

    .click이 jquery에서 제대로 작동하지 않습니다.

  19. 19

    `super ()`키워드가 목록 이해에서 제대로 작동하지 않습니다.

  20. 20

    Android에서 정렬 배열 목록이 작동하지 않습니다.

  21. 21

    Jquery Sortable Horizontal 목록이 고정 항목에서 작동하지 않습니다.

  22. 22

    jquery-ias 플러그인이 내 웹 사이트에서 작동하지 않습니다. 더 많은 항목을로드하면 작동합니다.

  23. 23

    md-no-focus-style이 내 설정에서 제대로 작동하지 않습니다.

  24. 24

    내 jQuery가 제대로 작동하지 않습니다.

  25. 25

    스윙-목록이 paintComponent 메소드 내에서 작동하지 않는 문제

  26. 26

    jquery .on ( 'click')이 새로 고침 div 내에서 작동하지 않습니다.

  27. 27

    내 탐색이 제대로 작동하지 않습니다.

  28. 28

    내 'if'문이 제대로 작동하지 않습니다.

  29. 29

    내 strcpy 구현이 제대로 작동하지 않습니다.

뜨겁다태그

보관