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

  • 二手车没有发票能过户吗
  • 利润表的调整
  • 待抵扣进项税什么时候抵扣
  • 营改增对小规模的影响
  • 汇兑损益在外币业务核算中有什么重要意义
  • 专项维修资金的,由县级
  • 佣金付给别人公司违法吗
  • 补开去年未开票收入
  • 出口退税可以不申报是否需要缴纳增值税
  • 企业盈利后又亏损怎么算
  • 工作服清洗费要交个税那
  • 宽带合同属于什么合同
  • 移动电子发票怎么发送到邮箱
  • 营改增后建筑业怎么开票
  • 企业财务负责人和办税人哪个责任大
  • 进口货物退税流程
  • 增长率应该要如何计算呢?
  • 季报现金流量表怎么填
  • 经营方式变更说明
  • 车间低值易耗品有哪些
  • 经费开支原则是什么
  • 无形资产入股注意事项
  • iframe更改自身src
  • 联营企业分得的利润应计入
  • 最小的洗衣机是多少
  • 原 !神静态网页布局详解,html+css布局实战,附详细代码
  • 劳动保护经费
  • 布拉塞龙图片
  • 计提城建税是在当月提吗
  • 用人单位垫付生育津贴怎么算
  • before跟after区别
  • 使用spring框架,大概有哪些步骤
  • 社保在会计上的分类
  • 开了红字信息表当月怎么做账
  • 红字发票信息表填好后再怎么操作
  • 离职员工的个税忘记申报了补申报会被处罚吗
  • 不是自己生产的产品可以用自己的商标吗
  • 客户的赔偿金会计分录
  • 白酒贴牌酒是真酒还是假酒
  • SQL Report Builder 报表里面的常见问题分析
  • sql server 2008使用说明
  • 企业购买的理财产品 亏损了,是否可以税前扣除
  • 人力资源行业企业成长
  • 销售旧固定资产开票编码
  • 房产税土地使用税会计分录
  • 小规模纳税人记账报税流程
  • 专项应付款二级科目
  • 企业网银代发工资明细打印
  • 黄金以旧换新是不是不划算
  • 公司章程上的出资额怎么填
  • 未认证的进项税额转出会计分录
  • 合资公司政策
  • 利润表中财务费用中的利息费用和利息收入
  • 首先要知道什么英语
  • 进口固定资产的汇率怎么算
  • 审计真实性认定
  • mysql 5.7.17 winx64.zip安装配置方法图文教程
  • xp系统找不到指定的路径怎么办
  • Win7旗舰版系统文件名称
  • win2003自动关机
  • ubuntu16.04安装步骤
  • webinstall.exe - webinstall是什么进程
  • win8系统如何关闭杀毒系统
  • win10 禁用驱动
  • mac os 必装软件
  • js中date对象
  • javascript RegExp 使用说明
  • blockqueue生产者消费者
  • AndroidSharedpreference数据共享
  • python 控制台交互
  • jquery ajax分页插件的简单实现
  • unity ugui组件
  • js获取
  • 陕西省税务局电子税务局官网
  • 大连税务局王局长
  • 竣工交付的资产有哪几种
  • 车辆购置税查询不到应征欠税信息,不能进行扣款操作
  • 国家税务总局成都市武侯区税务局
  • 审计的起源和发展史
  • 认缴制什么时候开始的?
  • 免责声明:网站部分图片文字素材来源于网络,如有侵权,请及时告知,我们会第一时间删除,谢谢! 邮箱:opceo@qq.com

    鄂ICP备2023003026号

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

    友情链接: 武汉网站建设