位置: 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虚拟机安装在移动硬盘)

  • 含13%的增值税怎么算
  • 应纳税所得额的会计分录
  • 会计凭证借贷方向
  • 季度企业所得税申报表怎么填写
  • 公司向公司借款可以无息吗
  • 所得税费用期末应转入
  • 明明申报了为什么显示没有申报
  • 职工食堂支出明细表
  • 保险金扣税吗
  • 由于汇率引起汇兑损益计入哪个科目?
  • 保险公司返点啥意思
  • 公司改变工资结构变相降工资怎么办
  • 房产开发公司土地增值税计算方法
  • 结余资金财政收回如何做账
  • 私营独资企业是公司还是非公司
  • 申报专项资金项目是什么
  • 有开发票就要交印花税吗?
  • 税务局锁了开票系统票已领出还可以开出来吗
  • 工资条上税基调整是啥意思
  • 发票总金额怎么算折扣
  • 企业筹建期发生的广告宣传费怎么抵减所得税
  • 派遣员工发生工伤这钱谁出
  • 一台设备发票开来0.5台怎么做账?
  • 公司收到保险公司退保费怎么账务处理
  • 权益净利率是什么意思啊
  • 其他综合收益包括留存收益吗
  • 公司试乘试驾车卖掉在增值税哪个模块里开具
  • 股权转让的条件和方式
  • 前端字符长度限制
  • 外籍人员个税免征吗
  • elements vue
  • 海关进口增值税怎么认证抵扣
  • yii框架教程
  • 境外机动车临时可以办理什么车险业务
  • 一次性扣除固定资产汇算清缴
  • 观景台俯视图
  • php调用其他php函数
  • vuedraggable官方文档
  • sklearn average precision
  • 长期挂账的其他应付款税务风险
  • 卖固定资产计入
  • PostgreSQL中常用的时间日期脚本使用教程
  • 深入理解计算机系统
  • sqlserver使用awe分配内存
  • 土石方费用入什么科目
  • 进项税额的作用
  • sqlserver控制台
  • 应收账款收不回来怎么处理分录
  • 解除劳动合同的合法程序
  • 残值收入交税吗
  • 失控发票已补税及滞纳金后还有事吗
  • 资产负债率怎么调整到50%以下
  • 新会计准则捐赠收入
  • mysql缩进快捷键
  • mysql和mysql数据库的区别
  • centosgui
  • Win7旗舰版系统文件名称
  • solaris8+apache2+weblogic813+db2_82客户端+128 安装过程
  • ubuntu rar压缩
  • win7系统如何删除隐藏文件
  • centos6基础命令
  • smss.exe是干嘛的
  • centos7 本地yum
  • 导演都有什么分类
  • angularjs表格控件
  • node.js中module.exports与exports用法上的区别
  • opengl入门教程
  • oracle shell脚本
  • 分享一下什么
  • js控制display属性
  • jquery的设计模式
  • javascript面向对象吗
  • 广东国家税务局电话
  • 税务局有哪些职务名称
  • 就业失业登记证网上申请
  • 正外部性与负外部性的定义
  • 车辆购置税完税证明有用吗
  • 苏州吴江区事业单位2023成绩公布
  • 江苏发票认证平台app
  • 专用发票章盖在哪里
  • 免责声明:网站部分图片文字素材来源于网络,如有侵权,请及时告知,我们会第一时间删除,谢谢! 邮箱:opceo@qq.com

    鄂ICP备2023003026号

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

    友情链接: 武汉网站建设