Vue動態(tài)操作表格

https://www.cnblogs.com/PoetryAndYou/p/12729513.html

前提

iviewui主題
使用方法見官網(wǎng)↓
iviewui

效果

image

安裝iviewui

  • 此處安裝可參考官網(wǎng)

方式一:CDN 引入

<!-- import Vue.js -->
<script src="http://vuejs.org/js/vue.min.js"></script>
<!-- import stylesheet -->
<link rel="stylesheet" >
<!-- import iView -->
<script src="http://unpkg.com/view-design/dist/iview.min.js"></script>

方式二: NPM

$ npm install view-design --save

mian.js配置

import Vue from 'vue';
import VueRouter from 'vue-router';
import App from 'components/app.vue';
import Routers from './router.js';
import ViewUI from 'view-design';
import 'view-design/dist/styles/iview.css';

Vue.use(VueRouter);
Vue.use(ViewUI);

// The routing configuration
const RouterConfig = {
    routes: Routers
};
const router = new VueRouter(RouterConfig);

new Vue({
    el: '#app',
    router: router,
    render: h => h(App)
});

代碼

<template>
    <div>
    <Row>
      <Col span="4" style="padding-right:10px">
        <Input type="text" v-model="name" placeholder="姓名" />
      </Col>
      <Col span="4" style="padding-right:10px">
        <Input type="text" v-model="age" placeholder="地址"/>
      </Col>
      <Col span="4" style="padding-right:10px">
        <Input type="text" v-model="birthday" placeholder="出生年月" />
      </Col>
      <Col span="4" style="padding-right:10px">
        <Input type="text" v-model="address"  placeholder="地址"/>
      </Col>
    </Row>
  <Button @click="add()">
    添加
  </Button>
  <Table :columns="columns" :data="data">
    <template slot-scope="{ row, index }" slot="name">
      <Input type="text" v-model="editName" v-if="editIndex === index" />
      <span v-else>{{ row.name }}</span>
    </template>

    <template slot-scope="{ row, index }" slot="age">
      <Input type="text" v-model="editAge" v-if="editIndex === index" />
      <span v-else>{{ row.age }}</span>
    </template>

    <template slot-scope="{ row, index }" slot="birthday">
      <Input type="text" v-model="editBirthday" v-if="editIndex === index" />
      <span v-else>{{ getBirthday(row.birthday) }}</span>
    </template>

    <template slot-scope="{ row, index }" slot="address">
      <Input type="text" v-model="editAddress" v-if="editIndex === index" />
      <span v-else>{{ row.address }}</span>
    </template>

    <template slot-scope="{ row, index }" slot="action">
      <div v-if="editIndex === index">
        <Button @click="handleSave(index)">保存</Button>
        <Button @click="editIndex = -1">取消</Button>
      </div>
      <div v-else>
        <Button @click="handleEdit(row, index)">操作</Button>
        <Button type="error"  @click="remove(index)">Delete</Button>
      </div>
    </template>
  </Table>
 </div>
</template>
<script>
  export default {
    data () {
      return {
         name:'',
        age:'',
        birthday:'',
        address:'',
        columns: [
          {
            title: '姓名',
            slot: 'name'
          },
          {
            title: '年齡',
            slot: 'age'
          },
          {
            title: '出生日期',
            slot: 'birthday'
          },
          {
            title: '地址',
            slot: 'address'
          },
          {
            title: '操作',
            slot: 'action'
          }
        ],
        data:[],
        editIndex: -1,  // 當(dāng)前聚焦的輸入框的行數(shù)
        editName: '',  // 第一列輸入框,當(dāng)然聚焦的輸入框的輸入內(nèi)容,與 data 分離避免重構(gòu)的閃爍
        editAge: '',  // 第二列輸入框
        editBirthday: '',  // 第三列輸入框
        editAddress: '',  // 第四列輸入框
      }
    },
    methods: {
      add(){
       var _this=this
        _this.data.push({
         
          name:_this.name,
          age:_this.age,
          birthday:_this.birthday,
          address:_this.address
          
        })
       console.log(this.data)
      },
      handleEdit (row, index) {
        this.editName = row.name;
        this.editAge = row.age;
        this.editAddress = row.address;
        this.editBirthday = row.birthday;
        this.editIndex = index;
      },
      handleSave (index) {
        this.data[index].name = this.editName;
        this.data[index].age = this.editAge;
        this.data[index].birthday = this.editBirthday;
        this.data[index].address = this.editAddress;
        this.editIndex = -1;
      },
      getBirthday (birthday) {
        const date = new Date(parseInt(birthday));
        const year = date.getFullYear();
        const month = date.getMonth() + 1;
        const day = date.getDate();
        return `${year}-${month}-${day}`;
      },
       remove(index) {
            this.data.splice(index, 1);
      }
    }
  }
</script>
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • vue-cli3項目搭建配置以及性能優(yōu)化 在之前的開發(fā)中主要用的是vue-cli2,最近空閑時間比較多,接下來有新...
    bayi_lzp閱讀 19,557評論 16 68
  • 寫在開頭 先說說為什么要寫這篇文章, 最初的原因是組里的小朋友們看了webpack文檔后, 表情都是這樣的: (摘...
    Lefter閱讀 5,449評論 4 31
  • 前言 注:適用于windows系統(tǒng)本文講述要點(diǎn): vue-cli項目搭建基本環(huán)境配置; 常用插件描述及分享; 項目...
    流眸Tel閱讀 4,056評論 0 10
  • 裊裊薄暮唱晚風(fēng),霰雪萬里孤月朦,怎得月色枕上夢。 何處長亭應(yīng)回首,把酒江月醉相逢。江南憶,樓外樓,逗秋風(fēng)。
    三月里的白閱讀 444評論 0 1
  • 中國 中國 中國 電腦殺毒 電腦殺毒 百度[圖片上傳失敗...(image-8283f-1527220127467)]
    Shocker閱讀 130評論 0 0

友情鏈接更多精彩內(nèi)容