လုပ်နည်း - စာရင်းတစ်ခုကို စီပါ။
JavaScript ကို အသုံးပြု၍ HTML စာရင်းကို စီရန်ကို လေ့လာပါ။
စာရင်းကို အက္ခရာစဉ်အလိုက်စီရန် ခလုတ်ကို နှိပ်ပါ-
- အော်စလို
- စတော့ဟုမ်း
- ဟယ်စင်ကီ
- ဘာလင်
- ပါခင်ဗျာ။
- မက်ဒရစ်
Sort Function ဖန်တီးခြင်း။
ဥပမာ
<ul id="id01">
<li>Oslo</li>
<li>Stockholm</li>
<li>Helsinki</li>
<li>Berlin</li>
<li>Rome</li>
<li>Madrid</li>
</ul>
<script>
function sortList() {
var list, i, switching, b, shouldSwitch;
list =
document.getElementById("id01");
switching = true;
/* Make
a loop that will continue until
no switching has been done: */
while (switching) {
// Start by saying: no switching is
done:
switching = false;
b =
list.getElementsByTagName("LI");
// Loop through all
list items:
for (i = 0; i < (b.length - 1); i++) {
// Start by saying there should be no switching:
shouldSwitch = false;
/* Check if the next
item should
switch place with the current
item: */
if (b[i].innerHTML.toLowerCase() >
b[i + 1].innerHTML.toLowerCase()) {
/* If next item is alphabetically lower than current item,
mark as a switch and break the loop: */
shouldSwitch = true;
break;
}
}
if (shouldSwitch) {
/* If a switch has been marked, make the switch
and mark the switch as done: */
b[i].parentNode.insertBefore(b[i + 1], b[i]);
switching = true;
}
}
}
</script>
Ascending နှင့် Descending ကို စီခြင်း
ခလုတ်ကို ပထမဆုံးအကြိမ် နှိပ်လိုက်သောအခါ စီခြင်းဦးတည်ချက်သည် အတက်အကျ (A မှ Z) ဖြစ်သည်။
ထပ်နှိပ်ပါ၊ စီစဥ်သည့် ဦးတည်ချက်သည် (Z မှ A မှ ဆင်းပါမည်)။
- အော်စလို
- စတော့ဟုမ်း
- ဟယ်စင်ကီ
- ဘာလင်
- ပါခင်ဗျာ။
- မက်ဒရစ်
ဥပမာ
<ul id="id01">
<li>Oslo</li>
<li>Stockholm</li>
<li>Helsinki</li>
<li>Berlin</li>
<li>Rome</li>
<li>Madrid</li>
</ul>
<script>
function sortListDir() {
var list, i, switching, b, shouldSwitch, dir, switchcount = 0;
list
= document.getElementById("id01");
switching = true;
// Set
the sorting direction to ascending:
dir = "asc";
// Make a
loop that will continue until no switching has been done:
while
(switching) {
// Start by saying: no switching is done:
switching = false;
b = list.getElementsByTagName("LI");
// Loop through all list-items:
for (i = 0; i < (b.length
- 1); i++) {
// Start by saying there should
be no switching:
shouldSwitch = false;
/* Check if the next item should switch place with the current item,
based on the sorting direction (asc or desc): */
if (dir == "asc") {
if (b[i].innerHTML.toLowerCase()
> b[i + 1].innerHTML.toLowerCase()) {
/* If next item is alphabetically lower than current item,
mark as a switch and break the loop: */
shouldSwitch = true;
break;
}
} else if (dir == "desc") {
if
(b[i].innerHTML.toLowerCase() < b[i + 1].innerHTML.toLowerCase()) {
/* If next item is alphabetically higher than current item,
mark as a switch and break the loop: */
shouldSwitch= true;
break;
}
}
}
if (shouldSwitch) {
/* If a switch has been marked, make the switch
and mark that a switch has been done: */
b[i].parentNode.insertBefore(b[i + 1], b[i]);
switching = true;
// Each time a switch is
done, increase switchcount by 1:
switchcount
++;
} else {
/* If no
switching has been done AND the direction is "asc",
set the direction to "desc" and run the while loop again. */
if (switchcount == 0 && dir == "asc") {
dir = "desc";
switching = true;
}
}
}
}
</script>
စာရင်းကို ဂဏန်းများဖြင့် စီပါ။
ဥပမာ
if (Number(b[i].innerHTML) > Number(b[i + 1].innerHTML)) {
shouldSwitch = true;
break;
}