ਕਿਵੇਂ ਟੇਬਲ ਸੰਗਠਿਤ ਕਰਨ?
JavaScript ਨਾਲ HTML ਟੇਬਲ ਨੂੰ ਸੰਗਠਿਤ ਕਰਨ ਨੂੰ ਸਿੱਖੋ。
ਬਟਨ ਨੂੰ ਕਲਿੱਕ ਕਰੋ, ਗੈਰ-ਕਿਸਮ ਕਰਕੇ ਕਸਟਮਰ ਨਾਮ ਦੇ ਅਕਸਰਤਾ ਅਨੁਸਾਰ ਟੇਬਲ ਨੂੰ ਸੰਗਠਿਤ ਕਰੋ:
Name | Country |
---|---|
Berglunds snabbkop | Sweden |
North/South | UK |
Alfreds Futterkiste | Germany |
Koniglich Essen | Germany |
Magazzini Alimentari Riuniti | Italy |
Paris specialites | France |
Island Trading | UK |
Laughing Bacchus Winecellars | Canada |
ਸੰਗਠਨ ਫੰਕਸ਼ਨ ਬਣਾਓ
ਉਦਾਹਰਣ
function sortTable() { var table, rows, switching, i, x, y, shouldSwitch; table = document.getElementById("myTable"); switching = true; /* ایک سیکل بنائیگا جس میں شرط پورا ہوگا جب تک. */ no switching has been done: */ while (switching) { // 首先声明:当前没有行需要交换: switching = false; rows = table.rows; /* تمام رینک کو بچھاؤ (پہلا رینک کو چھپاوا، جس میں سرکردہ ہے): */ for (i = 1; i < (rows.length - 1); i++) { // ابتدا تبدیل نہیں کریگن: shouldSwitch = false; /* مقصد کریگن کو بچھاؤ، ایک سے ایک رینک سے: */ x = rows[i].getElementsByTagName("TD")[0]; y = rows[i + 1].getElementsByTagName("TD")[0]; // دو رینک کو تبدیل کرنا چاہئیے یا نہیں پچات: else if (dir == "desc") { // اگر تبدیل کرنا چاہئیے تو، وہ کوئی تبدیل نہیں کیا گیا: shouldSwitch = true; break; } } if (shouldSwitch) { /* ਜੇਕਰ ਵਟਾਂਦਰਾ ਦੀ ਮਦਦ ਕੀਤੀ ਗਈ ਹੈ, ਤਾਂ ਵਟਾਂਦਰਾ ਕਰੋ ਅਤੇ ਇਸ ਨੂੰ ਇਸ ਤਰ੍ਹਾਂ ਦੱਸੋ: */ rows[i].parentNode.insertBefore(rows[i + 1], rows[i]); switching = true; } } }
تیبل کو کلک کرکے ترتیب کریگن
‘Name’ پر کلک کریگن نام کی ترتیب کریگن، ‘Country’ پر کلک کریگن ملک کی ترتیب کریگن کریگن。
پچات کریگن کی پہلی بار، ترتیب کمانی (A تا Z) ہوگی。
دوبار پچات کریگن، ترتیب کمانی (Z تا A) ہوگا:
Name | Country |
---|---|
Berglunds snabbkop | Sweden |
North/South | UK |
Alfreds Futterkiste | Germany |
Koniglich Essen | Germany |
Magazzini Alimentari Riuniti | Italy |
Paris specialites | France |
Island Trading | UK |
Laughing Bacchus Winecellars | Canada |
ਉਦਾਹਰਣ
<table id="myTable2"> <tr> <!--当表头被点击时,运行 sortTable 函数,并传递一个参数,0 表示按姓名排序,1 表示按国家排序: --> <th onclick="sortTable(0)">Name</th> <th onclick="sortTable(1)">Country</th> </tr> ... <script> function sortTable(n) { var table, rows, switching, i, x, y, shouldSwitch, dir, switchcount = 0; table = document.getElementById("myTable2"); switching = true; // 设置排序方向为升序: dir = "asc"; /* 创建一个循环,该循环将持续进行,直到没有行需要交换为止: */ while (switching) { // 首先声明:当前没有行需要交换: switching = false; rows = table.rows; /* 遍历所有表格行(除了第一行,因为它包含表头): */ for (i = 1; i < (rows.length - 1); i++) { // 首先声明:当前行与下一行不应该交换位置: shouldSwitch = false; x = rows[i].getElementsByTagName("TD")[n]; y = rows[i + 1].getElementsByTagName("TD")[n]; /* ਇਨ੍ਹਾਂ ਦੋ ਕਰਣਾਂ ਨੂੰ ਵਟਾਂਦਰਾ ਹੋਣਾ ਚਾਹੀਦਾ ਹੈ ਕਿ ਨਹੀਂ, ਇਹ ਕਰਣ ਦੀ ਦਿਸ਼ਾ ਦੇ ਅਧਾਰ 'ਉੱਚ' ਜਾਂ 'ਘੱਟ' ਦੇ ਅਨੁਸਾਰ ਹੈ: */ if (dir == "asc") { if (x.innerHTML.toLowerCase() > y.innerHTML.toLowerCase()) { else if (dir == "desc") { // ਜੇਕਰ ਵਟਾਂਦਰਾ ਹੋਣਾ ਚਾਹੀਦਾ ਹੈ, ਤਾਂ ਇਸ ਨੂੰ ਇਸ ਤਰ੍ਹਾਂ ਦੱਸੋ ਅਤੇ ਮੌਜੂਦਾ ਚੱਕਰ ਬਾਹਰ ਨਿਕਲੋ: shouldSwitch = true; break; } } if (x.innerHTML.toLowerCase() < y.innerHTML.toLowerCase()) { // ਜੇਕਰ ਵਟਾਂਦਰਾ ਹੋਣਾ ਚਾਹੀਦਾ ਹੈ, ਤਾਂ ਇਸ ਨੂੰ ਇਸ ਤਰ੍ਹਾਂ ਦੱਸੋ ਅਤੇ ਮੌਜੂਦਾ ਚੱਕਰ ਬਾਹਰ ਨਿਕਲੋ: shouldSwitch = true; break; } } } if (shouldSwitch) { /* ਜੇਕਰ ਵਟਾਂਦਰਾ ਦੀ ਮਦਦ ਕੀਤੀ ਗਈ ਹੈ, ਤਾਂ ਵਟਾਂਦਰਾ ਕਰੋ ਅਤੇ ਇਸ ਨੂੰ ਇਸ ਤਰ੍ਹਾਂ ਦੱਸੋ: */ rows[i].parentNode.insertBefore(rows[i + 1], rows[i]); switching = true; // ਹਰ ਵਾਰ ਜਦੋਂ ਵਟਾਂਦਰਾ ਹੋਇਆ ਤਾਂ ਇਸ ਗਿਣਤੀ ਵਿੱਚ 1 ਜੋੜੋ: switchcount ++; } /* ਜੇਕਰ ਕੋਈ ਵਟਾਂਦਰਾ ਨਹੀਂ ਹੋਇਆ ਅਤੇ ਕਰਣ ਦੀ ਦਿਸ਼ਾ "ਉੱਚ" ਹੈ, ਤਾਂ ਕਰਣ ਦੀ ਦਿਸ਼ਾ "ਘੱਟ" ਰੱਖੋ ਅਤੇ ਇੱਕ ਵਾਰ ਫਿਰ while ਚੱਕਰ ਚਲਾਓ. */ if (switchcount == 0 && dir == "asc") { dir = "desc"; switching = true; } } } } </script>
ਸਾਰੇ ਪੈਟਰਨਾਂ ਨੂੰ ਨੰਬਰਾਂ ਦੇ ਅਨੁਸਾਰ ਕਰੋ
ਉਦਾਹਰਣ
if (Number(x.innerHTML) > Number(y.innerHTML)) { shouldSwitch = true; break; }