VUE+ELEMENTUI el-tree+el-card+el-table结合使用

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。

  • 页面组件高度可以自适应浏览器窗体高度
  • 效果图
    VUE+ELEMENTUI el-tree+el-card+el-table结合使用_第1张图片

左树形结构,右列表

<template>
  <div class="mod-user">
    <el-row :gutter="10">
    
      <el-col :span="4">
        <el-card class="box-card" shadow="hover">
          <el-input placeholder="输入关键字进行过滤" v-model="filterText" clearable>
          el-input>
          <div class="treebox" :style="{ 'height': treeClientHeight  + 'px' }">
            <el-tree class="filter-tree" :data="commodityTypeTreeData" :props="defaultProps" default-expand-all
              :filter-node-method="filterNode" ref="tree" node-key="commodityTypeCode" :highlight-current="true"
              @node-click="handleNodeClick">
            el-tree>
          div>
        el-card>
      el-col>
      
      <el-col :span="20">
        <el-card shadow="hover" :style="{ 'min-height': treeClientHeight + 80 + 'px' }">
          <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
            <el-form-item label="商品编号">
              <el-input v-model="dataForm.commodityNo" placeholder="商品编号" clearable>	   
              el-input>
            el-form-item>           
            <el-form-item>
              <el-button @click="getDataList()" type="primary" icon="el-icon-search">查询
              el-button>
              <el-button type="success" icon="el-icon-circle-plus-outline" 
              @click="addOrUpdateHandle()">新增el-button>
              <el-button type="danger" icon="el-icon-delete" @click="deleteHandle()"
                :disabled="dataListSelections.length <= 0">批量删除el-button>
            el-form-item>
          el-form>
          
          <el-table :data="dataList" border v-loading="dataListLoading" @selection-
          change="selectionChangeHandle" style="width: 100%;" height="550">
            <el-table-column type="selection" header-align="center" align="center" width="50" fixed="left">
            el-table-column>
            <el-table-column prop="commodityNo" header-align="center" align="center" label="商品编号" fixed="left"
              width="150">
            el-table-column>
            <el-table-column prop="commodityName" header-align="center" align="center" label="商品名称" fixed="left"
              width="150">
            el-table-column>
            <el-table-column prop="remark" header-align="center" align="center" label="备注" width="150">
            el-table-column>
            <el-table-column fixed="right" header-align="center" align="center" width="300" label="操作">
              <template slot-scope="scope">
                <el-button type="warning" icon="el-icon-edit" size="small" @click="addOrUpdateHandle(scope.row.id)">修改
                el-button>
                <el-button type="danger" icon="el-icon-delete" size="small"
                  @click="deleteHandle(scope.row.commodityNo)">删除
                el-button>                
              template>
            el-table-column>
          el-table>
          <el-pagination @size-change="sizeChangeHandle" @current-change="currentChangeHandle" :current-page="pageIndex"  :page-sizes="[10, 20, 50, 100]" :page-size="pageSize" :total="totalPage"
            layout="total, sizes, prev, pager, next, jumper">
          el-pagination>
        el-card>
      el-col>
    el-row>
  div>
template>
js
<script>
  import {
    treeDataTranslate
  } from '@/utils'
  export default {
    data() {
      return {
        filterText: '',
        treeClientHeight: 0,
        dataForm: {
          commodityNo: ''
          commodityTypeCode: ''
        },
        commodityTypeTreeData: [],
        defaultProps: {
          children: 'children',
          label: 'commodityTypeName'
        },
        dataList: [],
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        dataListSelections: [],
        addOrUpdateVisible: false
      }
    },
    activated() {
      this.dataForm.commodityNo = ''
      this.dataForm.commodityTypeCode = ''
      this.getTreeData()
      this.getDataList()
    },
    computed: {
      documentClientHeight: {
        get() {
          return this.$store.state.common.documentClientHeight
        },
        set(val) {
          this.$store.commit('common/updateDocumentClientHeight', val)
        }
      }
    },
    mounted() {
      this.resetDocumentClientHeight()
      window.addEventListener('resize', () => {
        this.resetDocumentClientHeight()
      })
    },
    watch: {
      filterText(val) {
        this.$refs.tree.filter(val);
      }
    },
    methods: {
    
      resetDocumentClientHeight() {
        this.documentClientHeight = document.documentElement['clientHeight']
		//减多少自己看着办
        this.treeClientHeight = this.documentClientHeight - 250
        window.onresize = () => {
          this.documentClientHeight = document.documentElement['clientHeight']
        }
      },
      
      filterNode(value, data) {
        if (!value) return true;
        return data.commodityTypeName.indexOf(value) !== -1;
      },
      
      //点击某个树节点时
      handleNodeClick(data, node, el) {
        this.dataForm.commodityTypeCode = ''
        if (node.level !== 1) {
          this.dataForm.commodityTypeCode = this.$refs.tree.getCurrentNode().commodityTypeCode
        }
        this.getDataList()
      },
      
      //获取左树数据
      getTreeData() {
        this.$http({
          url: this.$http.adornUrl(`/xx/aaa`),
          method: 'get',
          params: this.$http.adornParams()
        }).then(({
          data
        }) => {
          if (data && data.code === 0) {
            this.commodityTypeTreeData = treeDataTranslate(data.jdata, 'commodityTypeCode', 'parentId')
          } else {
            this.$message.error(data.msg)
          }
        })
      },
      
      // 获取右边表格数据列表
      getDataList() {
        this.dataListLoading = true
        this.pageIndex = 1
        this.getPageData()
      },
      
      getPageData() {
        this.$http({
          url: this.$http.adornUrl('/xx/vvv'),
          method: 'get',
          params: this.$http.adornParams({
            'page': this.pageIndex,
            'limit': this.pageSize,
            'commodityTypeCode': this.dataForm.commodityTypeCode,
            'commodityNo': this.dataForm.commodityNo
          })
        }).then(({
          data
        }) => {
          if (data && data.code === 0) {
            this.dataList = data.page.ListData
            this.totalPage = data.page.totalCount
          } else {
            this.dataList = []
            this.totalPage = 0
          }
          this.dataListLoading = false
        })
      },

      // 删除
      deleteHandle(commodityNo) {
        var commodityNos = commodityNo ? [commodityNo] : this.dataListSelections.map(item => {
          return item.commodityNo
        })
        this.$confirm(`确定对[商品编号=${commodityNos.join(',')}]进行[${commodityNo ? '删除' : '批量删除'}]操作?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http({
            url: this.$http.adornUrl('/xx/ccc'),
            method: 'post',
            data: this.$http.adornData({
              'commodityNos': commodityNos
            })
          }).then(({
            data
          }) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.getDataList()
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        }).catch(() => {})
      },

      // 每页数
      sizeChangeHandle(val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle(val) {
        this.pageIndex = val
        this.getPageData()
      },
      // 多选
      selectionChangeHandle(val) {
        this.dataListSelections = val
      }
    }
  }
script>


style
<style scoped="scoped">
  .treebox {
    overflow-y: auto;
  }
style>

你可能感兴趣的:(vue.js,elementui)