Vue实现排序功能

 

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Vue测试</title>
    <script type="text/javascript" src='https://cdn.jsdelivr.net/npm/vue@0.11/dist/vue.min.js'></script>
    <script type="text/javascript" src='jquery.js'></script>
    <style>
        body {
          font-family: Helvetica Neue, Arial, sans-serif;
          font-size: 14px;
          color: #444;
        }
 
        table {
          border: 2px solid #42b983;
          border-radius: 3px;
          background-color: #fff;
        }
 
        th {
          background-color: #42b983;
          color: rgba(255,255,255,0.66);
          cursor: pointer;
          -webkit-user-select: none;
          -moz-user-select: none;
          -user-select: none;
        }
 
        td {
          background-color: #f9f9f9;
        }
 
        th, td {
          min-width: 120px;
          padding: 10px 20px;
        }
 
        th.active {
          color: #fff;
        }
 
        th.active .arrow {
          opacity: 1;
        }
 
        .arrow {
          display: inline-block;
          vertical-align: middle;
          width: 0;
          height: 0;
          margin-left: 5px;
          opacity: 0.66;
        }
 
        .arrow.asc {
          border-left: 4px solid transparent;
          border-right: 4px solid transparent;
          border-bottom: 4px solid #fff;
        }
 
        .arrow.dsc {
          border-left: 4px solid transparent;
          border-right: 4px solid transparent;
          border-top: 4px solid #fff;
        }
    </style>
</head>
<body>
    <!-- demo root element -->
    <div id="demo">
        <form id="search">
        Search <input name="query" v-model="filterKey">
        <b>排序:</b>
        <select name="key" id="key">
            <option value="{{key}}" v-repeat="key : gridColumns">{{key | capitalize}}</option>
        </select>
        <select name="sequence" id="sequence">
            <option value="default">默认</option>
            <option value="small">由小到大</option>
            <option value="big">由大到小</option>
        </select>
        <button id="btn">确认</button>
        </form>
        <table>
            <thead>
                <tr>
                <th v-repeat="key: gridColumns" v-on="click:sortBy(key)" v-class="active: sortKey == key">
                    {{key | capitalize}}
                    <span class="arrow" v-class="reversed[key] ? 'dsc' : 'asc'">
                    </span>
                </th>
              </tr>
            </thead>
            <tbody>
                <tr v-repeat="entry: gridData
                | filterBy filterKey
                | orderBy sortKey reversed[sortKey]">
                    <td v-repeat="key: gridColumns">
                        {{entry[key]}}
                    </td>
                </tr>
            </tbody>
        </table>
    </div>
 
    <script>
        $('#btn').click(function (e) {
            e.preventDefault();
            var key = $('#key').val();
            var sequence = $('#sequence').val();
            console.log(key+'顺序'+sequence);
            if (sequence === 'default') {
                demo.sortKey = '';
            }
            else {
                controller(key, sequence);
            }
            function controller(key, sequence) {
                demo.sortKey = key;
                if (sequence === 'big') {
                    demo.reversed[key] = true;
                }
                else {
                    demo.reversed[key] = false;
                }
            }
        });
        // bootstrap the demo
        var demo = new Vue({
          el: '#demo',
          data: {
            sortKey: '',
            filterKey: '',
            reversed: {},
            search: '',
            gridColumns: ['name', 'power'],
            gridData: [
              { name: 'Chuck Norris', power: Infinity },
              { name: 'Bruce Lee', power: 9000 },
              { name: 'Jacky Chang', power: 7000 },
              { name: 'Jet Li', power: 8000 }
            ]
          },
          compiled: function () {
            // initialize reverse state
            var self = this
            this.gridColumns.forEach(function (key) {
                self.reversed.$add(key, false)
            })
          },
          methods: {
            sortBy: function (key) {
              this.sortKey = key
              this.reversed[key] = !this.reversed[key]
            }
          }
        })
    </script>
</body>
</html>

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

reg183

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值