Vue中搭配Bootstrap實(shí)現(xiàn)Vue的列表增刪功能
在日常開發(fā)中,我們可以用 “拿來主義” 借助Bootstarp現(xiàn)成的一些樣式,快速生成我們想要的頁面布局,避免書寫大量的HTML和CSS代碼,省下了許多不必要的時(shí)間。
當(dāng)我們想要生成表單表格時(shí)我們可以查看Bootstrap的官方文檔,來選擇我們想要的樣式,并根據(jù)自己的一些實(shí)際情況或者個(gè)人喜好進(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="請(qǐng)輸入水果名字"> </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ò)充一下寬度。
接下來我們?cè)诮柚鶥ootstrap生成一個(gè)表格部分:
<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對(duì)表格進(jìn)行填充數(shù)據(jù)了。
<script src="/Vue.js/vue.js"></script> <script> Vue.config.productionTip = false; //阻止 vue 在啟動(dòng)時(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鏈接綁定一個(gè)刪除的點(diǎn)擊事件。
我們使用filter進(jìn)行過濾掉刪除的數(shù)組,當(dāng)前循環(huán)項(xiàng)的item.id不等于我們要?jiǎng)h的id,就返回。
接下來我們實(shí)現(xiàn)水果的添加功能。
給輸入框設(shè)置雙向綁定事件,給表單設(shè)置提交事件并添加阻止事件。
定義用戶輸入的水果名稱以及下一個(gè)可用的ID :
給綁定的add事件添加判斷行為:
現(xiàn)在基本的添加刪除功能已經(jīng)完成,請(qǐ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="請(qǐng)輸入水果名字" 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 在啟動(dòng)時(shí)生成生產(chǎn)提示 const vm = new Vue({ data: { // 用戶輸入的水果名稱 brand: '', // nextID是下一個(gè)可用的 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)擊鏈接刪除對(duì)應(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.先把要添加的水果對(duì)象整理出來 const obj = { id: this.nextId, name:this.brand, status:true, time:new Date() } // 2.往this.list數(shù)組中push步驟一中得到的對(duì)象 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)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue3處理錯(cuò)誤邊界(error boundaries)的示例代碼
在開發(fā) Vue 3 應(yīng)用時(shí),處理錯(cuò)誤邊界(Error Boundaries)是一個(gè)重要的考量,在 Vue 3 中實(shí)現(xiàn)錯(cuò)誤邊界的方式與 React 等其他框架有所不同,下面,我們將深入探討 Vue 3 中如何實(shí)現(xiàn)錯(cuò)誤邊界,并提供一些示例代碼幫助理解什么是錯(cuò)誤邊界,需要的朋友可以參考下2024-10-10基于vue+face-api.js實(shí)現(xiàn)前端人臉識(shí)別功能
基于face-api.js要實(shí)現(xiàn)人臉識(shí)別功能,首先要將自己需要的模型文件下載保存在靜態(tài)目錄下,可以通過cdn的方式在index.html中引入face-api.js,本文給大家介紹vue+face-api.js實(shí)現(xiàn)前端人臉識(shí)別功能,感興趣的朋友一起看看吧2023-12-12vue中watch監(jiān)聽器用法之deep、immediate、flush
Vue是可以監(jiān)聽到多層級(jí)數(shù)據(jù)改變的,且可以在頁面上做出對(duì)應(yīng)展示,下面這篇文章主要給大家介紹了關(guān)于vue中watch監(jiān)聽器用法之deep、immediate、flush的相關(guān)資料,需要的朋友可以參考下2022-09-09vue單文件組件lint error自動(dòng)fix與styleLint報(bào)錯(cuò)自動(dòng)fix詳解
這篇文章主要給大家介紹了關(guān)于vue單文件組件lint error自動(dòng)fix與styleLint報(bào)錯(cuò)自動(dòng)fix的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-01-01Vue實(shí)現(xiàn)一種簡(jiǎn)單的無限循環(huán)滾動(dòng)動(dòng)畫的示例
這篇文章主要介紹了Vue實(shí)現(xiàn)一種簡(jiǎn)單的無限循環(huán)滾動(dòng)動(dòng)畫的示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01Vuex unknown action type報(bào)錯(cuò)問題及解決
這篇文章主要介紹了Vuex unknown action type報(bào)錯(cuò)問題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-02-02