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