VUE 实现table 动态增加行增加列,且行内属性自定义,行内内容可编辑,标题栏内容可编辑

VUE 实现table 动态增加行增加列,且行内属性自定义,行内内容可编辑,标题栏内容可编辑

最终实现效果如下图

  源码

<template>
  <div class="edit-box">
    <div class="botom-box">
        <Button type="primary" size="small" @click="AddRow">增加行</Button>
        <Button type="primary" size="small" @click="AddColumn">增加列</Button>
    </div>
    <div class="data-box" ref="tabdiv">
        <Table :height="tabheight" border :columns="columndata" :data="rowsdata" class="table-css"></Table>
    </div>
  </div>
</template>

<script>

export default {

  data() {
    return {
      tabheight:0,
      columndata:[
        {
          title: '名称',
          key: 'name',
          width:150,
          fixed: 'left',
          resizable:true ,
          render: (h, params) => {
            var _this = this;
            return h('div', [
              h('Icon', {
                props: {
                    type: 'md-close-circle',
                    size:"15"
                }, 
                style:{
                  color:'#FF0000',
                  cursor: 'pointer',
                  float:'left',
                  margin:"5px 10px 0px 0px"
                },
                on: {
                    click: () => {
                      if(_this.rowsdata.length>1){
                        _this.rowsdata.splice(params.index,1);
                      }else{
                        this.$Message.warning('至少要保留一行数据!');
                      }
                    }
                }
              }),
              h('Input', {
                props: {
                  value: params.row[params.column.key],
                },
                style:{
                  width:'75%',
                  float:'left'
                },
                on: {
                  'on-change': event => {
                    params.row[params.column.key] = event.target.value;
                    _this.rowsdata[params.index]= params.row;
                  },
                }
              })
            ])
          }
        },
        {
          title: '属性值',
          key: 'value1',
          width:120,
          resizable:true,
          render: (h, params) => {
            var _this = this;
            return  h('InputNumber', {
              props: {
                value: params.row[params.column.key],
                step: 1,
              },
              style:{
                width:'100%',
                float:'left'
              },
              on: {
                'on-change': event => {
                  params.row[params.column.key] = event;
                  _this.rowsdata[params.index]= params.row;
                },
              }
            })
          },
          renderHeader:(h, params,) => {
            var _this = this;
              return h('div', [
                h('Icon', {
                  props: {
                      type: 'md-close-circle',
                      size:"15"
                  }, 
                  style:{
                    color:'#FF0000',
                    cursor: 'pointer',
                    float:'left',
                    margin:"5px 10px 0px 0px"
                  },
                  on: {
                      click: () => {
                        if(_this.columndata.length>2){
                          _this.columndata.splice(params.index,1);
                        }else{
                          this.$Message.warning('属性值至少保留一列!');
                        }
                      }
                  }
                }),
                h('Input', {
                props: {
                  value: params.column.title,
                },
                style:{
                  width:'70%',
                  float:'left'
                },
                on: {
                  'on-change': event => { 
                    params.column.title = event.target.value;
                    _this.columndata[params.index]= params.column;
                  }
                }
              })
            ])
          }
        }
      ],
      rowsdata:[
          {name:'苹果',value1:11}
      ],
      columnIndex:1//当前动态添加列的数量
    }
  },
  methods: {
    AddRow(){
      var row = JSON.parse(JSON.stringify(this.rowsdata[0]));
      for(var name in row){
        if(name=="name"){
          row[name]="name";  
        }else{
          row[name]=0;  
        }     
      } 
      this.rowsdata.push(row);
    },
    AddColumn(){
      this.columnIndex++;
      var keyName = 'value' + this.columnIndex;
      this.rowsdata.forEach((value , index) => {
        value[keyName] = 0;
      })
      var column = {
          title: '属性值'+this.columnIndex,
          key: keyName,
          width:120,
          resizable:true,
          render: (h, params) => {
            var _this = this;
            return  h('InputNumber', {
              props: {
                value: params.row[params.column.key],
                step: 1,
              },
              style:{
                width:'100%',
                float:'left'
              },
              on: {
                'on-change': event => {
                  params.row[params.column.key] = event;
                  _this.rowsdata[params.index]= params.row;
                },
              }
            })
          },
          renderHeader:(h, params,) => {
            var _this = this;
              return h('div', [
                h('Icon', {
                  props: {
                      type: 'md-close-circle',
                      size:"15"
                  }, 
                  style:{
                    color:'#FF0000',
                    cursor: 'pointer',
                    float:'left',
                    margin:"5px 10px 0px 0px"
                  },
                  on: {
                      click: () => {
                        if(_this.columndata.length>2){
                          _this.columndata.splice(params.index,1);
                        }else{
                          this.$Message.warning('属性值至少保留一列!');
                        }
                      }
                  }
                }),
                h('Input', {
                props: {
                  value: params.column.title,
                },
                style:{
                  width:'70%',
                  float:'left'
                },
                on: {
                  'on-change': event => { 
                    params.column.title = event.target.value;
                    _this.columndata[params.index]= params.column;
                  }
                }
              })
            ])
          }
        }
      this.columndata.push(column);
    }
  },
  mounted() {
    this.tabheight = window.innerHeight*0.5695;
  }
}
</script>

<style scoped>
  .edit-box{ width: 100%; height: 60vh; font-size: 14px;}
  /* padding: 1vh 1vw;  */
  .botom-box {width: 100%; height: 3vh; text-align: right;}
  .data-box{ float:left; width: 100%; height:57vh;border: 1px solid rgb(211, 214, 208);    border-radius: .133333rem;}
 
  /* 滚动条 */
  .table-css >>> .ivu-table-overflowY::-webkit-scrollbar {
      width: 10px;
      height: 10px;
  }
  .table-css >>> .ivu-table-overflowX::-webkit-scrollbar {
      width: 10px;
      height: 10px;
  }
</style>

 

  • 6
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 7
    评论
Ant Design VueTable 组件提供了可编辑、可新增、可删除等功能,可以很方便地实现多个值的表格。下面是实现步骤: 1. 在页面中引入 Table 组件并配置表头。在表头中定义需要展示的,例如:字段1、字段2、字段3。 ``` <template> <a-table :columns="columns" :data-source="dataSource"></a-table> </template> <script> export default { data() { return { columns: [ { title: '字段1', dataIndex: 'field1', key: 'field1' }, { title: '字段2', dataIndex: 'field2', key: 'field2' }, { title: '字段3', dataIndex: 'field3', key: 'field3' } ], dataSource: [] } } } </script> ``` 2. 在 dataSource 中初始化一空白数据,作为新增数据使用。 ``` data() { return { columns: [...], dataSource: [ { key: '0', field1: '', field2: '', field3: '' } ] } } ``` 3. 针对每一数据,可以添加一个编辑按钮或者直接在单元格中添加编辑功能,让用户可以对数据进修改。这里我们采用在单元格中添加编辑功能的方式,使用 Editable Cell 和 Editable Row 组件来实现。 ``` <template> <a-table :columns="columns" :data-source="dataSource" :components="components"> <template #body="tableProps"> <editable-row :record="tableProps.record" :index="tableProps.index"> <a-table-cell v-for="column in columns" :key="column.dataIndex" :dataIndex="column.dataIndex" :record="tableProps.record" :editable="column.editable" @update="handleUpdate" > {{ tableProps.record[column.dataIndex] }} </a-table-cell> </editable-row> </template> </a-table> </template> <script> import { EditableRow, EditableCell } from 'ant-design-vue'; export default { components: { EditableRow, EditableCell }, data() { return { columns: [...], dataSource: [ { key: '0', field1: '', field2: '', field3: '' } ] } }, methods: { handleUpdate(record, dataIndex, value) { const index = this.dataSource.findIndex(item => item.key === record.key); this.dataSource[index][dataIndex] = value; } } } </script> ``` 在这个例子中,我们使用了 Editable Cell 和 Editable Row 组件来实现单元格和编辑功能。其中,Editable Cell 组件用于渲染单元格内容,Editable Row 组件用于渲染整数据。我们给每个需要编辑的单元格添加了 editable 属性以及 update 事件,当用户修改单元格内容时,会触发 update 事件,我们可以在这个事件中更新 dataSource 中对应的数据。 4. 当用户点击新增按钮时,可以在表格中添加一空白,并让用户输入新的数据。同时,也要提供一个保存按钮,让用户可以保存新增的数据。这里我们通过添加一个按钮来实现新增功能,并在按钮点击事件中向 dataSource 中添加一空白数据。 ``` <template> <div> <a-button type="primary" @click="handleAdd">新增</a-button> <br /><br /> <a-table :columns="columns" :data-source="dataSource" :components="components"> <template #body="tableProps"> <editable-row :record="tableProps.record" :index="tableProps.index"> <a-table-cell v-for="column in columns" :key="column.dataIndex" :dataIndex="column.dataIndex" :record="tableProps.record" :editable="column.editable" @update="handleUpdate" > {{ tableProps.record[column.dataIndex] }} </a-table-cell> </editable-row> </template> </a-table> </div> </template> <script> import { EditableRow, EditableCell } from 'ant-design-vue'; export default { components: { EditableRow, EditableCell }, data() { return { columns: [...], dataSource: [ { key: '0', field1: '', field2: '', field3: '' } ] } }, methods: { handleAdd() { const newData = { key: `${this.dataSource.length}`, field1: '', field2: '', field3: '' }; this.dataSource.push(newData); }, handleUpdate(record, dataIndex, value) { const index = this.dataSource.findIndex(item => item.key === record.key); this.dataSource[index][dataIndex] = value; } } } </script> ``` 在这个例子中,我们添加了一个按钮来触发新增操作,并在按钮点击事件中向 dataSource 中添加了一空白数据。注意,每数据都必须有一个唯一的 key 属性,用于区分不同的数据。 通过以上步骤,就可以实现 Ant Design VueTable 组件中的一多个值可编辑新增的功能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值