๋ชฉ์ฐจ
โ jquery .sort()๋ฅผ ํ์ฉํ ์์ ์ด๋๊ณผ ์ํธ ์ ๋ ฌ
์๋
ํ์ธ์! ์ด๋ฒ ํฌ์คํ
์์๋ jQuery์ .sort()
ํจ์๋ฅผ ํ์ฉํ์ฌ ์์๋ฅผ ์ด๋ํ๊ณ ๋์์ ์ํธ ์ ๋ ฌํ๋ ๋ฐฉ๋ฒ์ ๋ํด ์์๋ณด๊ฒ ์ต๋๋ค. ์ด ๊ธฐ์ ์ ๋ถ์๋ ํธ๋ฆฌ๊ตฌ์กฐ์ ๋ชฉ๋ก์์ ํญ๋ชฉ์ ๋ค๋ฅธ ์์น๋ก ์ด๋ํ๋ฉด์ ์ ๋ ฌ์ ์ ์งํ๊ณ ์ ํ ๋ ์ ์ฉํ๊ฒ ํ์ฉ๋ ์ ์์ต๋๋ค. ์ดํด๋ฅผ ๋๊ธฐ ์ํด ๊ฐ๋จํ ์์์ ํจ๊ป ์ฝ๋๋ฅผ ์ดํด๋ณด๊ฒ ์ต๋๋ค.


์์: ๋ถ์ ์ด๋๊ณผ ์ํธ ์ ๋ ฌ
์ ์๋ ์์์์๋ '๋ถ์๋ชฉ๋ก'๊ณผ '์ ํ๋ ๋ถ์'๋ผ๋ ๋ ๊ฐ์ ๊ทธ๋ฃน์ด ์์ต๋๋ค. ์ด ๊ทธ๋ฃน๋ค์ UL๊ณผ LI ์์๋ฅผ ์ฌ์ฉํ์ฌ ๊ตฌ์ฑ๋์ด ์์ต๋๋ค. ๊ฐ ๋ถ์๋ ๋ฐ์ดํฐ ์์ฑ data-sid
๋ฅผ ํตํด ์๋ณ๋ฉ๋๋ค. ์ฌ์ฉ์๊ฐ '๋ถ์๋ชฉ๋ก'์ ๋ถ์๋ฅผ ์ ํํ๋ฉด ํด๋น ๋ถ์๊ฐ '์ ํ๋ ๋ถ์'๋ก ์ด๋ํ๊ฒ ๋๋ฉฐ, ์ด๋๋ ํ์๋ ๋ถ์๋ค์ด ์์๋๋ก ์ํ
์ ๋ ฌ๋ฉ๋๋ค.
์ฝ๋ ์์
๋จผ์ , HTML ์ฝ๋๋ฅผ ์ดํด๋ณด๊ฒ ์ต๋๋ค.
<div class="sectionWrap">
<div class="choiceGroup ">
<!-- ... -->
<div class="scrollHere choiceGroup1">
<ul>
<!-- ๋ถ์ ๋ชฉ๋ก -->
</ul>
</div>
</div>
<div class="toRightArr"></div>
<div class="choiceGroup">
<!-- ... -->
<div class="scrollHere choiceGroup2">
<ul>
<!-- ์ ํ๋ ๋ถ์ -->
</ul>
</div>
</div>
</div>
๊ทธ๋ฆฌ๊ณ JavaScript ์ฝ๋๋ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
$(document).ready(function () {
$(document).on('click', '.choiceGroup1>ul>li', function () {
$(this).appendTo('.choiceGroup2>ul');
sortLi('.choiceGroup2');
});
$(document).on('click', '.choiceGroup2>ul>li', function () {
$(this).appendTo('.choiceGroup1>ul');
sortLi('.choiceGroup1');
});
function sortLi(e) {
$(e + '>ul>li').sort(function (a, b) {
return parseInt(a.dataset.sid) - parseInt(b.dataset.sid);
}).appendTo(e + '>ul');
}
});
CSS ์คํ์ผ๋ง
CSS ์คํ์ผ๋ง์ ๋ค์๊ณผ ๊ฐ์ด ์ ์๋์ด ์์ต๋๋ค.
/* ... */
.sectionWrap {
/* ... */
}
.choiceGroup1>ul>li>a {
/* ... */
}
.choiceGroup2>ul>li>a {
/* ... */
}
.choiceGroup {
/* ... */
}
.toRightArr {
/* ... */
}
๊ฒฐ๋ก
์ด๋ ๊ฒ jQuery์ .sort()
ํจ์์ .appendTo()
ํจ์๋ฅผ ํ์ฉํ์ฌ ๋ถ์๋ฅผ ์ด๋ํ๋ฉด์ ์ ๋ ฌํ๋ ๋ฐฉ๋ฒ์ ๊ตฌํํด๋ณด์์ต๋๋ค. ์ด ์์๋ฅผ ํตํด ๋ณต์กํ ๋ฐ์ดํฐ ๊ตฌ์กฐ์์๋ ์ํ๋ ํญ๋ชฉ์ ์ ํ์ ์ผ๋ก ์ด๋ํ๊ณ ๋์์ ์ํ
์ ๋ ฌ์ ์ ์งํ๋ ๋ฐฉ๋ฒ์ ๋ฐฐ์๋ณด์์ต๋๋ค. ์ด ๊ธฐ์ ์ ์์ฉํ์ฌ ๋ค์ํ ์ํฉ์์ ์ ์ฉํ๊ฒ ํ์ฉํด๋ณด์๊ธฐ ๋ฐ๋๋๋ค.
์ฃผ์ ํฌ์ธํธ ์์ฝ
- jQuery์
.sort()
ํจ์๋ฅผ ํ์ฉํ์ฌ ์์๋ฅผ ์ํ ์ ๋ ฌํ ์ ์์ต๋๋ค. .appendTo()
ํจ์๋ฅผ ์ฌ์ฉํ์ฌ ์์๋ฅผ ๋ค๋ฅธ ์์น๋ก ์ด๋์ํฌ ์ ์์ต๋๋ค.- ๋ฐ์ดํฐ ์์ฑ์ ํ์ฉํ์ฌ ๊ฐ ํญ๋ชฉ์ ์๋ณํ๊ณ ์ ๋ ฌํ๋ ๊ธฐ๋ฅ์ ๊ตฌํํ ์ ์์ต๋๋ค.
๊ฐ์ฌํฉ๋๋ค!

๋๊ธ