Vue列表过滤与排序

目录

实现搜索过滤的功能

使用watch实现

使用computed实现

实现列表排序功能

表单数据的收集


实现搜索过滤的功能

使用watch实现
javascript 复制代码
<div id="app">
    <h1>{{msg}}</h1>
    <input type="text" placeholder="请输入搜索关键字" v-model="keyword" /><br /><br />
    <table>
      <tr>
        <th>序号</th>
        <th>任务</th>
        <th>薪资</th>
        <th>选择</th>
      </tr>
      <tr v-for="item,index in newHeros" :key="item.id">
        <td>{{item.id}}</td>
        <td>{{item.name}}</td>
        <td>{{item.power}}</td>
        <td><input type="checkbox" /></td>
      </tr>
    </table>
  </div>
javascript 复制代码
<script>
    const vm = new Vue({
      el: "#app",
      data() {
        return {
          msg: "搜索过滤",
          Heros: [
            {
              id: "1",
              name: "张三",
              power: "6000"
            },
            {
              id: "2",
              name: "张四",
              power: "7000"
            },
            {
              id: "3",
              name: "李四",
              power: "8000"
            },
            {
              id: "4",
              name: "李五",
              power: "9000"
            },
          ],
          keyword: "",
          newHeros: []
        }
      },
      watch: {
        keyword: {
          handler() {
            this.newHeros = this.Heros.filter((item => {
              return item.name.includes(this.keyword)
            }));
          },
          immediate: true,
        }
      }
    })
  </script>
使用computed实现
javascript 复制代码
<!doctype html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <title>列表过滤计算属性实现</title>
    <script src="./js/vue.js"></script>
    <style>
      th,
      td {
        border: 1px solid black;
      }
    </style>
  </head>

  <body>
    <div id="app">
      <h1>{{msg}}</h1>
      <input
        type="text"
        placeholder="请输入搜索关键字"
        v-model="keyword"
      /><br /><br />
      <table>
        <tr>
          <th>序号</th>
          <th>姓名</th>
          <th>薪资</th>
          <th>选择</th>
        </tr>
        <tr v-for="item in newHeros" :key="item.id">
          <td>{{item.id}}</td>
          <td>{{item.name}}</td>
          <td>{{item.power}}</td>
          <td><input type="checkbox" /></td>
        </tr>
      </table>
    </div>
    <script>
      const vm = new Vue({
        el: "#app",
        data() {
          return {
            msg: "列表过滤-计算属性",
            keyword: "",
            heros: [
              { id: "101", name: "张三", power: 10000 },
              { id: "102", name: "三德子", power: 9000 },
              { id: "103", name: "王德", power: 8000 },
              { id: "104", name: "张六", power: 6000 },
              { id: "105", name: "老六", power: 9100 },
            ],
          };
        },
        computed: {
          newHeros() {
            return this.heros.filter((item) => {
              return item.name.includes(this.keyword);
            });
          },
        },
      });
    </script>
  </body>
</html>

实现列表排序功能

javascript 复制代码
<!doctype html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <title>列表排序</title>
    <script src="./js/vue.js"></script>
    <style>
      th,
      td {
        border: 1px solid black;
      }
    </style>
  </head>

  <body>
    <div id="app">
      <h1>{{msg}}</h1>
      <input type="text" placeholder="请输入搜索关键字" v-model="keyword" />
      <br />
      <button @click="type = 1">升序</button>
      <button @click="type = 2">降序</button>
      <button @click="type = 0">原序</button>
      <table>
        <tr>
          <th>序号</th>
          <th>姓名</th>
          <th>薪资</th>
          <th>选择</th>
        </tr>
        <tr v-for="item in newHeros" :key="item.id">
          <td>{{item.id}}</td>
          <td>{{item.name}}</td>
          <td>{{item.power}}</td>
          <td><input type="checkbox" /></td>
        </tr>
      </table>
    </div>
    <script>
      const vm = new Vue({
        el: "#app",
        data() {
          return {
            // 定义标识项,1是升序,2是降序,0是原序
            type: 0,
            keyword: "",
            msg: "列表排序",
            heros: [
              { id: "101", name: "章三", power: 10000 },
              { id: "102", name: "三响", power: 9000 },
              { id: "103", name: "李四", power: 8000 },
              { id: "104", name: "李章", power: 11000 },
            ],
            newHeros: [],
          };
        },
        // computed: {
        //   newHeros() {
        //     if (this.type == 0) {
        //       return this.heros;
        //     } else if (this.type == 1) {
        //       return this.heros.toSorted((a, b) => {
        //         return a.power - b.power;
        //       });
        //     } else if (this.type == 2) {
        //       return this.heros.toSorted((a, b) => {
        //         return b.power - a.power;
        //       });
        //     }
        //   },
        // },
        watch: {
          type: {
            handler(n) {
              // 先过滤,再排序
              let arr = this.heros.filter((item) => {
                return item.name.includes(this.keyword);
              });

              if (n == 0) {
                this.newHeros = arr;
              } else if (n == 1) {
                // sort会改变原数组
                this.newHeros = arr.sort((a, b) => {
                  return a.power - b.power;
                });
              } else if (n == 2) {
                this.newHeros = arr.sort((a, b) => {
                  return b.power - a.power;
                });
              }
            },
            immediate: true,
          },
        },
      });

      // 回顾sort方法
      let arr = [8, 9, 5, 4, 1, 2, 3];

      // sort方法排序之后,不会生成一个新的数组,是在原数组的基础之上进行排序,会影响原数组的结构。
      // a在前,升序,b在前降序
    </script>
  </body>
</html>

表单数据的收集

javascript 复制代码
 <!-- 对于单选框:手动配置value值,用来标识到底选的是谁,v-model会把value放到对应变量中 -->
      性别:男:<input
        type="radio"
        name="sex"
        value="boy"
        v-model="userInfo.sex"
      />
javascript 复制代码
    <!-- 复选框情况一:选择不止一个,选择复选框的value值放到数组中 -->
    爱好:运动
    <input type="checkbox" value="sprot" v-model="userInfo.hobbdy" /> 旅游
    <input type="checkbox" value="travel" v-model="userInfo.hobbdy" /> 唱歌
    <input type="checkbox" value="song" v-model="userInfo.hobbdy" /><br /><br />

....
 hobbdy: [],
....
javascript 复制代码
 <!-- 复选框情况二:就一种情况,要么选中,要么是不选中
      不需要配置value,默认收集是true或false 
      -->
    <input type="checkbox" v-model="userInfo.arrge" />阅读并接受协议<br /><br />


....
arrge: false,
....
javascript 复制代码
<!-- 下拉框:value值配置在option中,数据收集放在select标签中 -->
    学历:<select name="" id="" v-model="userInfo.grade">
      <option value="dz">大专</option>
      <option value="bk">本科</option>
      <option value="ss">硕士</option>
      <option value="bs">博士</option>
    </select><br /><br />
相关推荐
gCode Teacher 格码致知2 小时前
Javascript提高:Node.js readline 模块 完整使用教程
javascript·node.js
牛十二2 小时前
智能体框架开发实战
运维·服务器·前端
鹅天帝2 小时前
20230319网安学习日志——XSS漏洞
前端·学习·web安全·网络安全·xss
floret. 小花2 小时前
Vue3 + Electron 知识点总结 · 2026-03-21
前端·面试·electron·学习笔记·vue3
蓝黑20202 小时前
Vue的v-if和v-for放在同一个HTML元素里的坑
前端·javascript·vue.js
进击的雷神2 小时前
展位号后缀清理、详情页JS数据提取、重试机制控制、地址字段重构——美国NPE展爬虫四大技术难关攻克纪实
javascript·爬虫·python·重构
转角羊儿2 小时前
精灵图案例
开发语言·前端·javascript
大雷神2 小时前
HarmonyOS APP<玩转React>开源教程十八:课程详情页面
前端·react.js·开源·harmonyos
spencer_tseng2 小时前
secure-keyboard.js secure-keyboard.css
javascript·css