使用vuex做列表数据过滤

功能需求
  1. 展示一个表格,表格包含选项有“ 姓名 年龄 是否显示”三个列表项
  2. 是否显示列表项是可操作开关,点击切换打开/关闭
  3. 将表格中开关为打开状态的列表项,在另一个表格中显示
    效果预览
需求分析

根据功能需求,

  1. 我们需要定义两个页面组件: 用作渲染所有数据的组件,新建allList.vue文件; 用作渲染选中数据的组件,新建filterList.vue文件
  2. 我们可以使用vuex状态管理,引入vuex跟踪数据状态变化
  3. 定义数据结构,做列表渲染用
目录结构

下面该demo的目录结构
目录结构

代码编写
状态管理文件

首先定义todoList变量,存放列表数据。当localStorage中没有todolist时,就将列表数组给到todoList变量,否则就从localStorage中取值。这样做的目的是,当我们操作了列表,防止刷新时列表状态又回复到最原始的状态。 使用本地存储保证刷新后状态也是最后一次操作的状态。

import Vue from 'vue';
import Vuex from 'vuex';

Vue.use(Vuex);

var todoList = JSON.parse(localStorage.getItem('todolist')) || [
    {id:1,name:'赵一铭',age:26,level:9,isShow:true},
    {id:2,name:'钱二婷',age:19,level:1,isShow:true},
    {id:3,name:'孙三堤',age:22,level:7,isShow:false},
    {id:4,name:'李四喜',age:23,level:3,isShow:true},
    {id:5,name:'周六冉',age:24,level:4,isShow:false}
];

const store = new Vuex.Store({
    state:{
        todoList:todoList
    },
    mutations:{
        changeTodoList(state,row){
            state.todoList.find((item,index) => {
                if(item.id == row.id){
                    item.isShow = row.isShow
                }
            })
            localStorage.setItem('todolist',JSON.stringify(state.todoList))
        }
    },
    getters:{
        todoList:state => {
            return state.todoList.filter(todo => todo.isShow)
        }
    }
})

export default store;
App.vue父容器组件

App.vue文件我们只当做父容器组件,在如组件中引入两个子组件AllDataFilterData

<template>
    <div id="app">
        <h1>List Demo</h1>
        <div class="content">
            <div class="item">
                <all-data></all-data>
            </div>
            <div class="item">
                <filter-data></filter-data>
            </div>
        </div>
    </div>
</template>
<script>
import AllData from './components/allList';
import FilterData from './components/filterList'
export default {
    name:'App',
    components:{
        AllData,
        FilterData
    }
}
</script>
子组件AllData

computed计算属性中返回store中的列表数据todoList,切换开关出发switchChange方法提交store中mutation更改todoList数据

<template>
    <div>
        <template>
            <el-table :data="todoList" border style="width=100%">
                <el-table-column prop="name" label="姓名"></el-table-column>
                <el-table-column prop="age" label="年龄"></el-table-column>
                <el-table-column label="是否显示">
                    <template slot-scope="scope">
                        <el-switch v-model="scope.row.isShow" active-color="#13ce66" inactive-color="#ccc" @change="switchChange(scope.row)"></el-switch>
                    </template>
                </el-table-column>
            </el-table>
        </template>
    </div>
</template>

<script>
export default {
    computed:{
        todoList(){
            return this.$store.state.todoList
        }
    },
    methods:{
        switchChange(row){
            this.$store.commit('changeTodoList',row)
        }
    }
}
</script>
FilterData子组件

这个组件中只做过滤数据渲染,在computed计算属性中返回store的getters中的数据。getters在vuex中相当于vue的computed计算属性,返回过滤后的数据。

<template>
    <div>
        <template>
            <el-table :data="todos" border style="width=100%">
                <el-table-column prop="name" label="姓名"></el-table-column>
                <el-table-column prop="age" label="年龄"></el-table-column>
            </el-table>
        </template>
    </div>
</template>

<script>
export default {
    computed:{
        todos(){
            return this.$store.getters.todoList
        }
    },
    methods:{
    }
}
</script>

以上~

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值