所以我有使用php回显的表。我从数据库中获取数据,然后填充表格。现在我有数据不正常。所以我的桌子不整齐这是这样的表
product | quantity | amount
===========================
Samosa | 2 | 20
Shwarma | 1 | 50
Fries | 1 | 30
我想使用任何方法javascript,jquery,php重新排序表格。 我希望金额按这种降序排列
product | quantity | amount
===========================
Shwarma | 1 | 50
Fries | 1 | 30
Samosa | 2 | 20
要注意的事情是1)我在获取数据后计算了金额,因此无法使用mysql ORDER BY
对其进行排序2)我使用ajax请求获取并填充数据。
我该怎么做?
答案 0 :(得分:0)
您可以使用uasort在PHP中进行操作:
uasort($data, function ($a, $b) {
return $a['quantity '] > $b['quantity '];
});
答案 1 :(得分:0)
可能下面的代码对您有用。根据您的要求,我在代码行中输入代码。
var data = await http.get("https://www.googleapis.com/youtube/v3/search?part=snippet&channelId={id}&maxResults=5&order=date&type=video&key={key}");
var jsonData = json.decode(data.body);
return Result.fromJson(jsonData);
代码输出为
输入
$myData = array(
array(
'product' => 'Samosa',
'quantity' => '2',
'amount' => '20',
),
array(
'product' => 'Shwarma',
'quantity' => '1',
'amount' => '50',
),
array(
'product' => 'Fries',
'quantity' => '1',
'amount' => '30',
)
);
echo '<pre>';
print_r($myData);
uasort($myData, function($a, $b) {
return $a['amount'] < $b['amount'];
});
print_r($myData);
die;
结果:
Array
(
[0] => Array
(
[product] => Samosa
[quantity] => 2
[amount] => 20
)
[1] => Array
(
[product] => Shwarma
[quantity] => 1
[amount] => 50
)
[2] => Array
(
[product] => Fries
[quantity] => 1
[amount] => 30
)
)
答案 2 :(得分:0)
此功能对我有用。
function sortTable() {
var table, rows, switching, i, x, y, shouldSwitch;
table = document.getElementById("myTable");
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;
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")[0];
y = rows[i + 1].getElementsByTagName("TD")[0];
// Check if the two rows should switch place:
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;
}
}
}