纯CSS实现表格排序-利用CSS 变量和Flexbox

1.引子

今天闲逛知名前端资讯站Front-End Front,发现一个比较有意思的效果,给大家分享下,希望可以对大家有所启发。

纯CSS实现表格排序

心急的同学,先看效果,我放在codepen上。


codepen上预览、编辑效果


本案例用到了以下知识点:

  • CSS变量
  • Flexbox布局
  • grid布局(非必须,可以替代)

因此,本案例只在现代浏览器中运行良好。

2.核心原理

我们利用Flexbox的方式打破表格布局,让表格做Flex容器(container),每一行做Flex子项(item),然后利用CSS变量的方式设置order。

.table-body {
  display: flex;
  flex-direction: column;
}

.table-row {
  order: calc(var(--order) * var(--sort-order, -1));
}

另外,为了实现纯CSS,我们用到了input的:checked伪类模拟实现单击。

#sort-by-published:checked ~ .table > .table-body > .table-row {
  --order: var(--order-by-published);
}

#sort-by-views:checked ~ .table > .table-body > .table-row {
  --order: var(--order-by-views);
}

#sort-ascending:checked + .table {
  --sort-order: 1;
}

3.实现步骤

3.1 HTML

为了实现单击,我们使用input+label的方式调用:checked伪类。首先,我们需要两组input。

  <!--  :checked实现单击,这些input不在网页中显示  -->
  <!--  name为sort的input,用来控制排序字段  -->
  <input type="radio" name="sort" id="sort-by-name">
  <input type="radio" name="sort" id="sort-by-published" checked="checked">
  <input type="radio" name="sort" id="sort-by-views">
  <!--  name为sort-order的input,用来控制排序方式,升序还是降序  -->
  <input type="radio" name="sort-order" id="sort-descending" checked="checked">
  <input type="radio" name="sort-order" id="sort-ascending">

然后是label。

<table class="table">
    <thead class="table-head">
      <tr class="table-row">
        <th class="table-cell">
          <label class="table-sorter" for="sort-by-name">文章</label>
          <label class="table-orderer" for="sort-ascending" data-filtered="filtered">↓</label>
          <label class="table-orderer" for="sort-descending" data-filtered="filtered">↑</label>
        </th>
        <th class="table-cell">
          <label class="table-sorter" for="sort-by-published">出版</label>
          <label class="table-orderer" for="sort-ascending">↓</label>
          <label class="table-orderer" for="sort-descending" data-filtered="filtered">↑</label>
        </th>
        <th class="table-cell">
          <label class="table-sorter" for="sort-by-views">浏览</label>
          <label class="table-orderer" for="sort-ascending" data-filtered="filtered">↓</label>
          <label class="table-orderer" for="sort-descending" data-filtered="filtered">↑</label>
        </th>
      </tr>
    </thead>
    <tbody class="table-body">
    </tbody>
  </table>

接下去是表格主题内容。

   <tbody class="table-body">
      <tr class="table-row" style="--order-by-published: 161021; --order-by-views: 10368;">
        <th class="table-cell">Conditions for CSS Variables</th>
        <td class="table-cell">2016-10-21</td>
        <td class="table-cell">10&thinsp;368</td>
      </tr>
      <tr class="table-row" style="--order-by-published: 161221; --order-by-views: 2431;">
        <th class="table-cell">Controlling the Specificity</th>
        <td class="table-cell">2016-12-21</td>
        <td class="table-cell">2&thinsp;431</td>
      </tr>
      <tr class="table-row" style="--order-by-published: 180104; --order-by-views: 4463;">
        <th class="table-cell">Counters and Stones</th>
        <td class="table-cell">2018-01-04</td>
        <td class="table-cell">4&thinsp;463</td>
      </tr>
      <tr class="table-row" style="--order-by-published: 171128; --order-by-views: 6585;">
        <th class="table-cell">Flexible Overflow</th>
        <td class="table-cell">2017-11-28</td>
        <td class="table-cell">6&thinsp;585</td>
      </tr>
      <tr class="table-row" style="--order-by-published: 170627; --order-by-views: 4597;">
        <th class="table-cell">Keyboard-Only Focus</th>
        <td class="table-cell">2017-06-27</td>
        <td class="table-cell">4&thinsp;597</td>
      </tr>
      <tr class="table-row" style="--order-by-published: 170531; --order-by-views: 2829;">
        <th class="table-cell">Label-to-Input States</th>
        <td class="table-cell">2017-05-31</td>
        <td class="table-cell">2&thinsp;829</td>
      </tr>
    </tbody>

3.2 CSS

/* 关键代码 */

.table-body {
  display: flex;
  flex-direction: column;
}

.table-row {
  order: calc(var(--order) * var(--sort-order, -1));
}

#sort-by-published:checked ~ .table > .table-body > .table-row {
  --order: var(--order-by-published);
}

#sort-by-views:checked ~ .table > .table-body > .table-row {
  --order: var(--order-by-views);
}

#sort-ascending:checked + .table {
  --sort-order: 1;
}

/* 反向排序 */
#sort-by-name:checked ~ #sort-ascending:checked + .table > .table-body {
  flex-direction: column-reverse;
}

以及其他布局和样式方面的代码

/* 其他代码 */

.table-wrapper > input {
  position: fixed;
  left: 0;
  right: 0;
  clip: rect(1px,1px,1px,1px);
  visibility: hidden;
}

.table,
.table-caption,
.table-head {
  display: block;
  margin: 0;
}

table,td,th{
  border: 1px solid #000;
  border-collapse:collapse;
}

a{
  text-decoration: none;
  color:black;
}

.table-row {
  position: relative;
  display: grid;
  grid-template-columns: 50% 25% 25%;
}

.table-cell {
  text-align: right;
  padding: 0.25em 0.5em;
  white-space: nowrap;
}

.table-cell:first-child {
  text-align: left;
  overflow: hidden;
  text-overflow: ellipsis;
}

.table-orderer {
  display: none;
}

.table-head .table-cell {
  position: relative;
  display: flex;
  user-select: none;
}

.table-head .table-cell:hover {
  background: rgba(0,0,0,0.05);
}

.table-head .table-cell:not(:first-child) {
  flex-direction: row-reverse;
}

.table-sorter:before,
.table-orderer:before {
  content: "";
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  cursor: pointer;
}

#sort-by-name:checked ~ .table .table-sorter[for=sort-by-name] ~ .table-orderer,
#sort-by-published:checked ~ .table .table-sorter[for=sort-by-published] ~ .table-orderer,
#sort-by-views:checked ~ .table .table-sorter[for=sort-by-views] ~ .table-orderer {
  display: inline;
}

#sort-ascending:checked + .table .table-orderer[for=sort-ascending],
#sort-ascending:not(:checked) + .table .table-orderer[for=sort-descending] {
  display: none !important;
}

3.3 整体源码

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8" />
        <title>纯CSS实现表格排序-利用CSS 变量和Flexbox</title>
        <style type="text/css">
            /* 其他代码 */
            
            .table-wrapper>input {
                position: fixed;
                left: 0;
                right: 0;
                clip: rect(1px, 1px, 1px, 1px);
                visibility: hidden;
            }
            
            .table,
            .table-caption,
            .table-head {
                display: block;
                margin: 0;
            }
            
            table,
            td,
            th {
                border: 1px solid #000;
                border-collapse: collapse;
            }
            
            a {
                text-decoration: none;
                color: black;
            }
            
            .table-row {
                position: relative;
                display: grid;
                grid-template-columns: 50% 25% 25%;
            }
            
            .table-cell {
                text-align: right;
                padding: 0.25em 0.5em;
                white-space: nowrap;
            }
            
            .table-cell:first-child {
                text-align: left;
                overflow: hidden;
                text-overflow: ellipsis;
            }
            
            .table-orderer {
                display: none;
            }
            
            .table-head .table-cell {
                position: relative;
                display: flex;
                user-select: none;
            }
            
            .table-head .table-cell:hover {
                background: rgba(0, 0, 0, 0.05);
            }
            
            .table-head .table-cell:not(:first-child) {
                flex-direction: row-reverse;
            }
            
            .table-sorter:before,
            .table-orderer:before {
                content: "";
                position: absolute;
                top: 0;
                left: 0;
                right: 0;
                bottom: 0;
                cursor: pointer;
            }
            
            #sort-by-name:checked~.table .table-sorter[for=sort-by-name]~.table-orderer,
            #sort-by-published:checked~.table .table-sorter[for=sort-by-published]~.table-orderer,
            #sort-by-views:checked~.table .table-sorter[for=sort-by-views]~.table-orderer {
                display: inline;
            }
            
            #sort-ascending:checked+.table .table-orderer[for=sort-ascending],
            #sort-ascending:not(:checked)+.table .table-orderer[for=sort-descending] {
                display: none !important;
            }
        </style>
    </head>
    <body>
        <div class="table-wrapper">
            <!--  :checked实现单击,这些input不在网页中显示  -->
            <input type="radio" name="sort" id="sort-by-name">
            <input type="radio" name="sort" id="sort-by-published" checked="checked">
            <input type="radio" name="sort" id="sort-by-views">
            <input type="radio" name="sort-order" id="sort-descending" checked="checked">
            <input type="radio" name="sort-order" id="sort-ascending">
            <table class="table">
                <thead class="table-head">
                    <tr class="table-row">
                        <th class="table-cell">
                            <label class="table-sorter" for="sort-by-name">文章</label>
                            <label class="table-orderer" for="sort-ascending" data-filtered="filtered">↓</label>
                            <label class="table-orderer" for="sort-descending" data-filtered="filtered">↑</label>
                        </th>
                        <th class="table-cell">
                            <label class="table-sorter" for="sort-by-published">出版</label>
                            <label class="table-orderer" for="sort-ascending">↓</label>
                            <label class="table-orderer" for="sort-descending" data-filtered="filtered">↑</label>
                        </th>
                        <th class="table-cell">
                            <label class="table-sorter" for="sort-by-views">浏览</label>
                            <label class="table-orderer" for="sort-ascending" data-filtered="filtered">↓</label>
                            <label class="table-orderer" for="sort-descending" data-filtered="filtered">↑</label>
                        </th>
                    </tr>
                </thead>
                <tbody class="table-body">
                    <tr class="table-row" style="--order-by-published: 161021; --order-by-views: 10368;">
                        <th class="table-cell">Conditions for CSS Variables</th>
                        <td class="table-cell">2016-10-21</td>
                        <td class="table-cell">10&thinsp;368</td>
                    </tr>
                    <tr class="table-row" style="--order-by-published: 161221; --order-by-views: 2431;">
                        <th class="table-cell">Controlling the Specificity</th>
                        <td class="table-cell">2016-12-21</td>
                        <td class="table-cell">2&thinsp;431</td>
                    </tr>
                    <tr class="table-row" style="--order-by-published: 180104; --order-by-views: 4463;">
                        <th class="table-cell">Counters and Stones</th>
                        <td class="table-cell">2018-01-04</td>
                        <td class="table-cell">4&thinsp;463</td>
                    </tr>
                    <tr class="table-row" style="--order-by-published: 171128; --order-by-views: 6585;">
                        <th class="table-cell">Flexible Overflow</th>
                        <td class="table-cell">2017-11-28</td>
                        <td class="table-cell">6&thinsp;585</td>
                    </tr>
                    <tr class="table-row" style="--order-by-published: 170627; --order-by-views: 4597;">
                        <th class="table-cell">Keyboard-Only Focus</th>
                        <td class="table-cell">2017-06-27</td>
                        <td class="table-cell">4&thinsp;597</td>
                    </tr>
                    <tr class="table-row" style="--order-by-published: 170531; --order-by-views: 2829;">
                        <th class="table-cell">Label-to-Input States</th>
                        <td class="table-cell">2017-05-31</td>
                        <td class="table-cell">2&thinsp;829</td>
                    </tr>
                </tbody>
            </table>
        </div>
    </body>
</html>

4.声明

爱前端,乐分享。FedFun希望与您共同进步。
欢迎任何形式的转载,烦请注明装载,保留本段文字。
独立博客http://whqet.github.io
极客头条http://geek.csdn.net/user/publishlist/whqet
CSDN博客http://blog.csdn.net/whqet/
我的简书//www.greatytc.com/u/c11d4318b3c7

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 217,509评论 6 504
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,806评论 3 394
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 163,875评论 0 354
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,441评论 1 293
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,488评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,365评论 1 302
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,190评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,062评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,500评论 1 314
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,706评论 3 335
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,834评论 1 347
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,559评论 5 345
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,167评论 3 328
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,779评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,912评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,958评论 2 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,779评论 2 354

推荐阅读更多精彩内容

  • 问答题47 /72 常见浏览器兼容性问题与解决方案? 参考答案 (1)浏览器兼容问题一:不同浏览器的标签默认的外补...
    _Yfling阅读 13,751评论 1 92
  • H5移动端知识点总结 阅读目录 移动开发基本知识点 calc基本用法 box-sizing的理解及使用 理解dis...
    Mx勇阅读 4,496评论 0 26
  • 2018年2月23日 星期五 天气 多云 时间真是过的很快,觉得还没在家呆多久,就又要准备离开。不过还好,还有些日...
    602f5c8ad9da阅读 485评论 0 5
  • 死宅的我,在凉凉的夜色里,一个人漫步于人海中、车水马龙间:一是出来疏散疏散心情,二是于人世的烟火里感受别人的喜...
    木子米尔阅读 315评论 0 0
  • 白如晶玉的雪花在空中洋洋洒洒地飘着,坐在归家的车上面,我看着就像蒙着灰纱的天空心中不由得悸动着。‘这就是近乡情更怯...
    暮轩阅读 629评论 2 7