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>
  • 效果

初始化 不排序

年龄逆序

年龄正序

年龄原序

过滤 & 年龄正序

过滤 & 年龄逆序

相关推荐
YH丶浩21 小时前
vue自定义数字滚动插件
开发语言·前端·javascript·vue
个人看法2 天前
h5实现一个吸附在键盘上的工具栏
前端·javascript·vue
西洼工作室3 天前
设计模式与原则精要
前端·javascript·设计模式·vue
雪山上的小灰熊3 天前
UNIAPP如何自定义全局方法?
javascript·typescript·uni-app·vue·vue3·vite·hooks
fcm195 天前
(6) tauri之前端框架性能对比
前端·javascript·rust·前端框架·vue·react
王者鳜錸5 天前
方言普通话识别大模型,支持中英+202种方言识别
java·vue·语音识别
瓯雅爱分享5 天前
基于Java后端与Vue前端的MES生产管理系统,涵盖生产调度、资源管控及数据分析,提供全流程可视化支持,包含完整可运行源码,助力企业提升生产效率与管理水平
java·mysql·vue·软件工程·源代码管理
邂逅星河浪漫6 天前
【Spring AI】Ollama大模型-智能对话实现+项目实战(Spring Boot + Vue)
java·人工智能·spring boot·vue·prompt·agent·ollama
知识分享小能手6 天前
React学习教程,从入门到精通,React Router 语法知识点及使用方法详解(28)
前端·javascript·学习·react.js·前端框架·vue·react
中杯可乐多加冰7 天前
高校迎新管理系统:基于 smardaten AI + 无代码开发实践
人工智能·低代码·语言模型·llm·vue·管理系统·无代码