我需要在单击标题链接时对表格进行排序。当点击链接第一次时间表应该 单击下一个时间表时按升序排序应按降序排序。我已经编写了 PHP 后端代码,它运行良好。 但我不知道如何在使用 Javascript 单击链接时传递所需的参数。
我的表格 html
<table class="table table-bordered table-striped">
<thead>
<tr>
<th><b>#</b></th>
<th><b id = "name_sort">Email</b> </th>
<th ><b >Name</b></th>
<th><b>Team</b> </th>
</tr>
</thead>
<tbody>
<tr>
<td></td>
<td></td>
<td></td>
</tr>
</tbody>
</table>
Javascript
$(function() {
$('#name_sort').click(function() {
window.location.href = "http://testdomain.com/admin-home?sort=email&sort_type=asc";
// when click again i need change the url to descending
http://testdomain.com/admin-home?sort=email&sort_type=desc
});
});
我不知道如何在前端使用 JS 实现它。请帮我解决这个问题。
最佳答案
你可以试试这个
PHP:首先获取下一个排序类型
默认为asc
$new_sort_type="asc"
if(isset($_REQUEST['sort_type']))
{
$sort_type = $_REQUEST['sort_type'];
if($sort_type=='asc')
{
$new_sort_type = 'desc';
}
}
jquery :现在将新排序类型
添加到jquery
中的url
,如下所示
$(function() {
$('#name_sort').click(function() {
window.location.href = "http://testdomain.com/admin-home?sort=email&sort_type=<?php echo $new_sort_type;?>";
^ here add that variable
});
});
关于javascript - 单击链接时如何对表格升序和降序排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25181539/