一、用戶管理
- 界面
<template>
<!-- 用戶管理 -->
<div>
<el-row class="topRow">
<el-button type="primary" @click="addUser">添加用戶</el-button>
<el-button type="success" @click="batchDel">批量刪除</el-button>
</el-row>
<el-table
ref="multipleTable"
:data="userData"
tooltip-effect="dark"
style="width: 100%"
@selection-change="handleSelectionChange"
>
<!-- 復(fù)選接鈕 -->
<el-table-column type="selection" width="55"></el-table-column>
<el-table-column label="編號(hào)" prop="id" width="120"></el-table-column>
<el-table-column prop="username" label="用戶" width="120"></el-table-column>
<el-table-column prop="createTime" label="創(chuàng)建日期" show-overflow-tooltip></el-table-column>
<el-table-column label="操作" show-overflow-tooltip>
<template slot-scope="scope">
<el-button size="mini" @click="handleEdit(scope.$index, scope.row)">編輯</el-button>
<el-button size="mini" type="danger" @click="handleDelete(scope.$index, scope.row)">刪除</el-button>
</template>
</el-table-column>
</el-table>
<el-dialog :title="title" :visible.sync="dialogVisible" width="30%" :before-close="handleClose">
<el-form ref="form" :model="form" label-width="80px">
<el-form-item label="編號(hào)">
<el-input v-model="form.id"></el-input>
</el-form-item>
<el-form-item label="用戶">
<el-input v-model="form.username"></el-input>
</el-form-item>
</el-form>
<span slot="footer" class="dialog-footer">
<el-button @click="dialogVisible = false">取 消</el-button>
<el-button type="primary" @click="save">確 定</el-button>
</span>
</el-dialog>
</div>
</template>
<script>
export default {
data() {
return {
form: { id: "", username: "", createTime: "" },
userData: [
{ id: 1, username: "王小虎", createTime: "2019-08-13 15:00:00" },
{ id: 2, username: "李小七", createTime: "2019-08-13 15:00:00" },
{ id: 3, username: "張小八", createTime: "2019-08-13 15:00:00" }
],
multipleSelection: [],
title: "",
dialogVisible: false
};
},
methods: {
// 選擇
handleSelectionChange(val) {
this.multipleSelection = val;
},
handleClose() {
this.dialogVisible = false;
},
}
};
</script>
<style scoped>
.topRow {
margin-bottom: 10px;
float: left;
}
</style>
- 刪除事件
// 刪除
handleDelete(index, rows) {
this.userData.splice(index, 1);
},
batchDel() {
this.multipleSelection.forEach(element => {
this.userData.forEach((item, index) => {
if (item.username == element.username) {
this.userData.splice(index, 1);
}
});
});
},
- 添加編輯事件
addUser() {
this.form.id = "";
this.form.username = "";
this.title = "添加用戶";
this.dialogVisible = true;
},
// 編輯
handleEdit(index, rows) {
this.title = "編輯用戶";
this.dialogVisible = true;
this.form.id = rows.id;
this.form.username = rows.username;
console.log(index + "->" + rows.username);
},
- 保存事件
save() {
if (this.title == "添加用戶") {
let user = { id: "", username: "", createTime: "" };
user.id = this.form.id;
user.username = this.form.username;
user.createTime= "2019-08-14 10:00:00";
this.userData.push(user);
this.dialogVisible = false;
} else if (this.title == "編輯用戶") {
this.userData.forEach((item,index)=>{
if(item.username==this.form.username){
item.username=this.form.username;
this.dialogVisible = false;
return;
}
});
}
},

界面

添加對(duì)話框