VUE+Element實(shí)現(xiàn)增刪改查的示例源碼
前言
&最近因?yàn)橐恍┰?,沒有更博客,昨天老師布置了一個(gè)作業(yè),用vue實(shí)現(xiàn)增刪改查功能,想想這也不難,就做一下試試吧。
因?yàn)樽约簩懙臉邮經(jīng)]有別人做的好,因此我想用現(xiàn)成的UI框架,一直也沒用過Element,就干脆趁機(jī)學(xué)一下吧。
實(shí)驗(yàn)步驟
首先引入一下element的css以及js
<!-- 引入樣式 --> <link rel="stylesheet" rel="external nofollow" rel="external nofollow" > <!-- 引入組件庫(kù) --> <script src="https://unpkg.com/element-ui/lib/index.js"></script>
然后引入需要用到的vue相關(guān)的js文件
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
下面說一下HTML結(jié)構(gòu)
<div id="app">
<h1>職位的增刪改查</h1>
<div class="head">
<el-row :gutter="20">
<el-col :span="6">
<el-input v-model="userInfo.name" placeholder="請(qǐng)輸入你的公司名"></el-input>
</el-col>
<el-col :span="6">
<el-input v-model="userInfo.position" placeholder="請(qǐng)輸入你的職位"></el-input>
</el-col>
<el-col :span="6">
<el-input v-model="userInfo.major" placeholder="請(qǐng)輸入你的專業(yè)"></el-input>
</el-col>
<el-col :span="6">
<el-input v-model="userInfo.number" placeholder="請(qǐng)輸入數(shù)量"></el-input>
</el-col>
</el-row>
<el-button type="primary" @click="addUser" class="add-btn" plain>添加信息</el-button>
</div>
<!-- 主體內(nèi)容 -->
<div class="body">
<template>
<el-table :data="tableData" style="width: 100%">
<el-table-column label="序號(hào)" width="180"><template slot-scope="scope"> {{scope.$index + 1 }} </template></el-table-column>
<el-table-column prop="name" label="公司名" width="180"></el-table-column>
<el-table-column prop="position" label="職位"></el-table-column>
<el-table-column prop="major" label="專業(yè)"></el-table-column>
<el-table-column prop="number" label="數(shù)量"></el-table-column>
<el-table-column prop="birthday" label="操作">
<template slot-scope="scope">
<el-button type="primary" icon="el-icon-edit" @click="editUser(scope.row,scope.$index)" circle></el-button>
<el-button type="danger" icon="el-icon-delete" @click="delUser(scope.$index)" circle></el-button>
</template>
</el-table-column>
</el-table>
</template>
</div>
<!-- 編輯框 -->
<el-dialog title="編輯用戶信息" :visible.sync="dialogVisible" width="30%" :before-close="handleClose">
<div>
<el-form ref="form" :model="editObj" label-width="80px">
<el-form-item label="公司名"><el-input v-model="editObj.name"></el-input></el-form-item>
<el-form-item label="職位"><el-input v-model="editObj.position"></el-input></el-form-item>
<el-form-item label="專業(yè)"><el-input v-model="editObj.major"></el-input></el-form-item>
<el-form-item label="數(shù)量"><el-input v-model="editObj.number"></el-input></el-form-item>
</el-form>
</div>
<span slot="footer" class="dialog-footer">
<el-button @click="dialogVisible = false">取 消</el-button>
<el-button type="primary" @click="confirm">確 定</el-button>
</span>
</el-dialog>
</div>
這一段是element的表單以及編輯等樣式 ,其中添加了一些click操作 后面需要用到
加上基礎(chǔ)的樣式
<style>
#app{
width:1024px;
margin: 0 auto;
}
.add-btn{
margin-top: 20px;
width: 100%;
}
.body{
margin-top:20px;
}
</style>
現(xiàn)在頁(yè)面的基本樣式就做好了,如下圖所示:

下面開始寫vue代碼,對(duì)各個(gè)功能進(jìn)行處理操作
了解過vuejs的應(yīng)該知道這樣的結(jié)構(gòu) data里面填寫我們獲取的數(shù)據(jù) 一些規(guī)則,定義一些變量 ,在methods進(jìn)行我們的操作。
new Vue({
el: '#app',
data:{},
methods:{}
})
data: function(){
return{
userInfo:{
name:'',
position: '',
major: '',
number: '',
},
tableData: [{
name:'互聯(lián)網(wǎng)+學(xué)院',
position: '專職教師',
major: '對(duì)外貿(mào)易',
number: '2',
},{
name:'徐州重工',
position: '工廠車研發(fā)部工程師',
major: '精密機(jī)械制造',
number: '12',
},{
name:'北京青碼科技',
position: '前端開發(fā)工程師',
major: 'Vue、React',
number: '4',
}
],
dialogVisible: false,
editObj:{
name:'',
position: '',
major: '',
number: '',
},
userIndex:0,
}
},
接下來我們添加methods
- addUser() 是添加數(shù)據(jù)
- delUser()是刪除數(shù)據(jù)
- editUser()是編輯數(shù)據(jù)
- handleClose()是是否彈出編輯框
- confirm()是確認(rèn)信息并且傳數(shù)據(jù)到表格中
在增加模塊中,我做了信息判斷,如果是信息是空就會(huì)彈出提示框,顯示信息不能為空,
在刪除模塊中,點(diǎn)擊可以刪除一行信息
在修改模塊中,會(huì)先將原本的信息拿到,然后再修改你需要修改的信息。
methods:{
//添加
addUser(){
if(!this.userInfo.name){
this.$message({
message: '請(qǐng)輸入你的公司名!',
});
return;
}
if(!this.userInfo.position){
this.$message({
message: '請(qǐng)輸入你的職位!',
type: 'warning'
});
return;
}
if (!this.userInfo.major) {
this.$message({
message: '請(qǐng)輸入你的專業(yè)!',
type: 'warning'
});
return;
}
if (!this.userInfo.number) {
this.$message({
message: '請(qǐng)輸入數(shù)量!',
type: 'warning'
});
return;
}
this.tableData.push(this.userInfo);
this.userInfo = {
name:'',
position: '',
major: '',
number: '',
};
},
//刪除
delUser(idx){
this.$confirm('確認(rèn)刪除此用戶信息?')
.then(_ => {
this.tableData.splice(idx, 1);
})
.catch(_ => {});
},
//編輯
editUser(item,idx){
this.userIndex = idx;
this.editObj = {
name: item.name,
position: item.position,
major: item.major,
number: item.number,
};
this.dialogVisible = true;
},
handleClose(){
this.dialogVisible = false;
},
confirm(){
this.dialogVisible = false;
Vue.set(this.tableData, this.userIndex, this.editObj);
}
},
})
總結(jié):
通過這次練習(xí),讓我知道了Element框架是怎么使用的,Element框架寫代碼做樣式的確方便,以后有什么要求低的作業(yè)可以拿來使用,目前的我畢竟還是一個(gè)學(xué)生,我還是需要多鍛煉寫代碼,手寫樣式的能力。
最后: 附整個(gè)項(xiàng)目的源代碼,本項(xiàng)目?jī)H供學(xué)習(xí)交流。
源代碼
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel="stylesheet" rel="external nofollow" rel="external nofollow" >
<title>Vue增刪改查</title>
<style>
#app{
width:1024px;
margin: 0 auto;
}
.add-btn{
margin-top: 20px;
width: 100%;
}
.body{
margin-top:20px;
}
</style>
</head>
<body>
<div id="app">
<h1>職位的增刪改查</h1>
<div class="head">
<el-row :gutter="20">
<el-col :span="6">
<el-input v-model="userInfo.name" placeholder="請(qǐng)輸入你的公司名"></el-input>
</el-col>
<el-col :span="6">
<el-input v-model="userInfo.position" placeholder="請(qǐng)輸入你的職位"></el-input>
</el-col>
<el-col :span="6">
<el-input v-model="userInfo.major" placeholder="請(qǐng)輸入你的專業(yè)"></el-input>
</el-col>
<el-col :span="6">
<el-input v-model="userInfo.number" placeholder="請(qǐng)輸入數(shù)量"></el-input>
</el-col>
</el-row>
<el-button type="primary" @click="addUser" class="add-btn" plain>添加信息</el-button>
</div>
<!-- 主體內(nèi)容 -->
<div class="body">
<template>
<el-table :data="tableData" style="width: 100%">
<el-table-column label="序號(hào)" width="180"><template slot-scope="scope"> {{scope.$index + 1 }} </template></el-table-column>
<el-table-column prop="name" label="公司名" width="180"></el-table-column>
<el-table-column prop="position" label="職位"></el-table-column>
<el-table-column prop="major" label="專業(yè)"></el-table-column>
<el-table-column prop="number" label="數(shù)量"></el-table-column>
<el-table-column prop="birthday" label="操作">
<template slot-scope="scope">
<el-button type="primary" icon="el-icon-edit" @click="editUser(scope.row,scope.$index)" circle></el-button>
<el-button type="danger" icon="el-icon-delete" @click="delUser(scope.$index)" circle></el-button>
</template>
</el-table-column>
</el-table>
</template>
</div>
<!-- 編輯框 -->
<el-dialog title="編輯用戶信息" :visible.sync="dialogVisible" width="30%" :before-close="handleClose">
<div>
<el-form ref="form" :model="editObj" label-width="80px">
<el-form-item label="公司名"><el-input v-model="editObj.name"></el-input></el-form-item>
<el-form-item label="職位"><el-input v-model="editObj.position"></el-input></el-form-item>
<el-form-item label="專業(yè)"><el-input v-model="editObj.major"></el-input></el-form-item>
<el-form-item label="數(shù)量"><el-input v-model="editObj.number"></el-input></el-form-item>
</el-form>
</div>
<span slot="footer" class="dialog-footer">
<el-button @click="dialogVisible = false">取 消</el-button>
<el-button type="primary" @click="confirm">確 定</el-button>
</span>
</el-dialog>
</div>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script src="https://unpkg.com/element-ui/lib/index.js"></script>
<script>
new Vue({
el:'#app',
data: function(){
return{
userInfo:{
name:'',
position: '',
major: '',
number: '',
},
tableData: [{
name:'互聯(lián)網(wǎng)+學(xué)院',
position: '專職教師',
major: '對(duì)外貿(mào)易',
number: '2',
},{
name:'徐州重工',
position: '工廠車研發(fā)部工程師',
major: '精密機(jī)械制造',
number: '12',
},{
name:'北京青碼科技',
position: '前端開發(fā)工程師',
major: 'Vue、React',
number: '4',
}
],
dialogVisible: false,
editObj:{
name:'',
position: '',
major: '',
number: '',
},
userIndex:0,
}
},
methods:{
//添加
addUser(){
if(!this.userInfo.name){
this.$message({
message: '請(qǐng)輸入你的公司名!',
});
return;
}
if(!this.userInfo.position){
this.$message({
message: '請(qǐng)輸入你的職位!',
type: 'warning'
});
return;
}
if (!this.userInfo.major) {
this.$message({
message: '請(qǐng)輸入你的專業(yè)!',
type: 'warning'
});
return;
}
if (!this.userInfo.number) {
this.$message({
message: '請(qǐng)輸入數(shù)量!',
type: 'warning'
});
return;
}
this.tableData.push(this.userInfo);
this.userInfo = {
name:'',
position: '',
major: '',
number: '',
};
},
//刪除
delUser(idx){
this.$confirm('確認(rèn)刪除此用戶信息?')
.then(_ => {
this.tableData.splice(idx, 1);
})
.catch(_ => {});
},
//編輯
editUser(item,idx){
this.userIndex = idx;
this.editObj = {
name: item.name,
position: item.position,
major: item.major,
number: item.number,
};
this.dialogVisible = true;
},
handleClose(){
this.dialogVisible = false;
},
confirm(){
this.dialogVisible = false;
Vue.set(this.tableData, this.userIndex, this.editObj);
}
},
})
</script>
</body>
</html>
以上就是VUE+Element實(shí)現(xiàn)增刪改查的示例源碼的詳細(xì)內(nèi)容,更多關(guān)于VUE+Element實(shí)現(xiàn)增刪改查的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
vue2.0中g(shù)oods選購(gòu)欄滾動(dòng)算法的實(shí)現(xiàn)代碼
這篇文章主要介紹了vue2.0中g(shù)oods選購(gòu)欄滾動(dòng)算法的實(shí)現(xiàn)代碼,需要的朋友可以參考下2017-05-05
vue3項(xiàng)目使用pinia狀態(tài)管理器的使用
Pinia是一個(gè)專為Vue3設(shè)計(jì)的現(xiàn)代化狀態(tài)管理庫(kù),本文主要介紹了vue3項(xiàng)目使用pinia狀態(tài)管理器的使用,具有一定的參考價(jià)值,感興趣的可以了解一下2024-05-05
Ant Design的可編輯Tree的實(shí)現(xiàn)操作
這篇文章主要介紹了Ant Design的可編輯Tree的實(shí)現(xiàn)操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-10-10
vue3?setup語(yǔ)法糖下父組件如何調(diào)用子組件
這篇文章主要介紹了vue3?setup語(yǔ)法糖下父組件如何調(diào)用子組件問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-03-03
vue通過watch對(duì)input做字?jǐn)?shù)限定的方法
本篇文章主要介紹了vue通過watch對(duì)input做字?jǐn)?shù)限定的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07
使用Vant如何實(shí)現(xiàn)數(shù)據(jù)分頁(yè),下拉加載
這篇文章主要介紹了使用Vant實(shí)現(xiàn)數(shù)據(jù)分頁(yè)及下拉加載方式。具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-06-06
vue寫一個(gè)全局彈窗組件通過js調(diào)用的方法
彈窗效果是在Web開發(fā)中經(jīng)常用到的一種交互效果,它可以在用戶點(diǎn)擊某個(gè)按鈕或者觸發(fā)某個(gè)事件時(shí)顯示一個(gè)懸浮框,提供用戶與頁(yè)面進(jìn)行交互的機(jī)會(huì),這篇文章主要給大家介紹了關(guān)于vue寫一個(gè)全局彈窗組件通過js調(diào)用的方法,需要的朋友可以參考下2024-06-06
淺談Vue使用Elementui修改默認(rèn)的最快方法
這篇文章主要介紹了淺談Vue使用Elementui修改默認(rèn)的最快方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-12-12
vue實(shí)現(xiàn)鼠標(biāo)移入移出事件代碼實(shí)例
這篇文章主要介紹了vue實(shí)現(xiàn)鼠標(biāo)移入移出事件,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-03-03

