vuejlementtable表格添加⾏,修改,单独删除⾏,批量删除
⾏操作
1.表格动态添加,也可删除
<template>
<div class="TestWord">
<el-button @click="addLine">添加⾏数</el-button>
<el-button @click="save">保存</el-button>
<el-table
:data="tableData"
>
<el-table-column prop="bookname" label="书名">
<template slot-scope="scope">
<el-input v-model="w.bookname" placeholder="书名"></el-input>
</template>
</el-table-column>
<el-table-column prop="bookvolume" label="册数">
<template slot-scope="scope">
<el-input v-model="w.bookvolume" placeholder="册数"></el-input>
</template>
</el-table-column>
<el-table-column prop="bookbuyer" label="购买者">
<template slot-scope="scope">
<el-input v-model="w.bookbuyer" placeholder="购买者"></el-input>
</template>
</el-table-column>
<el-table-column prop="bookborrower" label="借阅者">
<template slot-scope="scope">
<el-input v-model="w.bookborrower" placeholder="借阅者"></el-input>
</template>
</el-table-column>
<el-table-column prop="bookbuytime" label="购买⽇期">
<template slot-scope="scope">
<el-date-picker
v-model="w.bookbuytime"
type="date"
format="yyyy-MM-dd"
value-format="yyyy-MM-dd"
placeholder="购买⽇期">
</el-date-picker>
</template>
</el-table-column>
<el-table-column prop="bookbuytime" label="购买⽇期">
<template slot-scope="scope">
<el-button
size="mini"
type="danger"
v-if="!w.editing"
icon="el-icon-delete"
@click="handleDelete(scope.$index, w)">删除
</el-button>
</template>
</el-table-column>
</el-table>
</div>
</template>
vuejs 代码
export default {
name:'TestWorld',情侣间的小游戏
data() {
return {
tableData:[{
bookname: '',
bookbuytime: '',
bookbuyer: '',
bookborrower: '',
bookvolume:''
}]
}
},
methods:{
addLine(){ //添加⾏数
var newValue = {
bookname: '',
bookbuytime: '',
bookbuyer: '',
bookborrower: '',
bookvolume:''
};
//添加新的⾏数
this.tableData.push(newValue);
},
handleDelete(index){ //删除⾏数
this.tableData.splice(index, 1)
暨南大学排名
},
save(){
//这部分应该是保存提交你添加的内容
console.log(JSON.stringify(this.tableData))
}
}
}
运⾏图⽚
2.编辑表格(即使input已经修改过,当点击取消时,内容不会变)
<template>
<div class="TestWorld">
<el-button @click="savemodify">保存</el-button>
<el-table
:data="modifyData"
>
<el-table-column prop="bookname" label="书名">
<template slot-scope="scope">
<template v-if="w.editing">
<el-input class="edit-input" v-model="w.bookname" placeholder="书名"></el-input> </template>
<span v-el>{{ w.bookname }}</span>
</template>
</el-table-column>
<el-table-column prop="bookvolume" label="册数">
<template slot-scope="scope">
<template v-if="w.editing">
<el-input class="edit-input" v-model="w.bookvolume" placeholder="册数"></el-input> </template>
<span v-el>{{ w.bookvolume}}</span>
</template>
</el-table-column>
劳动的诗句
<el-table-column prop="bookbuyer" label="购买者">
<template slot-scope="scope">
<template v-if="w.editing">
<el-input class="edit-input" v-model="w.bookbuyer" placeholder="购买者"></el-input>
</template>
<span v-el>{{w.bookbuyer}}</span>
</template>
</el-table-column>
<el-table-column prop="bookborrower" label="借阅者">
<template slot-scope="scope">
<template v-if="w.editing">
<el-input class="edit-input" v-model="w.bookborrower" placeholder="借阅者"></el-input> </template>
<span v-el>{{w.bookborrower}}</span>
</template>
</el-table-column>
<el-table-column prop="bookbuytime" label="购买⽇期">
<template slot-scope="scope">
<template v-if="w.editing">
<el-date-picker
v-model="w.bookbuytime"
type="date"
value-format="yyyy-MM-dd"
placeholder="购买⽇期">
</el-date-picker>
</template>
<span v-el>{{w.bookbuytime}}</span>
</template>
</el-table-column>
<el-table-column prop="editing" label="操作">
<template slot-scope="scope">
<el-button
type="danger"
绘声绘色的意思v-if="!w.editing"
icon="el-icon-delete"
v-model="scope.$index"
@click="handleEdit(scope.$index, w)">编辑
</el-button>
<el-button
v-el
type="danger"
icon="el-icon-delete"
v-model="scope.$index"
@click="handleCancle(scope.$index, w)">取消
</el-button>
</template>
</el-table-column>
</el-table>
</div>
</template>
vuejs 代码
export default {
name:'TestWorld',
data() {
return {
modifyData:[],
prevValue:{}
}
},
mounted(){
},
methods:{
getData(){
this.$ajax({
method: 'get',
url:'../static/json/1.1.1.json', //<---本地地址
//url: '/api/drummer/8bd17859',
}).then((respon)=>{
console.log(JSON.stringify(respon.data))
let _data = respon.data;
let datalength = _data.length;
for(let i = 0;i < datalength; i++){
this.$t(_data[i], 'editing', fal)李重进
}
//赋值
}).catch(function(err){
console.log(err)
})
},
handleEdit(index,row){
row.editing = true;
console.log(index)
this.prevValue = JSON.par(JSON.stringify(row));
},
handleCancle(index,row){
row.editing = fal;
let prevContent = this.prevValue.bookname;
this.$t(row,"bookname",prevContent);
},
savemodify(){
console.log(JSON.difyData))
}
}
}
本地的1.1.1.JSON数据
[{"bookname":"普通⾼等教育物联⽹⼯程专业规划⽤书:物联⽹技术概论","bookbuytime": "2016-05-
04","bookbuyer": "李晓⽉","bookborrower": "王⼩虎","bookvolume":"1"},{"bookname":"区块链⾰命:⽐特币底层技术如何改变货币、商业和世界","bookbuytime": "2016-05-04","bookbuyer": "李晓⽉","bookborrower": "李⼩
虎","bookvolume":"1"},{"bookname":"⼤家⼀起学配⾊:数学⾊彩设计全能书","bookbuytime": "2017-12-
04","bookbuyer": "张晓⽉","bookborrower": "王⽽虎","bookvolume":"1"}]
如果不⽤get本地数据,vuejs如下
export default {
name:'TestWorld',
data() {
return {
modifyData:[公关媒介
{
bookname: '普通⾼等教育物联⽹⼯程专业规划⽤书:物联⽹技术概论',
bookbuytime: '2016-05-04',
bookbuyer: '李晓⽉',
bookborrower: '王⼩虎',
bookvolume: '1',
editing: fal
},
{
bookname: '区块链⾰命:⽐特币底层技术如何改变货币、商业和世界',
bookbuytime: '2016-05-04',
公司乔迁之喜贺词bookbuyer: '李晓⽉',
bookborrower: '李⼩虎',
bookvolume: '1',
editing: fal
},
{
bookname: '⼤家⼀起学配⾊:数学⾊彩设计全能书',
bookbuytime: '2017-12-04',
bookbuyer: '张晓⽉',
bookborrower: '王⽽虎',
bookvolume: '1',
editing: fal
}
],
prevValue:{}
}
},
methods:{
handleEdit(index,row){ //编辑
row.editing = true;
console.log(index)
this.prevValue = JSON.par(JSON.stringify(row));
},
handleCancle(index,row){ //取消
row.editing = fal;
let prevContent = this.prevValue.bookname;
this.$t(row,"bookname",prevContent);
},
savemodify(){
百鸟公园console.log(JSON.difyData))
}
}
}
运⾏图
3.批量删除⾏数
<template>
<div class="TestWorld">
<el-table ref="multipleTable" :data="tableData3" tooltip-effect="dark" @lection-change="handleSelectionChange"> <el-table-column type="lection" width="55">
</el-table-column>
<el-table-column label="⽇期" width="120">
<template slot-scope="scope">{{ w.date }}</template>
</el-table-column>
<el-table-column prop="name" label="姓名" width="120">
</el-table-column>
<el-table-column prop="address" label="地址" show-overflow-tooltip>
</el-table-column>
</el-table>
<div >
<el-button @click="batchDelete">批量删除</el-button>
<el-button @click="toggleSelection()">取消选择</el-button>
</div>
</div>
</template>
vuejs 代码
export default {
name:'TestWorld',
data() {
return {
tableData3: [
{
date: '2016-05-03',
name: '王⼩虎',
address: '上海市普陀区⾦沙江路 1518 弄'
},
{
date: '2016-05-02',
name: '王⼩虎',
address: '上海市普陀区⾦沙江路 1518 弄'
},
{
date: '2016-05-04',
name: '王⼩虎',