လုပ်နည်း - စစ်ထုတ်ခြင်း/ရှာဖွေမှုစာရင်း
JavaScript ဖြင့် filter list တစ်ခုဖန်တီးနည်းကို လေ့လာပါ။
စာရင်းစစ်
စာရင်းတစ်ခုရှိအရာများကိုရှာဖွေရန် JavaScript ကိုအသုံးပြုပုံ။
ရှာဖွေမှုစာရင်းဖန်တီးပါ။
အဆင့် 1) HTML ကိုထည့်ပါ
ဥပမာ
<input type="text" id="myInput" onkeyup="myFunction()" placeholder="Search
for names..">
<ul id="myUL">
<li><a href="#">Adele</a></li>
<li><a href="#">Agnes</a></li>
<li><a href="#">Billy</a></li>
<li><a href="#">Bob</a></li>
<li><a href="#">Calvin</a></li>
<li><a href="#">Christina</a></li>
<li><a href="#">Cindy</a></li>
</ul>
မှတ်ချက်- ကျွန်ုပ်တို့တွင် ၎င်းကို လင့်ခ်ချိတ်ရန် စာမျက်နှာမရှိသောကြောင့် ဤသရုပ်ပြတွင် ကျွန်ုပ်တို့သည် href="#" ကို အသုံးပြုပါသည်။ လက်တွေ့ဘဝတွင် ၎င်းသည် သီးခြားစာမျက်နှာတစ်ခုသို့ တကယ့် URL ဖြစ်သင့်သည်။
အဆင့် 2) CSS ကိုထည့်ပါ
ထည့်သွင်းသည့်ဒြပ်စင်နှင့် စာရင်းပုံစံကို ပုံစံသတ်မှတ်ပါ-
ဥပမာ
#myInput {
background-image: url('/css/searchicon.png');
/* Add a search icon to input */
background-position:
10px 12px; /* Position the search icon */
background-repeat: no-repeat; /* Do not repeat the icon image */
width: 100%; /* Full-width */
font-size: 16px; /*
Increase font-size */
padding: 12px 20px 12px 40px; /*
Add some padding */
border: 1px solid #ddd; /* Add a
grey border */
margin-bottom: 12px; /* Add some space
below the input */
}
#myUL {
/* Remove default list styling */
list-style-type:
none;
padding: 0;
margin: 0;
}
#myUL li a {
border: 1px solid #ddd; /* Add a border to all links */
margin-top: -1px; /* Prevent double borders */
background-color: #f6f6f6; /* Grey background color */
padding: 12px; /* Add some padding */
text-decoration:
none; /* Remove default text underline */
font-size:
18px; /* Increase the font-size */
color: black; /*
Add a black text color */
display: block; /* Make it
into a block element to fill the whole list */
}
#myUL
li a:hover:not(.header) {
background-color: #eee; /*
Add a hover effect to all links, except for headers */
}
အဆင့် 3) JavaScript ကိုထည့်ပါ။
ဥပမာ
<script>
function myFunction() {
// Declare variables
var input, filter,
ul, li, a, i, txtValue;
input = document.getElementById('myInput');
filter = input.value.toUpperCase();
ul =
document.getElementById("myUL");
li =
ul.getElementsByTagName('li');
// Loop through all
list items, and hide those who don't match the search query
for (i = 0; i <
li.length; i++) {
a = li[i].getElementsByTagName("a")[0];
txtValue = a.textContent || a.innerText;
if (txtValue.toUpperCase().indexOf(filter) > -1) {
li[i].style.display = "";
}
else {
li[i].style.display = "none";
}
}
}
</script>
အကြံပြုချက်- case-sensitive ရှာဖွေမှုကို လုပ်ဆောင်လိုပါက toUpperCase() ကို ဖယ်ရှား ပါ။
အကြံပြုချက်- Filter Table ကိုလည်း စစ်ဆေးကြည့်ပါ ။