Vuex从入门到实战(十二)——【 实践篇】剩余任务条数统计 + 清除已完成

news/2024/7/10 2:06:57 标签: vue, vuex, fileter, getters

这节比较简单,Getters实现剩余剩余任务条数统计;filter()方法做筛选返回状态为未完成的列表项。
在这里插入图片描述

1、store/index.js——mutations 增加 listClean 方法清除已完成,filter() 筛选出来完成状态为false的列表项并重新赋值给 state.list ;getters 增加 unDoneLength() 方法统计未完成项目条数,同样是用 filter() 实现。

import Vue from 'vue'
import Vuex from 'vuex'
import axios from 'axios'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    list: [],
    inputValue: '', // 文本框内容
    nextId: 5 // list中id属性取值
  },
  mutations: {
    initList (state, step) {
      state.list = step
    },
    // 为 inputValue 赋值
    setInputValue (state, val) {
      state.inputValue = val
    },
    // 将inputValue存储到list中
    addInputValue (state) {
      const obj = {
        id: state.nextId,
        info: state.inputValue.trim(),
        done: false
      }
      state.list.push(obj)
      state.nextId++

      state.inputValue = ''
    },
    removeItem1 (state, step) {
      state.list.forEach((e, index) => {
        if (e.id === step) {
          return state.list.splice(index, 1)
        }
      })
    },
    removeItem2 (state, step) {
      // 根据id查找对应的索引
      const i = state.list.findIndex(x => x.id === step)
      // 根据索引,删除对应的元素
      if (i !== -1) {
        state.list.splice(i, 1)
      }
    },
    // 修改列表项的选中状态
    changeStatus (state, param) {
      const i = state.list.findIndex(x => x.id === param.id)
      if (i !== -1) {
        state.list[i].done = param.done
      }
    },
    // 清除已完成列表项
    listClean (state) {
      state.list = state.list.filter(x => x.done === false)
    }
  },
  actions: {
    getList (context) {
      axios.get('/list.json').then(({ data }) => {
        console.log(data)
        context.commit('initList', data)
      })
    }
  },
  getters: {
    // 统计未完成任务条数
    unDoneLength (state) {
      return state.list.filter(x => x.done === false).length
    }
  }
})

2、App.vue——剩余条数统计使用插值表达式 {{ unDoneLength }} ,获取像之前一样将需要的getters函数映射为当前组件的computed;超链接 “清除已完成” 绑定cleanDone() ,调用刚才 store 中写好的 listClean() 。

<template>
  <div id="app">
    <a-input
      placeholder="请输入任务"
      class="my_ipt"
      :value="inputValue"
      @change="handleInputChange"
    />
    <a-button type="primary" @click="addItemToList()">添加事项</a-button>

    <a-list bordered :dataSource="list" class="dt_list">
      <a-list-item slot="renderItem" slot-scope="item">
        <!-- 复选框 -->
        <a-checkbox :checked='item.done' @change='onChange($event, item.id)'>{{ item.info }}</a-checkbox>
        <!-- 删除链接 -->
        <a slot="actions" @click="removeItemById(item.id)">删除</a>
      </a-list-item>

      <!-- footer区域 -->
      <div slot="footer" class="footer">
        <!-- 未完成的任务个数 -->
        <span>{{ unDoneLength }}条剩余</span>
        <!-- 操作按钮 -->
        <a-button-group>
          <a-button type="primary">全部</a-button>
          <a-button>未完成</a-button>
          <a-button>已完成</a-button>
        </a-button-group>
        <!-- 把已经完成的任务清空 -->
        <a @click="cleanDone()">清除已完成</a>
      </div>
    </a-list>
  </div>
</template>

<script>
import { mapState, mapMutations, mapGetters } from 'vuex'
export default {
  name: 'app',
  data () {
    return {}
  },
  created () {
    this.$store.dispatch('getList')
  },
  computed: {
    ...mapState(['list', 'inputValue']),
    ...mapGetters(['unDoneLength'])
  },
  methods: {
    ...mapMutations(['setInputValue', 'addInputValue', 'removeItem1', 'removeItem2', 'changeStatus', 'listClean']),
    // 监听文本框内容变化
    handleInputChange (e) {
      // 拿到最新值,并同步到store
      console.log(e.target.value)
      this.setInputValue(e.target.value)
    },
    // 向列表项中新增 item 项
    addItemToList () {
      if (this.inputValue.trim().length <= 0) { // 判空处理
        return this.$message.warning('文本框内容不能为空')
      }
      this.addInputValue()
    },
    removeItemById (id) {
      this.removeItem1(id)
    },
    // 监听复选框选中状态的变化
    onChange (e, id) {
      // 通过 e.target.checked 可以接受到最新的选中状态
      const param = {
        id: id,
        done: e.target.checked
      }
      this.changeStatus(param)
      // 另一种写法:
      // @change='(e) => {onChange(e, item.id)}'
      // console.log(`checked = ${e.target.checked}`)
    },
    // 清除已完成绑定事件
    cleanDone () {
      this.listClean()
    }
  }
}
</script>

<style scoped>
#app {
  padding: 10px;
}

.my_ipt {
  width: 500px;
  margin-right: 10px;
}

.dt_list {
  width: 500px;
  margin-top: 10px;
}

.footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>


http://www.niftyadmin.cn/n/1486145.html

相关文章

element在el-table-column中使用过滤器

<el-table-columnprop"status"label"状态"><template scope"scope">{{ scope.row.status | toAdmin }}</template></el-table-column> 转载于:https://www.cnblogs.com/992516410zhen/p/8361477.html

一个学习centos的好去处

最近捣鼓centos5.5&#xff0c;发现了一个学习centos5.5的好去处&#xff1a;http://www.askbar.net/&#xff0c;要常去看看&#xff0c;才能把我自己的centos服务器管理好哦&#xff01;转载于:https://blog.51cto.com/wuhaoshu/491736

Vuex从入门到实战(十三)——【实践篇】按钮样式切换

Todos 10 这节实现三个按钮样式切换&#xff0c;使用三目运算即可实现。 <a-button type"primary / default"> 需要注意的是 type 要进行属性绑定 &#xff08;不要跟我一样败在一个冒号上&#xff09; 1、store/index.js——增加 changeViewkey() 修改视图的关…

pku 2498 StuPId

主要是看题&#xff0c;要把id number 反过来乘以9&#xff0c;3&#xff0c;7的循环 然后……poj不支持strrev()函数……不信的可以自己试试…… #include <iostream>#include <cstdio>#include <cstring>#include <string>using namespace std;int c…

python接口自动化测试--数据分离读取Excal指定单元格数据

上一篇博客讲了怎么批量读取Excal单元格数据&#xff0c;现在咱们说一下怎么读取Excal指定单元格数据。 一、首先建一个Test_Main类 #!/usr/bin/python # -*- coding: UTF-8 -*- import requests import unittest class TestDenmo(unittest.TestCase): def setUp(self):…

Vuex从入门到实战(十四)——【实践篇】完结撒花!!!

Todos 11 这节实现点击三个按钮显示出相应的列表&#xff0c;使用 getters 实现&#xff0c;因为他不会改变我们的原始数据。 完整代码地址&#xff1a;https://github.com/huijieya/vuex_demo 1、store/index.js——getters 增加 infoList &#xff0c;根据 viewkey 返回列表…

兼容性超好的CSS滑动门

代码简介&#xff1a;兼容性好的CSS没去门&#xff0c;其实这里还是要用到JS来配合&#xff0c;不过相对其它的滑动门菜单&#xff0c;本没去门将代码做到了最简化&#xff0c;而且兼容性也不错&#xff0c;因此这是本菜单的亮点&#xff0c;从外观上来说&#xff0c;它和其它的…

PowerShell 异常处理

在使用 PowerShell 的过程中&#xff0c;发现它的异常处理并不像想象中的那么直观&#xff0c;所以在这里总结一下。 Terminating Errors 通过 ThrowTerminatingError 触发的错误称为 Terminating Errors。本质上它是创建了一个异常&#xff0c;所以我们可以使用 catch 语句来捕…