簡體   English   中英

單擊鏈接時如何對表的升序和降序進行排序

[英]How to sort table ascending and descending when click on link

點擊標題鏈接時,我需要對表格進行排序。 單擊鏈接時,第一個時間表應按升序排序。單擊下一個時間表時,應按降序排序。 我已經寫了PHP后端代碼,它工作正常。 但是我不知道如何在使用Java單擊鏈接時如何傳遞所需的參數。

我的表格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添加到您在jqueryurl中,如下所示

$(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
    });
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM