douzhu1188 2014-08-07 11:37
浏览 61
已采纳

单击链接时如何对表升序和降序进行排序

I have a requirement to sort a table when click on title link. When click the link first time table should sort ascending order when click on next time table should sort in descending order. I have written PHP back end code it was working fine. But i don't know how to pass the required parameter when click on link using 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
    });
    });

I dont know how to implement it in Front end using JS. Please help me to solve this issue.

  • 写回答

1条回答 默认 最新

  • dreamworld2007 2014-08-07 11:43
    关注

    you can try this

    PHP : first get the next sorting type default is asc

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

    jquery : now add the new sorting type to your url in jquery like below

    $(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
        });
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题