Vue-列表过滤排序

列表过滤

基础环境

  • 数据
javascript 复制代码
persons: [
   { id: "001", name: "刘德华", age: 19 },
   { id: "002", name: "马德华", age: 20 },
   { id: "003", name: "李小龙", age: 21 },
   { id: "004", name: "释小龙", age: 18 },
 ]
  • 根据 名称模糊过滤

监听属性

  • 代码
javascript 复制代码
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <title>列表过滤</title>
    <!--  引入Vue  -->
    <script type="text/javascript" src="../js/vue.js"></script>
    <style>
      .base {
        padding: 5px;
        height: 100px;
      }
    </style>
  </head>
  <body>
    <div id="root">
      <h1>列表过滤</h1>
      <div>
        <h2>数组</h2>
        <input type="text" placeholder="请输入名称" v-model="filterName" />
        <ul>
          <!-- :key="item.id" :key="index" -->
          <li v-for="(item,index) in filterPersons" :key="item.id">
            {{index}}-{{item.id}}-{{item.name}}
          </li>
        </ul>
      </div>
    </div>
  </body>
  <script type="text/javascript">
    Vue.config.productionTip = false; // 阻止vue在启动是生成生产提示
    const vm = new Vue({
      el: "#root",
      data: {
        name: "Vue 扛把子",
        persons: [
          { id: "001", name: "刘德华", age: 19 },
          { id: "002", name: "马德华", age: 20 },
          { id: "003", name: "李小龙", age: 21 },
          { id: "004", name: "释小龙", age: 18 },
        ],
        filterName: "",
        filterPersons: [],
      },

      methods: {},
      watch: {
        filterName: {
          immediate: true,
          handler(newValue) {
            onsole.log("filterName new value => " + newValue);
            if (newValue === "") {
              this.filterPersons = this.persons;
            } else {
              this.filterPersons = this.persons.filter((item) => {
                return item.name.indexOf(newValue) > -1;
              });
            }
          },
        },
      },
    });
  </script>
</html>
  • 效果

初始化

搜索

清空

计算属性

  • 代码
javascript 复制代码
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <title>列表过滤</title>
    <!--  引入Vue  -->
    <script type="text/javascript" src="../js/vue.js"></script>
    <style>
      .base {
        padding: 5px;
        height: 100px;
      }
    </style>
  </head>
  <body>
    <div id="root">
      <h1>列表过滤</h1>
      <div>
        <h2>数组</h2>
        <input type="text" placeholder="请输入名称" v-model="filterName" />
        <ul>
          <!-- :key="item.id" :key="index" -->
          <li v-for="(item,index) in filterPersons" :key="item.id">
            {{index}}-{{item.id}}-{{item.name}}
          </li>
        </ul>
      </div>
    </div>
  </body>
  <script type="text/javascript">
    Vue.config.productionTip = false; // 阻止vue在启动是生成生产提示
    const vm = new Vue({
      el: "#root",
      data: {
        name: "Vue 扛把子",
        persons: [
          { id: "001", name: "刘德华", age: 18 },
          { id: "002", name: "马德华", age: 20 },
          { id: "003", name: "李小龙", age: 20 },
          { id: "004", name: "释小龙", age: 18 },
        ],
        filterName: ""
      },
      computed: {
        filterPersons(){
            console.log(" computed filterName value => " + this.filterName); 
            return this.persons.filter((item) => {
              return item.name.indexOf( this.filterName) > -1;
            });
        }
      },

      methods: {},
      watch: {
      },
    });
  </script>
</html>
  • 效果

初始化

搜索

清空

列表排序

基础环境

  • 数据
javascript 复制代码
persons: [
   { id: "001", name: "刘德华", age: 19 },
   { id: "002", name: "马德华", age: 20 },
   { id: "003", name: "李小龙", age: 21 },
   { id: "004", name: "释小龙", age: 18 },
 ]
  • 根据 年龄排序

计算属性

  • 代码
javascript 复制代码
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <title>列表过滤</title>
    <!--  引入Vue  -->
    <script type="text/javascript" src="../js/vue.js"></script>
    <style>
      .base {
        padding: 5px;
        height: 100px;
      }
    </style>
  </head>
  <body>
    <div id="root">
      <h1>列表过滤</h1>
      <div>
        <h2>数组</h2>
        <input type="text" placeholder="请输入名称" v-model="filterName" />
        <button @click="sortType='desc'">年龄逆序</button>
        <button @click="sortType='asc'">年龄正序</button>
        <button @click="sortType=''">年龄原序</button>
        <ul>
          <!-- :key="item.id" :key="index" -->
          <li v-for="(item,index) in filterPersons" :key="item.id">
            {{index}}-{{item.id}}-{{item.name}}-{{item.age}}
          </li>
        </ul>
      </div>
    </div>
  </body>
  <script type="text/javascript">
    Vue.config.productionTip = false; // 阻止vue在启动是生成生产提示
    const vm = new Vue({
      el: "#root",
      data: {
        name: "Vue 扛把子",
        persons: [
          { id: "001", name: "刘德华", age: 19 },
          { id: "002", name: "马德华", age: 20 },
          { id: "003", name: "李小龙", age: 21 },
          { id: "004", name: "释小龙", age: 18 },
        ],
        filterName: "",
        sortType: ""
      },
      computed: {
        filterPersons() {
          console.log("computed filterName value => " + this.filterName);
          const arr = this.persons.filter((item) => {
            return item.name.indexOf(this.filterName) > -1;
          });
          if (this.sortType !== "") {
            console.log("computed sortType value => " + this.sortType);
            arr.sort((v1, v2) => {
              return this.sortType === "asc" ? v1.age - v2.age : v2.age - v1.age;
            });
          }
          return arr;
        },
      },

      methods: {},
      watch: {},
    });
  </script>
</html>
  • 效果

初始化 不排序

年龄逆序

年龄正序

年龄原序

过滤 & 年龄正序

过滤 & 年龄逆序

相关推荐
百锦再1 天前
Vue中对象赋值问题:对象引用被保留,仅部分属性被覆盖
前端·javascript·vue.js·vue·web·reactive·ref
一笑code1 天前
vue/微信小程序/h5 实现react的boundary
微信小程序·vue·react
eric*16881 天前
尚硅谷张天禹老师课程配套笔记
前端·vue.js·笔记·vue·尚硅谷·张天禹·尚硅谷张天禹
喜欢敲代码的程序员2 天前
SpringBoot+Mybatis+MySQL+Vue+ElementUI前后端分离版:项目搭建(一)
spring boot·mysql·elementui·vue·mybatis
海的诗篇_2 天前
前端开发面试题总结-原生小程序部分
前端·javascript·面试·小程序·vue·html
sunbyte2 天前
50天50个小项目 (Vue3 + Tailwindcss V4) ✨ | DragNDrop(拖拽占用组件)
前端·javascript·css·vue.js·vue
skyymrj12 天前
Vue3 + Tailwind CSS 后台管理系统教程
前端·css·vue
程序猿小D12 天前
[附源码+数据库+毕业论文]基于Spring+MyBatis+MySQL+Maven+Vue实现的校园二手交易平台管理系统,推荐!
java·数据库·mysql·spring·vue·毕业设计·校园二手交易平台
伍哥的传说12 天前
react gsap动画库使用详解之text文本动画
前端·vue.js·react.js·前端框架·vue·html5·动画
伍哥的传说12 天前
react gsap动画库使用详解之scroll滑动动画
前端·javascript·vue.js·react.js·前端框架·vue·动画