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 />
相关推荐
IT_陈寒1 小时前
Vite的热更新突然不香了,排查三小时差点砸键盘
前端·人工智能·后端
子兮曰2 小时前
Agency-Agents 深度解析:400+ AI 专家的"梦之队"如何重塑开发工作流
前端·后端·vibecoding
山河木马2 小时前
渲染管线-计算得到gl_Position(顶点着色器)之后续GPU流程
javascript·webgl·图形学
竹林8182 小时前
用 The Graph 查询链上数据实战:从手搓 RPC 到 Subgraph,我的 NFT 项目数据加载快了 10 倍
前端·javascript
妙码生花3 小时前
从 PHP 到 AI + Golang,程序员自救转型手记(十九):点选验证码代码逐行目检
前端·后端·go
Awu12273 小时前
⚡从零开发 Agent CLI(五)实现一个可治理、可扩展的工具系统
前端·人工智能·claude
咪库咪库咪4 小时前
Vue3-生命周期
前端
莪_幻尘4 小时前
你的 AI Skill 越多越蠢?Token 上下文爆炸的求生指南
前端·ai编程
lichenyang4535 小时前
从 has.echo 到异步 API 注册表:一次 ASCF API 回调不触发的排查复盘
前端
林瞅瞅5 小时前
Nuxt3 项目部署 Nginx 防盗链后特定 JS 文件 403 问题修复方案
前端