位置: IT常识 - 正文

Vue--》搭配Bootstrap实现Vue的列表增删功能(vue怎么用bootstrap)

编辑:rootadmin
Vue--》搭配Bootstrap实现Vue的列表增删功能

推荐整理分享Vue--》搭配Bootstrap实现Vue的列表增删功能(vue怎么用bootstrap),希望有所帮助,仅作参考,欢迎阅读内容。

文章相关热门搜索词:vue结合bootstrap,vue怎么用bootstrap,vue和bootstrap混用,vue-bootstrap,vue +bootstrap,vuecli bootstrap,vue-bootstrap,vue+bootstrap4,内容如对您有帮助,希望把文章链接给更多的朋友!

在日常开发中,我们可以用 “拿来主义” 借助Bootstarp现成的一些样式,快速生成我们想要的页面布局,避免书写大量的HTML和CSS代码,省下了许多不必要的时间。

当我们想要生成表单表格时我们可以查看Bootstrap的官方文档,来选择我们想要的样式,并根据自己的一些实际情况或者个人喜好进行一定的修改。了解Bootstrap

为了直接搭配Vue使用,我们把表单代码直接复制到 root 容器里面。

<div id="root"> <!-- 卡片区域 --> <div class="card"> <div class="card-header">添加水果</div> <div class="card-body"> <!-- 添加品牌的表单区域 --> <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">状态</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:'">删除</a> </td> </tr> </tbody></table>

表格结构写完之后,接下里我们就要使用Vue对表格进行填充数据了。

<script src="/Vue.js/vue.js"></script><script> Vue.config.productionTip = false; //阻止 vue 在启动时生成生产提示 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>Vue--》搭配Bootstrap实现Vue的列表增删功能(vue怎么用bootstrap)

接下里给删除操作绑定点击事件,如下:

给a链接绑定一个删除的点击事件。

我们使用filter进行过滤掉删除的数组,当前循环项的item.id不等于我们要删的id,就返回。

接下来我们实现水果的添加功能。

给输入框设置双向绑定事件,给表单设置提交事件并添加阻止事件。

定义用户输入的水果名称以及下一个可用的ID :

给绑定的add事件添加判断行为:

现在基本的添加删除功能已经完成,请看效果:

完整代码:

<!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"> <style> body { padding: 15px; } </style></head><body> <div id="root"> <!-- 卡片区域 --> <div class="card"> <div class="card-header">添加水果</div> <div class="card-body"> <!-- 添加品牌的表单区域 --> <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> <!-- 表格区域 --> <table class="table table-border table-hover table-striped"> <thead> <tr> <th scope="col">ID</th> <th scope="col">水果名称</th> <th scope="col">状态</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:'" @click="remove(item.id)">删除</a> </td> </tr> </tbody> </table> </div> <script src="/Vue.js/vue.js"></script> <script> Vue.config.productionTip = false; //阻止 vue 在启动时生成生产提示 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出去,应该执行添加逻辑 // 1.先把要添加的水果对象整理出来 const obj = { id: this.nextId, name:this.brand, status:true, time:new Date() } // 2.往this.list数组中push步骤一中得到的对象 this.list.push(obj) // 3.清空this.brand让this.nextID自增+1 // this.brand='' this.nextId++ }, } }) vm.$mount('#root') </script></body></html>
本文链接地址:https://www.jiuchutong.com/zhishi/298780.html 转载请保留说明!

上一篇:Ubuntu20.04安装OpenCV(ubuntu20.04安装opencv3.4)

下一篇:VMware虚拟机安装Ubuntu 2022最新版详细图文安装教程(VMware虚拟机安装+Ubuntu下载+VMware虚拟机配置运行)(vmware虚拟机安装在移动硬盘)

  • 纳税检查调整的滞纳金怎么收
  • 承租承包经营所得
  • 资产负债表里的存货包括哪些科目
  • 能从科目余额表入账吗
  • 新准则 开办费
  • 厂房推倒重建房产税
  • 结转库存商品会计分录
  • 企业所得税季度申报
  • 房产一般纳税人每个月报哪些税
  • 契税的发票开错了还可以作废吗?
  • 公司购买固定资产需要交印花税吗
  • 企业购进口汽车会计分录
  • 一般纳税人吧
  • 企业销售货物后,若发生销货退回或销售折让
  • 小规模纳税人核定标准
  • 地税收的其他政府基金计入科目及收益
  • 营改增金融业税收政策
  • 钢结构安装有哪些工种
  • 不是房屋产权人可以卖房吗
  • 交强险是不是只要有发票就可以报销
  • 固定资产清理账面价值怎么算
  • 个人到税局开具发票流程
  • 补贴收入营业外收入比例过高
  • 航天金穗怎么开票
  • 虚拟机怎么安装iso镜像文件
  • 其他现代服务业是什么
  • php常见面试问题
  • 上市公司发行股票会计分录
  • 在资本相对充足的情况下,为什么还要进一步引进外资
  • 如何安装iis网站服务器
  • php timestamp
  • laravel入门与实战
  • 小规模纳税人交增值税吗
  • 公允价值变动损益和投资收益区别
  • 数据库系统课程学什么
  • 简述php图像操作的基本步骤
  • 单目深度估计算法
  • python机器人编程控制
  • 所得税费用要结转损益吗
  • php获取参数值的三种方式
  • 广告公司的一般纳税人税率是多少
  • 怎样根据税负率计算税额
  • 企业代扣代缴个人所得税申报流程图
  • 存货盘亏处理报批
  • 调整以前年度少计收入
  • 金税四期上线后对个人的影响
  • 经营性应付项目包括哪些内容
  • mysql密码忘了怎么办?
  • 建筑企业结转收入方法
  • 国际快递运输服务有哪些
  • 应交增值税减免税额在借方
  • 公司对外投资企业与行政许可的区别是什么
  • 成品油经销企业资质
  • 影视公司临时演员怎么办
  • 异地预缴增值税多交了怎么办,可以退吗
  • 物业公司代收水费账务处理
  • 汽车贷款利息计算公式计算器
  • 设备的验证服务包括
  • 最新商业会计科目做账
  • 查看sqlserver操作记录
  • mysql5.5.62安装教程图解
  • 安装2个win10系统
  • mysql5.7解压版安装
  • u盘界面可以设置背景吗
  • 一切为了任务
  • freebsd怎么样
  • ubuntu configure
  • 电脑超频以后变得很卡
  • centos7.6有线连接
  • unity开发安卓游戏的input
  • jquery 字符串以什么开头
  • 如何用jquery
  • 复制到文件夹怎么弄
  • eclipse4.9.0安装windowbuilder
  • python记录运行状态的模块
  • 陕西省税务局机关服务中心
  • 云南税务网上税务局
  • ipadpro关税多少
  • 出口退税需要哪些
  • 车辆购置税怎么做账
  • 免责声明:网站部分图片文字素材来源于网络,如有侵权,请及时告知,我们会第一时间删除,谢谢! 邮箱:opceo@qq.com

    鄂ICP备2023003026号

    网站地图: 企业信息 工商信息 财税知识 网络常识 编程技术

    友情链接: 武汉网站建设