我正在使用 CodeIgniter 以各自的代理名稱檢索表中的資料計數。我通過使用以下陳述句來做到這一點:
$sql = SELECT count(*) AS cnt
FROM table 1 t LEFT JOIN user_table c ON t.agent_id = c.id
GROUP BY COALESCE(t.agent_id, 0), c.display_name
ORDER BY c.display_name IS NULL, c.display_name;
$query = $this->db->query($sql);
return $query;
這給了我以下輸出,從名稱的角度給出了來自 AZ 的表資料:
在此處輸入圖片說明
現在,當我點擊表格的標題時,我希望能夠以 ASC 和 DESC 的順序過濾這些資料。這是我的表的代碼:
<thead>
<tr>
<th><div onclick=//Some method>Agent</div></th>
<th><div onclick=//Some method>Count</div></th>
</tr>
</thead>
<?php
if(isset($agent_count) && count($agent_count) > 0)
{
foreach($agent_count as $row ){
?>
<tr>
<td><?= $row->name ?></td>
<td><?= $row->cnt ?></td>
</tr>
<?php } } ?>
因此,這里要對資料進行排序,我假設需要進行 AJAX 呼叫以在 DESC 中排序資料(如果它已經是 ASC),或者如果它已經是 DESC 則呼叫 ASC。此外,如果我單擊計數標題以按 DESC 順序獲取該資料,則代理列也應根據其各自的資料進行相應排序。
uj5u.com熱心網友回復:
您可以使用 javascript 實作相同的目的。沒有必要再做一次ajax呼叫。您只需從服務器獲取一次資料,然后使用 javascript 對表格進行排序。
來源:在標題單擊時對 HTML 表格進行排序
function sortTable(n) {
var table, rows, switching, i, x, y, shouldSwitch, dir, switchcount = 0;
table = document.getElementById("myTable");
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;
rows = table.rows;
/*Loop through all table rows (except the
first, which contains table headers):*/
for (i = 1; i < (rows.length - 1); i ) {
//start by saying there should be no switching:
shouldSwitch = false;
/*Get the two elements you want to compare,
one from current row and one from the next:*/
x = rows[i].getElementsByTagName("TD")[n];
y = rows[i 1].getElementsByTagName("TD")[n];
/*check if the two rows should switch place,
based on the direction, asc or desc:*/
if (dir == "asc") {
if (x.innerHTML.match(/^-?\d $/) && y.innerHTML.match(/^-?\d $/)) {
if (Number(x.innerHTML) > Number(y.innerHTML)) {
//if so, mark as a switch and break the loop:
shouldSwitch = true;
break;
}
} else if (x.innerHTML.match(/^\d \.\d $/) && y.innerHTML.match(/^\d \.\d $/)) {
if (Number(x.innerHTML) > Number(y.innerHTML)) {
//if so, mark as a switch and break the loop:
shouldSwitch = true;
break;
}
} else {
if (x.innerHTML.toLowerCase() > y.innerHTML.toLowerCase()) {
//if so, mark as a switch and break the loop:
shouldSwitch = true;
break;
}
}
} else if (dir == "desc") {
if (x.innerHTML.match(/^-?\d $/) && y.innerHTML.match(/^-?\d $/)) {
if (Number(x.innerHTML) < Number(y.innerHTML)) {
//if so, mark as a switch and break the loop:
shouldSwitch = true;
break;
}
} else if (x.innerHTML.match(/^\d \.\d $/) && y.innerHTML.match(/^\d \.\d $/)) {
if (Number(x.innerHTML) < Number(y.innerHTML)) {
//if so, mark as a switch and break the loop:
shouldSwitch = true;
break;
}
} else {
if (x.innerHTML.toLowerCase() < y.innerHTML.toLowerCase()) {
//if so, 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:*/
rows[i].parentNode.insertBefore(rows[i 1], rows[i]);
switching = true;
//Each time a switch is done, increase this count 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;
}
}
}
}
table {
border-spacing: 0;
width: 100%;
border: 1px solid #ddd;
}
th {
cursor: pointer;
}
th, td {
text-align: left;
padding: 16px;
}
tr:nth-child(even) {
background-color: #f2f2f2
}
<!DOCTYPE html>
<html>
<head>
<title>Sort a HTML Table Alphabetically</title>
</head>
<body>
<table id="myTable">
<tr>
<!--When a header is clicked, run the sortTable function, with a parameter, 0 for sorting by names, 1 for sorting by country:-->
<th onclick="sortTable(0)">Name</th>
<th onclick="sortTable(1)">Country</th>
<th onclick="sortTable(2)">Count</th>
<th onclick="sortTable(3)">Rating</th>
</tr>
<tr>
<td>Berglunds snabbkop</td>
<td>Sweden</td>
<td>5</td>
<td>4.5</td>
</tr>
<tr>
<td>North/South</td>
<td>UK</td>
<td>3</td>
<td>3</td>
</tr>
<tr>
<td>Alfreds Futterkiste</td>
<td>Germany</td>
<td>1</td>
<td>2.8</td>
</tr>
<tr>
<td>Koniglich Essen</td>
<td>Germany</td>
<td>78</td>
<td>0.9</td>
</tr>
<tr>
<td>Magazzini Alimentari Riuniti</td>
<td>Italy</td>
<td>5</td>
<td>4.5</td>
</tr>
<tr>
<td>Paris specialites</td>
<td>France</td>
<td>7</td>
<td>1.3</td>
</tr>
<tr>
<td>Island Trading</td>
<td>UK</td>
<td>0</td>
<td>1.2</td>
</tr>
<tr>
<td>Laughing Bacchus Winecellars</td>
<td>Canada</td>
<td>2</td>
<td>3.5</td>
</tr>
</table>
</body>
</html>
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/326287.html
標籤:阿贾克斯
上一篇:Chrome擴展::嘗試將chrome.runtime.sendmessage的語法理解為chrome.runtime.onMessage
下一篇:ajaxjson表分組行
