排序箭头,升序,降序简单实现

简介:

css不好实现的效果,通过背景图片来弥补。

css

    <style>
        .sortedASC{
            background: url({sh::PUB}img/icon-table-sort-asc.png) no-repeat 80% 5px #eee;
        }
        .sorted
        {
            background: url({sh::PUB}img/icon-table-sort.png) no-repeat 80% 9px #eee ;
        }

        .sortedDESC{
            background: url({sh::PUB}img/icon-table-sort-desc.png) no-repeat 80% 11px #eee;
        }
    </style>

html

            <tr role="row">
                <th role="columnheader" tabindex="0" aria-controls="sample-table-2" rowspan="1" colspan="1" aria-label="Domain: activate to sort column ascending">默认</th>
                <th class="sort sortedASC" role="columnheader" tabindex="0" aria-controls="sample-table-2" rowspan="1" colspan="1" aria-label="Price: activate to sort column ascending">销量</th>
                <th class="sort sortedDESC" role="columnheader" tabindex="0" aria-controls="sample-table-2" rowspan="1" colspan="1" aria-label="Price: activate to sort column ascending">新品</th>
                <th class="sort sorted" role="columnheader" tabindex="0" aria-controls="sample-table-2" rowspan="1" colspan="1" aria-label="Update: activate to sort column ascending">
                    <i class="ace-icon fa fa-clock-o bigger-110 hidden-480"></i> 价格
                </th>
            </tr>

效果有了,剩下的就是通过js逻辑和程序逻辑来实现排序了。

实现功能

html改造

<th class="sort" data-type="default" role="columnheader" tabindex="0" aria-controls="sample-table-2" rowspan="1" colspan="1" aria-label="Domain: activate to sort column ascending">默认</th>
<th class="sort <if condition='$sale eq 1'>sortedASC<elseif condition='$sale eq -1'/>sortedDESC<else />sorted</if>" data-type="sale" role="columnheader" tabindex="0" aria-controls="sample-table-2" rowspan="1" colspan="1" aria-label="Price: activate to sort column ascending">销量</th>
<th class="sort <if condition='$new eq 1'>sortedASC<elseif condition='$new eq -1'/>sortedDESC<else />sorted</if>" data-type="new" role="columnheader" tabindex="0" aria-controls="sample-table-2" rowspan="1" colspan="1" aria-label="Price: activate to sort column ascending">新品</th>
<th class="sort <if condition='$price eq 1'>sortedASC<elseif condition='$price eq -1'/>sortedDESC<else />sorted</if>" data-type="price" role="columnheader" tabindex="0" aria-controls="sample-table-2" rowspan="1" colspan="1" aria-label="Update: activate to sort column ascending">
    <i class="ace-icon fa fa-clock-o bigger-110 hidden-480"></i> 价格
</th>

增加date-type,统一的class 'sort'。

js事件

$('.sort').click(function() {
    var type = $(this).data('type');
    var category_id = '{sh:$category_id}';
    var sort;
    if ($(this).hasClass('sorted')) { // 降序
        $(this).removeClass('sorted').addClass('sortedDESC');
        sort = '-1';
    }else if ($(this).hasClass('sortedASC')) { // 降序
        $(this).removeClass('sortedASC').addClass('sortedDESC');
        sort = '-1';
    }else if ($(this).hasClass('sortedDESC')) { // 升序
        $(this).removeClass('sortedDESC').addClass('sortedASC');
        sort = '1';
    }
    if (type =='default') {
        location.href="{sh::U('Store/Home/goodslist',array('category_id'=>'"+category_id+"'))}";
    } else {
        location.href="{sh::U('Store/Home/goodslist',array('category_id'=>'"+category_id+"','"+type+"'=>'"+sort+"'))}";
    }

    
});

后台处理

if ($sale = $this->_request('sale')) {
    if ($sale == '-1') {
        $order = '(salecount+fakemembercount) desc';
    }

    if ($sale == '1') {
        $order = '(salecount+fakemembercount) asc';
    }
    $this->assign('sale',$sale);
}

if ($price = $this->_request('price')) {
    if ($price == '-1') {
        $order = 'oprice desc';
    }

    if ($price == '1') {
        $order = 'oprice asc';
    }
    $this->assign('price',$price);
}

if ($new = $this->_request('new')) {
    if ($new == '-1') {
        $order = 'addtime desc';
    }

    if ($new == '1') {
        $order = 'addtime asc';
    }
    $this->assign('new',$new);
}

tips:这里是大概的思路,具体需要你们根据实际情况去实现,可以优化成异步加载。




本文转自TBHacker博客园博客,原文链接:http://www.cnblogs.com/jiqing9006/p/5743537.html如需转载请自行联系原作者

相关文章
|
Java 应用服务中间件 Maven
配置阿里的maven镜像
配置阿里的maven镜像
2033 0
|
运维 监控 Oracle
Java小史:JDK现状
简单说一下现在主流的JDK
3394 0
Java小史:JDK现状
|
监控 网络协议 安全
如何在 Debian 下配置邮件服务器
本教程将讨论如何在Debian(或Ubuntu)配置一个可工作的邮件服务器。我们知道在邮件服务器使用的主要协议有SMTP、POP和IMAP。在本教程中,SMTP协议使用postfix,POP/IMAP协议使用dovecot。
5322 0
|
10月前
|
监控 安全 Java
Spring Boot 中的 Actuator 是什么?
Spring Boot 中的 Actuator 是什么?
1789 6
|
JSON 算法 Java
SpringBoot 实现接口参数加密解密功能
SpringBoot 实现接口参数加密解密功能
643 0
|
网络安全 数据安全/隐私保护 Windows
[笔记] Windows VBS脚本实现自动输入 解放双手 自动测试
[笔记] Windows VBS脚本实现自动输入 解放双手 自动测试
513 0
vscode 打开csv乱码
vscode 打开csv乱码
2892 0
|
大数据 关系型数据库 MySQL
MYSQL中group_concat有长度限制!默认1024(转载)
在mysql中,有个函数叫“group_concat”,平常使用可能发现不了问题,在处理大数据的时候,会发现内容被截取了,其实MYSQL内部对这个是有设置的,默认不设置的长度是1024,如果我们需要更大,就需要手工去修改配置文件。
5041 0
这几款 IDEA 主题也太好看了吧,百看不腻
在这篇文章中,我精选了几个比较适合 Java 编码的 IDEA 主题供小伙伴们选择。另外,我自己用的是 One Dark theme 这款。 注意:以下主题按照使用人数降序排序。推荐指数仅为个人看法。