zoukankan      html  css  js  c++  java
  • vue antd 动态table

    效果图:

    <template>
    
        <div>
            <a-button class="editable-add-btn" @click="handleAdd">
                Add
              </a-button> 
              <div>
    
                <a-table :columns="columns" :data-source="data" bordered :rowKey="(record,index)=>{return index}">
                    <template
                      v-for="col in ['Key', 'value', 'desc']"
                      :slot="col"
                      slot-scope="text, record, index"
                      
                    >
                      <div :key="col"  >
                        <a-input
                          v-if="canedit"
                          style="margin: -5px 0"
                          :value="text"
                          @change="e => handleChange(e.target.value, index, col,record)"
                        />
                        <template v-else>
                          {{ text }}
                        </template>
                      </div>
                    </template>
                    
    
    
                    <template slot="operation" slot-scope="text, record, index">
                              <a-popconfirm
                              v-if="data.length"
                              title="Sure to delete?"
                              @confirm="() => onDelete(index,record)"
                          >
                              <a href="javascript:;">Delete</a>
                          </a-popconfirm>
              
                    </template>
                  </a-table>
              </div>
        </div>
    
      </template>
      <script>
      const columns = [
        {
          title: 'Key',
          dataIndex: 'Key',
           '25%',
          scopedSlots: { customRender: 'Key' },
        },
        {
          title: 'value',
          dataIndex: 'value',
           '25%',
          scopedSlots: { customRender: 'value' },
        },
        {
          title: 'desc',
          dataIndex: 'desc',
           '30%',
          scopedSlots: { customRender: 'desc' },
        },
        {
          title: 'operation',
          dataIndex: 'operation',
          scopedSlots: { customRender: 'operation' },
        },
      ];
      
      const data = [];
      for (let i = 0; i < 5; i++) {
        data.push({
          Key: `key ${i}`,
          value: i,
          desc: `London Park no. ${i}`,
        });
      }
      export default {
        data() {
          this.cacheData = data.map(item => ({ ...item }));
          return {
    
            canedit:true,
            data,
            columns,
            editingKey: '',
          };
        },
        methods: {
          handleChange(value, index, column,record) {
            console.log(value)
            record[column]=value
            let newitem= record
            console.log(newitem)
            this.data.splice(index, 1, newitem)
          },
          onDelete(index){
            this.data.splice(index,1)
          },
          handleAdd() {
          console.log(this.data.length)
          const newData = {
            Key: '22',
            value: 32,
            desc: `London, Park Lane no `,
          };
          this.data = [...this.data, newData];
       
            },
        },
      };
      </script>
      <style scoped>
      .editable-row-operations a {
        margin-right: 8px;
      }
      .editable-add-btn {
      margin-bottom: 8px;
    }
      </style>
      
    

      

  • 相关阅读:
    sqlserver 游标格式 东师理想
    java流下载 东师理想
    列出所有K个元素的子集2013年1月26日
    你刚才在淘宝上买了一件东西【技术普及贴】(转载)
    递归统计项目中的非空白代码行数
    算法(数据结构)每天一题 2013年1月21日
    《TCP/IP详解卷1》学习小结(二)Internet Protocol
    产生所有排列字典顺序2013年1月23日
    产生所有排列旋转法2013年1月22日
    《TCP/IP详解卷1》学习小结(三)ARP协议与RARP协议
  • 原文地址:https://www.cnblogs.com/SunshineKimi/p/14745032.html
Copyright © 2011-2022 走看看