VUE中实现table的模糊查询相关学习(结合vuex module+axios实例)

使用VUE+element 实现纯前端的模糊查询。

关键代码

<el-table
            :data="tables"   //绑定计算之后(模糊查询之后)的数据属性
            style="width: 100%">
      <el-table-column
              prop="date"
              label="日期"
              width="180">
        <template slot-scope="scope">
          <i class="el-icon-time"></i>
          <span style="margin-left: 10px">{
   
   { scope.row.date }}</span>
        </template>
      </el-table-column>

      <el-table-column
              prop="name"
              label="姓名"
              width="180">
        <template slot-scope="scope">
          <span style="">{
   
   { scope.row.name }}</span>
        </template>
      </el-table-column>
      <el-table-column
              prop="address"
              label="地址">
        <template slot-scope="scope">
          <span style="">{
   
   { scope.row.address }}</span>
        </template>
      </el-table-column>
              <el-table-column>
                <template slot-scope="scope">
                  <el-button type="danger" @click="tableDelete(scope.$index, scope.row)">删除</el-button>
                </template>
              </el-table-column>
              <el-table-column>
                <template slot-scope="scope">
                <el-button type="warning" @click="handleEdit(scope.$index, scope.row)">修改</el-button>
                </template>
              </el-table-column>
    </el-table>
  name: 'app',
  data() {
    
    
    return {
    
    
      tableData: [{
    
    
        id: '1',
        date: '2016-05-02',
        name: '王小1',
        address: '上海市普陀区金沙江路 1518 弄'
      }, {
    
    
        id: '2',
        date: '2016-05-04',
        name: '王小2',
        address: '上海市普陀区金沙江路 1517 弄'
      }, {
    
    
        id: '3',
        date: '2016-05-01',
        name: '王小3',
        address: '上海市普陀区金沙江路 1519 弄'
      }, {
    
    
        id: '4',
        date: '2016-05-03',
        name: '王小4',
        address: '上海市普陀区金沙江路 1516 弄'
      }],
      NewRow: {
    
    id: '', date: '', name: '', address: ''},
      search:''
    }
   },
computed:{
    
    
     tables(){
    
    
        if(this.search) {
    
    
          return this.tableData.filter(item =>{
    
    
            return Object.keys(item).some(key => {
    
        //1,2
              return String(item[key]).toLowerCase().indexOf(this.search) > -1    //3
            })
          }
          )
        }else return this.tableData    //4
     }
   },

注:1.Object.keys(arr)方法会返回一个由一个给定对象的自身可枚举属性组成的数组,数组中属性名的排列顺序和使用 for…in 循环遍历该对象时返回的顺序一致 。如果对象的键-值都不可枚举,那么将返回由键组成的数组。

2.some()方法用于检测数组中的元素是否满足指定条件,依次执行数组的每个元素,如果有一个元素满足条件,则表达式返回true , 剩余的元素不会再执行检测;如果没有满足条件的元素,则返回false。这里操作的是tableData数组中某一项中的各个属性,实现全字段模糊匹配

3.String(item[key]) 把item[key]转为字符串

4.如果没有输入关键词,就返回所有数据

介绍完大致的用法之后,结合vuex来实现一个后台数据查询

这里因为项目需要,我用到了vuex中的modules,不是必须的,后台请求数据使用了axios,详细见我另一篇文章vue-cli3+axios+element实现调用后端接口

productList.js(vuex modules文件)
import ajax from '@/assets/js/ajax';

export default {
    
    
    namespaced: true,
    state: {
    
    
    //定义了两个数组,tableData用于展示数据,由于查询的时候需要直接修改,为了保存原始数据,定义一个origin数组。会在下面的查询相关代码中用到。
        tableData: [],
        origin: []
    },
    mutations: {
    
    
    //将后台的数据获取到tableData
        getTableData(state, res) {
    
    
            state.tableData.splice(0, state.tableData.length);
            state.tableData = res.data.data;
            state.origin = res.data.data;
        },
        //查询相关代码
        searchData(state, search) {
    
    
            if (search) {
    
    
            state.tableData = state.origin.filter(item =>
                Object.keys(item).some(key =>
                    String(item[key]).indexOf(search)> -1)
            )
            }
        }

    },
    actions: {
    
    
    //发起请求
        getData(context) {
    
    
            ajax({
    
    
                url: '/infocent/projectsearch/projectlist/base',
            }).then(res => {
    
    
                context.commit('getTableData', res);
            });
        }
    }
};
store.js文件
import Vue from 'vue';
import Vuex from 'vuex';
import productlist from '@/store/productList';

Vue.use(Vuex);

export default new Vuex.Store({
    
    
    modules: {
    
    
        Productlist: productlist,
    },
    state: {
    
    },
    mutations: {
    
    },
    actions: {
    
    }
});
<template>
    <div class="terminal">
        <el-row :gutter="24">
            <el-col :span="8" :offset="8">
                <el-input
                        placeholder="请输入关键字"
                        icon="search"
                        class="search"
                        v-model="search"
                        @input="handleKeywordsInput">
                </el-input>
            </el-col>
        </el-row>
        <el-row>
            <el-col :span="20" :offset="2">
                <el-table>
                     ...
                </el-table>   //表格具体内容就省略了
            </el-col>
        </el-row>
    </div>
</template>

<script>
    export default {
     
     
        name: 'Terminal',
        data() {
     
     
            return {
     
     
                search: ''
            };
        },
        mounted() {
     
     
        //调用vuex中actions里的方法,可以是异步方法,一般是发起请求
            this.$store.dispatch('Productlist/getData');
        },
        computed: {
     
     
        //更新表格数据
            tableData() {
     
     
                return this.$store.state.Productlist.tableData;
            }
        },
        methods: {
     
     
        //使用input调用查询方法
            handleKeywordsInput() {
     
     
                this.$store.commit('Productlist/searchData', this.search);
            }
        },
    };
</script>

猜你喜欢

转载自blog.csdn.net/e1172090224/article/details/95314089
今日推荐