Vue中搭配Bootstrap實(shí)現(xiàn)Vue的列表增刪功能
在日常開發(fā)中,我們可以用 “拿來主義” 借助Bootstarp現(xiàn)成的一些樣式,快速生成我們想要的頁面布局,避免書寫大量的HTML和CSS代碼,省下了許多不必要的時(shí)間。
當(dāng)我們想要生成表單表格時(shí)我們可以查看Bootstrap的官方文檔,來選擇我們想要的樣式,并根據(jù)自己的一些實(shí)際情況或者個人喜好進(jìn)行一定的修改。了解Bootstrap

為了直接搭配Vue使用,我們把表單代碼直接復(fù)制到 root 容器里面。
<div id="root">
<!-- 卡片區(qū)域 -->
<div class="card">
<div class="card-header">添加水果</div>
<div class="card-body">
<!-- 添加品牌的表單區(qū)域 -->
<form>
<div class="form-row align-items-center">
<div class="col-auto">
<div class="input-group mb-2">
<div class="input-group-prepend">
<div class="input-group-text">水果名稱</div>
</div>
<input type="text" class="form-control" placeholder="請輸入水果名字">
</div>
</div>
<div class="col-auto">
<button type="submit" class="btn btn-primary mb-2">添加</button>
</div>
</div>
</form>
</div>
</div>
</div>
這邊借助一下Bootstrap中的card(卡片)進(jìn)行布局,擴(kuò)充一下寬度。

接下來我們在借助Bootstrap生成一個表格部分:

<table class="table table-border table-hover table-striped">
<thead>
<tr>
<th scope="col">ID</th>
<th scope="col">水果名稱</th>
<th scope="col">狀態(tài)</th>
<th scope="col">添加時(shí)間</th>
<th scope="col">操作</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row">1</th>
<td>蘋果</td>
<td>
<div class="custom-control custom-switch">
<input type="checkbox" class="custom-control-input" id="customSwitch1">
<label class="custom-control-label" for="customSwitch1">已禁用</label>
</div>
</td>
<td>時(shí)間</td>
<td>
<a href="javascript:'" rel="external nofollow" rel="external nofollow" >刪除</a>
</td>
</tr>
</tbody>
</table>
表格結(jié)構(gòu)寫完之后,接下里我們就要使用Vue對表格進(jìn)行填充數(shù)據(jù)了。

<script src="/Vue.js/vue.js"></script>
<script>
Vue.config.productionTip = false; //阻止 vue 在啟動時(shí)生成生產(chǎn)提示
const vm = new Vue({
data: {
list: [
{ id: 1, name: '蘋果', status: true, time: new Date() },
{ id: 2, name: '香蕉', status: true, time: new Date() },
{ id: 3, name: '葡萄', status: false, time: new Date() },
{ id: 4, name: '桃子', status: true, time: new Date() },
]
}
})
vm.$mount('#root')
</script>
接下里給刪除操作綁定點(diǎn)擊事件,如下:
給a鏈接綁定一個刪除的點(diǎn)擊事件。

我們使用filter進(jìn)行過濾掉刪除的數(shù)組,當(dāng)前循環(huán)項(xiàng)的item.id不等于我們要刪的id,就返回。

接下來我們實(shí)現(xiàn)水果的添加功能。
給輸入框設(shè)置雙向綁定事件,給表單設(shè)置提交事件并添加阻止事件。

定義用戶輸入的水果名稱以及下一個可用的ID :

給綁定的add事件添加判斷行為:

現(xiàn)在基本的添加刪除功能已經(jīng)完成,請看效果:

完整代碼:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<link rel="stylesheet" href="/Bootstrap/bootstrap.css" rel="external nofollow" >
<style>
body {
padding: 15px;
}
</style>
</head>
<body>
<div id="root">
<!-- 卡片區(qū)域 -->
<div class="card">
<div class="card-header">添加水果</div>
<div class="card-body">
<!-- 添加品牌的表單區(qū)域 -->
<form @submit.prevent="add">
<div class="form-row align-items-center">
<div class="col-auto">
<div class="input-group mb-2">
<div class="input-group-prepend">
<div class="input-group-text">水果名稱</div>
</div>
<input type="text" class="form-control" placeholder="請輸入水果名字" v-model.trim="brand">
</div>
</div>
<div class="col-auto">
<button type="submit" class="btn btn-primary mb-2">添加</button>
</div>
</div>
</form>
</div>
</div>
<!-- 表格區(qū)域 -->
<table class="table table-border table-hover table-striped">
<thead>
<tr>
<th scope="col">ID</th>
<th scope="col">水果名稱</th>
<th scope="col">狀態(tài)</th>
<th scope="col">添加時(shí)間</th>
<th scope="col">操作</th>
</tr>
</thead>
<tbody>
<tr v-for="item in list" :key="item.id">
<th scope="row">{{item.id}}</th>
<td>{{item.name}}</td>
<td>
<div class="custom-control custom-switch">
<input type="checkbox" class="custom-control-input" :id="'customSwitch'+item.id"
v-model="item.status">
<label class="custom-control-label" :for="'customSwitch'+item.id"
v-if="item.status">已啟用</label>
<label class="custom-control-label" :for="'customSwitch'+item.id" v-else>已禁用</label>
</div>
</td>
<td>{{item.time}}</td>
<td>
<a href="javascript:'" rel="external nofollow" rel="external nofollow" @click="remove(item.id)">刪除</a>
</td>
</tr>
</tbody>
</table>
</div>
<script src="/Vue.js/vue.js"></script>
<script>
Vue.config.productionTip = false; //阻止 vue 在啟動時(shí)生成生產(chǎn)提示
const vm = new Vue({
data: {
// 用戶輸入的水果名稱
brand: '',
// nextID是下一個可用的 ID
nextId: 5,
list: [
{ id: 1, name: '蘋果', status: true, time: new Date() },
{ id: 2, name: '香蕉', status: true, time: new Date() },
{ id: 3, name: '葡萄', status: false, time: new Date() },
{ id: 4, name: '桃子', status: true, time: new Date() },
]
},
methods: {
// 點(diǎn)擊鏈接刪除對應(yīng)的水果
remove (id) {
this.list = this.list.filter(item => item.id !== id)
},
// 阻止表單的默認(rèn)提交行為
// 如果判斷到brand的值為空字符串,則return出去
add () {
if (this.brand === '') return alert('必須輸入水果')
// 如果沒有被return出去,應(yīng)該執(zhí)行添加邏輯
// 1.先把要添加的水果對象整理出來
const obj = {
id: this.nextId,
name:this.brand,
status:true,
time:new Date()
}
// 2.往this.list數(shù)組中push步驟一中得到的對象
this.list.push(obj)
// 3.清空this.brand讓this.nextID自增+1
// this.brand=''
this.nextId++
},
}
})
vm.$mount('#root')
</script>
</body>
</html>
到此這篇關(guān)于Vue中搭配Bootstrap實(shí)現(xiàn)Vue的列表增刪功能的文章就介紹到這了,更多相關(guān)vue bootstrap列表增刪內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue3父子組件傳值中props使用細(xì)節(jié)淺析
這篇文章主要給大家介紹了關(guān)于vue3父子組件傳值中props使用細(xì)節(jié)的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2022-03-03
vue中v-for和v-if不能在同一個標(biāo)簽使用的最新解決方案
這篇文章主要介紹了vue中v-for和v-if不能在同一個標(biāo)簽的最新解決方案,這里描述了兩種解決方案,結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2023-07-07
Vue3+Canvas實(shí)現(xiàn)坦克大戰(zhàn)游戲(二)
本文主要給大家講解一下子彈擊中物體、物體銷毀、敵方坦克構(gòu)建生成、運(yùn)動算法、爆炸效果、以及障礙物的生成,感興趣的小伙伴可以了解一下2022-03-03
vue-element-admin關(guān)閉eslint的校驗(yàn)方式
這篇文章主要介紹了vue-element-admin關(guān)閉eslint的校驗(yàn)方式,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-08-08
Vue編譯器源碼分析compileToFunctions作用詳解
這篇文章主要為大家介紹了Vue編譯器源碼分析compileToFunctions作用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-07-07
Vue屏幕自適應(yīng)三種實(shí)現(xiàn)方法詳解
在實(shí)際業(yè)務(wù)中,我們常用圖表來做數(shù)據(jù)統(tǒng)計(jì),數(shù)據(jù)展示,數(shù)據(jù)可視化等比較直觀的方式來達(dá)到一目了然的數(shù)據(jù)查看,但在大屏開發(fā)過程中,常會因?yàn)檫m配不同屏幕而感到困擾,下面我們來解決一下這個不算難題的難題2022-11-11

