1.0.27 • Published 5 years ago

el-table-editabled v1.0.27

Weekly downloads
20
License
ISC
Repository
github
Last release
5 years ago

el-table-editabled

一个用来实现el-table复杂编辑并包含验证场景的组件

点击查看在线demo

Install

npm install el-table-editabled -S

webpack.base.conf.js

因代码中使用了es6的语法 所以需要添加babel配置

'use strict'
const path = require('path')
const utils = require('./utils')
const config = require('../config')
const vueLoaderConfig = require('./vue-loader.conf')
// 此处为添加的配置
const fs = require('fs')

function resolve (dir) {
  return path.join(__dirname, '..', dir)
}

//此处为添加的配置
let dirsName = fs.readdirSync(resolve('node_modules')).filter(dirName => /el-table-editabled/.test(dirName))
const includesDirs = dirsName.map(dir => resolve(`node_modules/${dir}/src`))

module.exports = {
  ... //省略的代码
  module: {
    rules: [
      // 省略代码...
      // 此处有添加babel-loader配置 ‘...includesDirs‘
      {
        test: /.js$/,
        loader: 'babel-loader',
        include: [resolve('src'), resolve('test'), resolve('node_modules/webpack-dev-server/client'), ...includesDirs]
      },
      {
        test: /.(png|jpe?g|gif|svg)(?.*)?$/,
        loader: 'url-loader',
        options: {
          limit: 10000,
          name: utils.assetsPath('img/[name].[hash:7].[ext]')
        }
      },

Quick Start

import Vue from 'vue'
import ElTableEditabled from 'el-table-editabled'

Vue.use(ElTableEditabled)

基础用法

<template>
  <div>
    <div style="margin-bottom: 12px">
      <el-button @click="handleValidate">表格验证</el-button>
      <el-button @click="handleNewRows">新增行</el-button>
      <el-button @click="handleDelRows">删除行</el-button>
      <el-button @click="handleCheckData">打印编辑后的表格数据</el-button>
    </div>
    <el-table-editabled
      v-model="tableData"
      :columns="['date', 'name']"
      :validators="validators"
      ref="elTableEditabled"
    >
      <el-table
        :data="tableData"
        style="width: 100%"
        @selection-change="selection = arguments[0]"
      >
        <el-table-column
          type="selection"
        ></el-table-column>
        <el-table-column
          fixed
          prop="date"
          label="日期"
        >
          <template slot-scope="{ row }">
            <el-table-editabled-cell
              :row="row"
              prop="date"
            >
              <template slot-scope="{ rowStates, validateOwn }">
                <span v-show="!rowStates.editing">{{row.date}}</span>
                <el-date-picker
                  v-show="rowStates.editing"
                  v-model="row.date"
                  type="date"
                  value-format="yyyy-MM-dd"
                  placeholder="选择日期"
                  clearable
                  @change="validateOwn"
                >
                </el-date-picker>
              </template>
            </el-table-editabled-cell>
          </template>
        </el-table-column>
        <el-table-column
          prop="name"
          label="姓名"
        >
          <template slot-scope="{ row }">
            <el-table-editabled-cell
              :row="row"
              prop="name"
            >
              <template slot-scope="{ rowStates, validateOwn }">
                <span v-show="!rowStates.editing">{{row.name}}</span>
                <el-input
                  v-show="rowStates.editing"
                  v-model="row.name"
                  clearable
                  @blur="validateOwn"
                >
                </el-input>
              </template>
            </el-table-editabled-cell>
          </template>
        </el-table-column>
        <el-table-column
          prop="zip"
          label="邮编"
        >
        </el-table-column>
        <el-table-column
          fixed="right"
          label="操作"
          width="120">
          <template slot-scope="{ row }">
            <el-table-editabled-cell
              :row="row"
            >
              <template slot-scope="{ rowStates }">
                <el-button
                  v-show="!rowStates.editing"
                  @click="handleEdit(row)"
                  type="text"
                  size="small">
                  编辑
                </el-button>
                <div v-show="rowStates.editing">
                  <el-button
                    @click="handleCanelRow(row)"
                    type="text"
                    size="small">
                    取消
                  </el-button>
                  <el-button
                    @click="handleSave(row)"
                    type="text"
                    size="small">
                    保存
                  </el-button>
                </div>
              </template>
            </el-table-editabled-cell>
          </template>
        </el-table-column>
      </el-table>
    </el-table-editabled>
  </div>
</template>

<script>
export default {
  data () {
    return {
      tableData: [{
        date: '2016-05-03',
        name: '王小虎',
        zip: 200333
      }, {
        date: '2016-05-02',
        name: '王小虎',
        zip: 200333
      }, {
        date: '2016-05-04',
        name: '王小虎',
        zip: 200333
      }],
      validators: {
        date ({ date }, next) {
          if (!date) {
            next('请选择日期')
          } else {
            next('')
          }
        },
        name ({ name }, next) {
          if (!name) {
            next('请选择日期')
          } else {
            next('')
          }
        }
      }
    }
  },
  methods: {
    handleCheckData () {
      console.log(this.tableData)
    },
    getNewRowData () {
      return {
        date: '',
        name: '',
        zip: ''
      }
    },
    handleEdit (row) {
      this.$refs.elTableEditabled.editRows([row])
    },
    handleCanelRow (row) {
      this.$refs.elTableEditabled.cancelRows([row])
    },
    handleSave (row) {
      // 验证一些正在编辑的行
      this.$refs.elTableEditabled.validateRows([row], valid => {
        // valid 为布尔值 代表表格正在编辑的所有字段是否验证通过
        if (valid) {
          // 第二个参数为 是否回退数据 若传false 则只取消编辑状态 编辑的数据会保存
          this.$refs.elTableEditabled.cancelRows([row], false)
        }
      })
    },
    handleNewRows () {
      const newRow = this.getNewRowData()

      this.$refs.elTableEditabled.newRows([newRow])
    },
    handleDelRows () {
      // 删除选中的数据
      this.$refs.elTableEditabled.delRows(this.selection)
    },
    handleValidate () {
      // 验证表格中所有正在编辑的字段
      this.$refs.elTableEditabled.validate(valid => {
        // valid 为布尔值 代表表格正在编辑的所有字段是否验证通过
        console.log(valid)
      })
    }
  }
}
</script>

API

El-Table-Editabled Props:

属性说明 类型 默认值
v-model绑定el-table的表格数据Array--
columns需要编辑的列的字段名Array--
rowStates可以给行添加一些状态Function(row)--
cellStates可以给每一个单元格添加一些状态Object--
validators给需要编辑的列添加验证规则--
default-editing表格是否默认为正在编辑状态Booleanfalse
validate-msg-tooltip表格验证的提示信息是否使用tooltip展示Booleanfalse

El-Table-Editabled Methods:

方法说明参数
editRows使某些行进入编辑状态Function(rows: Array<row>)
editColumns是某些行的某些字段进入编辑状态Function(rows: Array<row>, columns: Array<column>)
delRows删除某些行Function(rows: Array<row>)
newRows新增行Function(newDatas: Array<newRowData>)
insertRowsBeforeRow在某一行之前插入新数据Function(row: Object, newDatas: Array<newRowData>)
insertRowsAfterRow在某一行之后插入新数据Function(row: Object, newDatas: Array<newRowData>)
cancelRows撤销某些行的编辑状态和数据Function(rows: Array<row>, isCancelData: Boolean(是否回滚数据,默认为true))
cancelCells撤销某些单元格的编辑状态和数据Function(rows: Array<row>, cells: Array<cell>, isCancelData: Boolean(是否回滚数据,默认为true))
validateRows对某些正在编辑的行数据进行校验Function(rows: Array<row>, callback: Function(valid))
validate对整个表格正在编辑的数据进行校验Function(callback: Function(valid))

El-Table-Editabled-Cell Props:

属性说明 类型 默认值
row当前单元格所在的行数据Object--
prop字段名(对应columns数组里面的配置)String--
dependencies可以通过该属性来监听rowStates,row,cellStates上的一些数据的变化来触发某些动作Object--

El-Table-Editabled-Cell Scoped Slot:

插槽名说明scope
--当前单元格的内容{ rowStates: { editing }, cellStates, ownStates: { editing, validateMsg }, validateOwn }

作者wx: ckang1229

1.0.27

5 years ago

1.0.26

5 years ago

1.0.24

5 years ago

1.0.23

5 years ago

1.0.22

5 years ago

1.0.21

5 years ago

1.0.20

5 years ago

1.0.19

5 years ago

1.0.18

5 years ago

1.0.17

5 years ago

1.0.16

5 years ago

1.0.15

5 years ago

1.0.14

5 years ago

1.0.13

5 years ago

1.0.12

5 years ago

1.0.11

5 years ago

1.0.10

5 years ago

1.0.9

5 years ago

1.0.8

5 years ago

1.0.7

5 years ago

1.0.6

5 years ago

1.0.5

5 years ago

1.0.4

5 years ago

1.0.3

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago