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 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据