vue與bootstrap實(shí)現(xiàn)簡單用戶信息添加刪除功能
本文實(shí)例為大家分享了vue與bootstrap實(shí)現(xiàn)用戶信息添加刪除操作的具體代碼,供大家參考,具體內(nèi)容如下
小記:
1.v-model="" 用于input表單雙向數(shù)據(jù)綁定 邏輯層跟渲染層雙向綁定
2.v-on:click='add()' click方法綁定
3.v-for='(item,index) in myData' 遍歷數(shù)組 {{index}} {{item.name}} {{item.age}} 適用于vue版本2.0
4.v-for='(item,index,key) in myData' 遍歷json {{index}} {{item}} {{key}} 適用于vue版本2.0
5.v-on:click="currentUser=index" 直接綁定點(diǎn)擊事件改變邏輯層的數(shù)據(jù) currentUser這里是邏輯層的數(shù)據(jù)
6.v-show="myData.length!=0" v-show根據(jù)后面的布爾值覺得顯示還是隱藏 可直接用邏輯層的數(shù)據(jù)進(jìn)行判斷
7.<div class="modal" role='dialog' id="layer"> modal dialog為遮罩框 id用來聯(lián)系觸發(fā)元素
8. data-toggle='modal' 交替顯示隱藏遮罩框 data-target='#layer' 確定目標(biāo)模態(tài)框
9. data-dismiss='modal' 點(diǎn)擊后消失目標(biāo)元素
效果圖:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,user-scalable=no,initial-scale=1.0" >
<title>Document</title>
<!-- 最新版本的 Bootstrap 核心 CSS 文件 -->
<link rel="stylesheet" rel="external nofollow" >
<script src='../jquery-3.2.1.min.js'></script>
<script src='bootstrap.js'></script>
<script src='vue.js'></script>
<style>
table td {vertical-align: middle !important;}
</style>
</head>
<body>
<div class="container">
<form action="" role='form' class="">
<div class="form-group">
<label for="username" class="">用戶名:</label>
<input type="text" id="username" class="form-control" v-model="username" placeholder="請輸入用戶名">
</div>
<div class="form-group">
<label for="age">年 齡:</label>
<input type="text" id="age" class="form-control" v-model="age" placeholder="請輸入年齡">
</div>
<div class="form-group">
<input type="button" value="添加" class="btn btn-primary" v-on:click='add()'>
<input type="reset" value="重置" class="btn btn-warning">
</div>
</form>
<table class="table table-bordered table-hover">
<caption class="h4 text-info text-center">用戶信息表</caption>
<tr class="text-danger">
<th class="text-center">序號</th>
<th class="text-center">姓名</th>
<th class="text-center">年齡</th>
<th class="text-center">操作</th>
</tr>
<tr class="text-center" v-for='(item,index) in myData'>
<td>{{index}}</td>
<td>{{item.name}}</td>
<td>{{item.age}}</td>
<td>
<button class="btn btn-danger btn-xs" data-toggle='modal' data-target='#layer' v-on:click="currentUser=index">刪除</button>
</td>
</tr>
<tr v-show="myData.length!=0">
<td colspan="4" class="text-right">
<button class="btn btn-danger btn-xs" v-on:click='currentUser="all"' data-toggle='modal' data-target="#layer">全部刪除</button>
</td>
</tr>
<tr v-show="myData.length==0">
<td colspan="4" class="text-center">
<p class="text-muted">暫無數(shù)據(jù)...</p>
</td>
</tr>
</table>
<div class="modal fade bs-example-modal-sm" role='dialog' id="layer">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button class="close" data-dismiss='modal'>
<span>×</span>
</button>
<h4 class="modal-title">確認(rèn)刪除嗎?</h4>
</div>
<div class="modal-body text-right">
<button class="btn btn-primary btn-sm" data-dismiss='modal'>取消</button>
<button class="btn btn-danger btn-sm" data-dismiss='modal' v-on:click="deleteuser()">確認(rèn)</button>
</div>
</div>
</div>
</div>
</div>
</body>
</html>
<script>
var c = new Vue({
el:'.container',
data:{
myData:[
{name:"張三",age:20},
{name:"李四",age:20},
{name:"王五",age:20},
],
username:"",
age:"",
currentUser :-100,
},
methods : {
deleteuser :function(){
if (this.currentUser == 'all') {
this.myData = [];
}else{
this.myData.splice(this.currentUser,1);
}
},
add : function(){
if (this.username!=""&&this.age!=0) {
this.myData.push({
name:this.username,
age:this.age
})
}
},
}
})
</script>
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
vue中v-for循環(huán)選中點(diǎn)擊的元素并對該元素添加樣式操作
這篇文章主要介紹了vue中v-for循環(huán)選中點(diǎn)擊的元素并對該元素添加樣式操作,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-07-07
VUE3+Element-plus中el-form的使用示例代碼
這篇文章主要介紹了VUE3+Element-plus中el-form的使用示例代碼,本文通過圖文示例代碼相結(jié)合給大家介紹的非常詳細(xì),需要的朋友可以參考下2024-07-07
vue實(shí)現(xiàn)表單數(shù)據(jù)的增刪改功能
這篇文章主要為大家詳細(xì)介紹了vue實(shí)現(xiàn)表單數(shù)據(jù)的增刪改功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03
基于better-scroll 實(shí)現(xiàn)歌詞聯(lián)動(dòng)功能的代碼
BetterScroll 是一款重點(diǎn)解決移動(dòng)端(已支持 PC)各種滾動(dòng)場景需求的插件,BetterScroll 是使用純 JavaScript 實(shí)現(xiàn)的,這意味著它是無依賴的。本文基于better-scroll 實(shí)現(xiàn)歌詞聯(lián)動(dòng)功能,感興趣的朋友跟隨小編一起看看吧2020-05-05

