如何排序表升序和降序时,单击链接

How to sort table ascending and descending when click on link

本文关键字:降序 链接 单击 升序 何排序 排序      更新时间:2023-09-26

我有一个要求排序表时,单击标题链接。当点击链接时,首先时间表应该按升序排序时,单击下一个时间表应按降序排序。我写了PHP后端代码,它工作得很好。但我不知道如何传递所需的参数时,点击使用Javascript的链接。

My table 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:首先获取next sorting type默认为asc

$new_sort_type="asc"
if(isset($_REQUEST['sort_type']))
{
   $sort_type = $_REQUEST['sort_type'];
   if($sort_type=='asc')
   {
         $new_sort_type = 'desc';
   }
}

jquery:现在将new sorting type添加到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
    });
});