Javascript 单击链接时如何对表进行升序和降序排序
我需要在点击标题链接时对表格进行排序。第一次单击链接时,时间表应 按升序排序单击下一个时间表时,应按降序排序。我已经编写了PHP后端代码,它工作得很好。 但我不知道在使用Javascript单击链接时如何传递所需的参数 我的表格htmlJavascript 单击链接时如何对表进行升序和降序排序,javascript,php,sorting,Javascript,Php,Sorting,我需要在点击标题链接时对表格进行排序。第一次单击链接时,时间表应 按升序排序单击下一个时间表时,应按降序排序。我已经编写了PHP后端代码,它工作得很好。 但我不知道在使用Javascript单击链接时如何传递所需的参数 我的表格html <table class="table table-bordered table-striped"> <thead> <tr> <th><b>#&
<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>
$(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
});
});
$(函数(){
$('#name_sort')。单击(函数(){
window.location.href=”http://testdomain.com/admin-home?sort=email&sort_type=";
^在这里添加该变量
});
});
感谢您的快速回复。我不确定我处理分拣的方式。这样做对吗?如果有更好的办法,请告诉我。谢谢不,没有问题,但是你必须像上面用php那样正确地处理这个问题