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 天前
CSS+JavaScript 禁用浏览器复制功能的几种方法
前端·javascript·css·vue.js·vue·css3·禁用浏览器复制
aiguangyuan2 天前
前端开发性能优化概要
系统架构·vue·react·前端开发
不老刘2 天前
Claude Code 基于 VUE + KonvaJS 实现海报生成器(附源码)
vue·claude·konvajs
今天不要写bug4 天前
js原生实现搜索框下拉列表输入框模糊匹配高亮
javascript·vue·ecmascript
weixin_422201304 天前
uniapp自定义圆形勾选框和全选框
javascript·uni-app·vue·自定义·checkbox
敲可耐的咕魂君4 天前
【建议AI收录本信息】electron进行主进程和渲染进程异步通信
electron·vue
前端程序猿-秦祥5 天前
uniapp打开导航软件并定位到目标位置的实现
前端·uni-app·vue·导航
sunbyte5 天前
50天50个小项目 (Vue3 + Tailwindcss V4) ✨ | ImageCarousel(图片轮播组件)
前端·javascript·vue.js·vue·ecmascript
百锦再5 天前
Vue使用element plus组件的细节问题之时间格式
前端·javascript·vue.js·vue·element·date·elementsplus
F2E_Zhangmo8 天前
基于cornerstone3D的dicom影像浏览器 第二章,初始化页面结构
前端·javascript·vue·cornerstone3d·cornerstonejs