DDR爱好者之家 Design By 杰米
最近在做可编辑特定列的单元格的elementUI table,看了N多的开源、文章,找到一个很优雅的实现方式,分享给大家。
PS:单元格可编辑的table,用英文搜索:Inline editable table with ElementUI 会得到高质量结果。
先上效果:
APP.vue:
<template> <div id="app"> <div style="margin-bottom: 30px"> <el-switch style="display: block" v-model="editModeEnabled" active-color="#13ce66" inactive-color="#ff4949" active-text="Edit enabled" inactive-text="Edit disabled"> </el-switch> </div> <el-table :data="gridData" style="width: 100%"> <el-table-column label="Name" min-width="180"> <editable-cell slot-scope="{row}" :can-edit="editModeEnabled" v-model="row.name"> <span slot="content">{{row.name}}</span> </editable-cell> </el-table-column> <el-table-column min-wwidth="150" label="Gender"> <editable-cell slot-scope="{row}" editable-component="el-select" :can-edit="editModeEnabled" close-event="change" v-model="row.gender"> <el-tag size="medium" :type="row.gender === 'M' " slot="content"> {{row.gender === 'M' "edit-component-slot"> <el-option value="M" label="Male"></el-option> <el-option value="F" label="Female"></el-option> </template> </editable-cell> </el-table-column> <el-table-column label="Birth Date" min-width="250"> <editable-cell slot-scope="{row}" :can-edit="editModeEnabled" editable-component="el-date-picker" format="yyyy-MM-dd" value-format="yyyy-MM-dd" v-model="row.date"> <span slot="content">{{row.date}}</span> </editable-cell> </el-table-column> </el-table> </div> </template> <script> import EditableCell from "./components/EditableCell.vue"; export default { name: "App", components: { EditableCell }, data() { return { editModeEnabled: false, gridData: [ { date: "2016-05-03", name: "Tom", gender: "M" }, { date: "2016-05-02", name: "Lisa", gender: "F" }, { date: "2016-05-04", name: "Jon", gender: "M" }, { date: "2016-05-01", name: "Mary", gender: "F" } ] }; } }; </script> <style> .edit-cell { min-height: 35px; cursor: pointer; } </style>
EditeableCell.vue:
<template> <div @click="onFieldClick" class="edit-cell"> <el-tooltip v-if="!editMode && !showInput" :placement="toolTipPlacement" :open-delay="toolTipDelay" :content="toolTipContent"> <div tabindex="0" class="cell-content" :class="{'edit-enabled-cell': canEdit}" @keyup.enter="onFieldClick"> <slot name="content"></slot> </div> </el-tooltip> <component :is="editableComponent" v-if="editMode || showInput" ref="input" @focus="onFieldClick" @keyup.enter.native="onInputExit" v-on="listeners" v-bind="$attrs" v-model="model"> <slot name="edit-component-slot"></slot> </component> </div> </template> <script> export default { name: "editable-cell", inheritAttrs: false, props: { value: { type: String, default: "" }, toolTipContent: { type: String, default: "Click to edit" }, toolTipDelay: { type: Number, default: 500 }, toolTipPlacement: { type: String, default: "top-start" }, showInput: { type: Boolean, default: false }, editableComponent: { type: String, default: "el-input" }, closeEvent: { type: String, default: "blur" }, canEdit: { type: Boolean, default: false } }, data() { return { editMode: false }; }, computed: { model: { get() { return this.value; }, set(val) { this.$emit("input", val); } }, listeners() { return { [this.closeEvent]: this.onInputExit, ...this.$listeners }; } }, methods: { onFieldClick() { if (this.canEdit) { this.editMode = true; this.$nextTick(() => { let inputRef = this.$refs.input; if (inputRef && inputRef.focus) { inputRef.focus(); } }); } }, onInputExit() { this.editMode = false; }, onInputChange(val) { this.$emit("input", val); } } }; </script> <style> .cell-content { min-height: 40px; padding-left: 5px; padding-top: 5px; border: 1px solid transparent; } .edit-enabled-cell { border: 1px dashed #409eff; } </style>
github:https://github.com/heianxing/editable-table-idea-vue-element
另外一个单元格编辑的例子:
App.vue:
<template> <div id="app"> <el-tooltip content="Click on any of the cells or on the edit button to edit content"> <i class="el-icon-info"></i> </el-tooltip> <el-table :data="gridData" style="width: 100%"> <el-table-column label="Operations" min-width="180"> <template slot-scope="{row, index}"> <el-button icon="el-icon-edit" @click="setEditMode(row, index)"> </el-button> <el-button type="success" icon="el-icon-check" @click="saveRow(row, index)"> </el-button> </template> </el-table-column> <el-table-column label="Name" min-width="180"> <editable-cell :show-input="row.editMode" slot-scope="{row}" v-model="row.name"> <span slot="content">{{row.name}}</span> </editable-cell> </el-table-column> <el-table-column min-wwidth="150" label="Gender"> <editable-cell :show-input="row.editMode" slot-scope="{row}" editable-component="el-select" close-event="change" v-model="row.gender"> <el-tag size="medium" :type="row.gender === 'M' " slot="content"> {{row.gender === 'M' "edit-component-slot"> <el-option value="M" label="Male"></el-option> <el-option value="F" label="Female"></el-option> </template> </editable-cell> </el-table-column> <el-table-column label="Birth Date" min-width="250"> <editable-cell :show-input="row.editMode" slot-scope="{row}" editable-component="el-date-picker" format="yyyy-MM-dd" value-format="yyyy-MM-dd" v-model="row.date"> <span slot="content">{{row.date}}</span> </editable-cell> </el-table-column> </el-table> </div> </template> <script> import EditableCell from "./components/EditableCell.vue"; export default { name: "App", components: { EditableCell }, data() { return { gridData: [ { date: "2016-05-03", name: "Tom", gender: "M" }, { date: "2016-05-02", name: "Lisa", gender: "F" }, { date: "2016-05-04", name: "Jon", gender: "M" }, { date: "2016-05-01", name: "Mary", gender: "F" } ] }; }, methods: { setEditMode(row, index) { row.editMode = true; }, saveRow(row, index) { row.editMode = false; } }, mounted() { this.gridData = this.gridData.map(row => { return { ...row, editMode: false }; }); } }; </script> <style> .edit-cell { min-height: 35px; cursor: pointer; } </style>
EditeableCell.vue:
<template> <div @click="onFieldClick" class="edit-cell"> <el-tooltip v-if="!editMode && !showInput" :placement="toolTipPlacement" :open-delay="toolTipDelay" :content="toolTipContent"> <div tabindex="0" @keyup.enter="onFieldClick"> <slot name="content"></slot> </div> </el-tooltip> <component :is="editableComponent" v-if="editMode || showInput" ref="input" @focus="onFieldClick" @keyup.enter.native="onInputExit" v-on="listeners" v-bind="$attrs" v-model="model"> <slot name="edit-component-slot"></slot> </component> </div> </template> <script> export default { name: "editable-cell", inheritAttrs: false, props: { value: { type: String, default: "" }, toolTipContent: { type: String, default: "Click to edit" }, toolTipDelay: { type: Number, default: 500 }, toolTipPlacement: { type: String, default: "top-start" }, showInput: { type: Boolean, default: false }, editableComponent: { type: String, default: "el-input" }, closeEvent: { type: String, default: "blur" } }, data() { return { editMode: false }; }, computed: { model: { get() { return this.value; }, set(val) { this.$emit("input", val); } }, listeners() { return { [this.closeEvent]: this.onInputExit, ...this.$listeners }; } }, methods: { onFieldClick() { this.editMode = true; this.$nextTick(() => { let inputRef = this.$refs.input; if (inputRef) { inputRef.focus(); } }); }, onInputExit() { this.editMode = false; }, onInputChange(val) { this.$emit("input", val); } } }; </script> <style> </style>
github:https://github.com/heianxing/editable-table-component-vue-element
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
DDR爱好者之家 Design By 杰米
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
DDR爱好者之家 Design By 杰米
暂无评论...
更新日志
2024年11月25日
2024年11月25日
- 凤飞飞《我们的主题曲》飞跃制作[正版原抓WAV+CUE]
- 刘嘉亮《亮情歌2》[WAV+CUE][1G]
- 红馆40·谭咏麟《歌者恋歌浓情30年演唱会》3CD[低速原抓WAV+CUE][1.8G]
- 刘纬武《睡眠宝宝竖琴童谣 吉卜力工作室 白噪音安抚》[320K/MP3][193.25MB]
- 【轻音乐】曼托凡尼乐团《精选辑》2CD.1998[FLAC+CUE整轨]
- 邝美云《心中有爱》1989年香港DMIJP版1MTO东芝首版[WAV+CUE]
- 群星《情叹-发烧女声DSD》天籁女声发烧碟[WAV+CUE]
- 刘纬武《睡眠宝宝竖琴童谣 吉卜力工作室 白噪音安抚》[FLAC/分轨][748.03MB]
- 理想混蛋《Origin Sessions》[320K/MP3][37.47MB]
- 公馆青少年《我其实一点都不酷》[320K/MP3][78.78MB]
- 群星《情叹-发烧男声DSD》最值得珍藏的完美男声[WAV+CUE]
- 群星《国韵飘香·贵妃醉酒HQCD黑胶王》2CD[WAV]
- 卫兰《DAUGHTER》【低速原抓WAV+CUE】
- 公馆青少年《我其实一点都不酷》[FLAC/分轨][398.22MB]
- ZWEI《迟暮的花 (Explicit)》[320K/MP3][57.16MB]