Vue 搜索和排序

<!DOCTYPE html>
<html lang="en">
<head>
    <script src="js/vue.js"></script>
    <style>

    </style>
</head>
<body>
<div id="app">
    filter by namee : <input type="text" v-model="shuru"/> <br/>
    <ul>
        <li v-for="(p,index) in filterpersons" :key="index">
            {{p.namee}}----{{p.age}}
        </li>
    </ul>
    <button @click="paixu(1)">up</button>
    <button @click="paixu(2)">down</button>
    <button @click="paixu(0)">default</button>
</div>
</body>
<script type="text/javascript">

    var vue = new Vue({
        el: "#app",
        data: {
            persons: [{namee: "xuhaitao", age: 33}, {namee: "hunk", age: 45}, {namee: "xuhaihuan", age: 30}],
            xuhao: ["A", "B", "C", "D"],
            shuru: '',
            zhi: 0
        },
        computed: {
            filterpersons() {
                var $this = this;  // 相当于这种写法 const {persons,shuru}=this;
                var filterP = this.persons.filter(function (item) {
                    return item.namee.indexOf($this.shuru) != -1;
                });
                if (this.zhi == 0) {
                    return filterP;
                } else if (this.zhi == 1) {
                    return filterP.sort(function (p1, p2) {
                        return p1.age - p2.age;
                    })
                } else if (this.zhi == 2) {
                    return filterP.sort(function (p1, p2) {
                        return p2.age - p1.age;
                    })
                }
            }
        },
        methods: {
            paixu(value) {
                this.zhi = value;
            }
        }

    });
</script>

</html>


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue Table 可以使用 sort-by 属性来进行排序。sort-by 属性可以指定一个字段或一个方法来进行排序,同时可以使用 sort-desc 属性来指定排序的方向。 以下是一个 Vue Table 排序的例子: ```html <template> <div> <table> <thead> <tr> <th @click="sortBy('name')">Name</th> <th @click="sortBy('age')">Age</th> <th @click="sortBy('email')">Email</th> </tr> </thead> <tbody> <tr v-for="user in sortedUsers" :key="user.id"> <td>{{ user.name }}</td> <td>{{ user.age }}</td> <td>{{ user.email }}</td> </tr> </tbody> </table> </div> </template> <script> export default { data() { return { users: [ { id: 1, name: 'John', age: 25, email: 'john@example.com' }, { id: 2, name: 'Jane', age: 30, email: 'jane@example.com' }, { id: 3, name: 'Bob', age: 20, email: 'bob@example.com' }, { id: 4, name: 'Alice', age: 35, email: 'alice@example.com' }, ], sortByField: 'name', sortDesc: false, }; }, computed: { sortedUsers() { const field = this.sortByField; const desc = this.sortDesc ? -1 : 1; return this.users.sort((a, b) => { if (a[field] < b[field]) return -1 * desc; if (a[field] > b[field]) return 1 * desc; return 0; }); }, }, methods: { sortBy(field) { if (field === this.sortByField) { this.sortDesc = !this.sortDesc; } else { this.sortByField = field; this.sortDesc = false; } }, }, }; </script> ``` 在上面的例子中,我们定义了一个 users 数组来存储用户信息。Vue Table 使用 v-for 指令来循环渲染用户信息,并使用 @click 事件来触发 sortBy 方法进行排序。sortBy 方法根据点击的字段来进行排序,并更新 sortByField 和 sortDesc 属性。sortedUsers 计算属性根据 sortByField 和 sortDesc 属性来返回排序后的用户信息。 注意,在实际开发中,我们可能需要使用分页、搜索等功能,这时需要引入第三方库或自己编写相关功能。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值