最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

使用Bootstrap + Vue.js實(shí)現(xiàn)表格的動(dòng)態(tài)展示、新增和刪除功能

 更新時(shí)間:2017年11月27日 09:42:45   作者:louie孫  
這篇文章主要介紹了使用Bootstrap + Vue.js實(shí)現(xiàn)表格的動(dòng)態(tài)展示、新增和刪除功能,需要的朋友可以參考下

一、寫在前面

1. Bootstrap是一個(gè)由 Twitter 開發(fā)和維護(hù)的前端框架,目前很受歡迎,Bootstrap中文網(wǎng)點(diǎn)擊這里。

2. Vue.js 是一套構(gòu)建用戶界面的漸進(jìn)式框架,點(diǎn)這里訪問官網(wǎng)

二、實(shí)現(xiàn)效果:

三、頁面引入bootstrap、vue資源

<link rel="stylesheet"  rel="external nofollow" >
<link rel="stylesheet"  rel="external nofollow" >
<script src="http://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script>
<script src="http://cdn.bootcss.com/popper.js/1.12.5/umd/popper.min.js"></script>
<script src="http://cdn.bootcss.com/bootstrap/4.0.0-beta/js/bootstrap.min.js"></script>
<script src="http://cdn.bootcss.com/vue/2.5.8/vue.min.js"></script>

這里需要注意的是,Boostrap依賴于JQuery,必須在引入Boostrap之前引入JQuery。

四、繪制表格

1.工具欄區(qū)

<div class="row mx-auto w-75">
 <div class="col-6">
  <div class="btn-group">
  <button type="button" class="btn btn-outline-info btn-sm" data-toggle="modal" data-target="#myModal">新增</button>
  <button type="button" class="btn btn-outline-primary btn-sm" @click="saveRows">保存</button>
  </div>
  <button type="button" class="btn btn-outline-warning btn-sm" @click="delRows">刪除</button>
 </div>
 <div class="col-6">
  <div class="input-group">
  <input type="text" class="form-control input-group-sm" placeholder="輸入設(shè)備編號(hào)進(jìn)行搜索">
  <span class="input-group-btn">
   <button class="btn btn-default" type="button"><i class="fa fa-search"></i></button>
   </span>
  </div>
 </div>
 </div>

2.表格區(qū)

<div class="row mx-auto w-75">
 <div class="col-12">
  <table class="table table-hover table-success">
  <thead class="thead-default">
  <tr>
   <th><input type="checkbox"></th>
   <th>序號(hào)</th>
   <th>設(shè)備編號(hào)</th>
   <th>設(shè)備名稱</th>
   <th>設(shè)備狀態(tài)</th>
   <th>采購日期</th>
   <th>設(shè)備管理員</th>
  </tr>
  </thead>
  <tbody>
  <tr v-for="(facility,index) in facilities">
   <td><input type="checkbox" :value="index" v-model="checkedRows"></td>
   <td>{{index+1}}</td>
   <td>{{facility.code}}</td>
   <td>{{facility.name}}</td>
   <td>{{facility.states}}</td>
   <td>{{facility.date}}</td>
   <td>{{facility.admin}}</td>
  </tr>
  </tbody>
  </table>
 </div>
 </div>

這里需要說明的是:

1.表格table的class Bootstrap3和Boostrap4有所不同;

2. vue.js for循環(huán),vue1與vue2有所出入,尤其是下標(biāo)index的使用。

以上兩點(diǎn)我們在使用中需要根據(jù)自己的版本做相應(yīng)調(diào)整了。

至此,展示表格數(shù)據(jù)的靜態(tài)頁面已經(jīng)完成,接下來我們使用Vue.js使表格數(shù)據(jù)成為動(dòng)態(tài)的。

五、 創(chuàng)建VUE對(duì)象、初始化表格數(shù)據(jù)

1.初始化數(shù)據(jù)

var datas = [
 {
  code: "A2017-001",
  name: "3800充電器",
  states: "正常",
  date: "2017-01-21",
  admin: "andy"
 },
 {
  code: "A2017-002",
  name: "Lenovo Type-c轉(zhuǎn)接器",
  states: "正常",
  date: "2017-01-21",
  admin: "zero"
 }];

Tips: datas在實(shí)際的場景中應(yīng)當(dāng)是通過ajax的方式訪問后臺(tái)獲取的業(yè)務(wù)數(shù)據(jù)。

2.創(chuàng)建vue對(duì)象

new Vue({
 el: "#vueApp",
 data: {
  checkAll: false,// 是否全選
  checkedRows: [],// 選中的行標(biāo),用于刪除行
  facilities: datas,// 表格數(shù)據(jù)
  newRow:{}// 新增的行數(shù)據(jù),用于新增行
 }
 })

ok,我們已經(jīng)完成了表格數(shù)據(jù)的動(dòng)態(tài)展示,下面我們來實(shí)現(xiàn)刪除行數(shù)據(jù)功能。

六、刪除行

刪除按鈕:

<button type="button" class="btn btn-outline-warning btn-sm" @click="delRows">刪除</button>

實(shí)現(xiàn)刪除功能:

delRows:function () {
  if (this.checkedRows.length <= 0){//是否選中判斷
   alert("您未選擇需要?jiǎng)h除的數(shù)據(jù)");
   return false;
  }
  if (!confirm("您確定要?jiǎng)h除選擇的數(shù)據(jù)嗎?")){//刪除確認(rèn)
   return false;
  }

  for(var i=0;i<this.checkedRows.length;i++){//循環(huán)獲取選中的行標(biāo)
   var checkedRowIndex = this.checkedRows[i];
   /**根據(jù)下標(biāo)移除數(shù)組元素*/
   this.facilities = $.grep(this.facilities,function (facility,j) {
   return j != checkedRowIndex;
   });
  }
  this.checkedRows = [];//清空選中行數(shù)據(jù)
  }

實(shí)現(xiàn)效果:

七、新增行

1.新增按鈕

<button type="button" class="btn btn-outline-info btn-sm" data-toggle="modal" data-target="#myModal">新增</button>

2.添加模態(tài)框用于錄入新增數(shù)據(jù)

<div class="modal fade" id="myModal">
 <div class="modal-dialog">
  <div class="modal-content">
  <div class="modal-header">
   <h4 class="modal-title">新增設(shè)備信息</h4>
   <button type="button" class="close" data-dismiss="modal">×</button>
  </div>
  <div class="modal-body">
   <div class="row">
   <div class="col-3">設(shè)備編號(hào):</div>
   <div class="col-9">
    <input class="form-control" placeholder="設(shè)備編號(hào)" v-model="newRow.code">
   </div>
   </div>
   <div class="row">
   <div class="col-3">設(shè)備名稱:</div>
   <div class="col-9">
    <input class="form-control" placeholder="設(shè)備名稱" v-model="newRow.name">
   </div>
   </div>
   <div class="row">
   <div class="col-3">設(shè)備狀態(tài):</div>
   <div class="col-9">
    <input class="form-control" placeholder="設(shè)備狀態(tài)" v-model="newRow.states">
   </div>
   </div>
   <div class="row">
   <div class="col-3">采購日期:</div>
   <div class="col-9">
    <input class="form-control" placeholder="采購日期" v-model="newRow.date">
   </div>
   </div>
   <div class="row">
   <div class="col-3">管理員:</div>
   <div class="col-9">
    <input class="form-control" placeholder="管理員" v-model="newRow.admin">
   </div>
   </div>
  </div>
  <div class="modal-footer">
   <button type="button" class="btn btn-outline-primary" data-dismiss="modal" @click="addRow">確認(rèn)</button>
  </div>
  </div>
 </div>
 </div>

3.實(shí)現(xiàn)新增邏輯

addRow: function () {
  this.facilities.push(this.newRow);//新行數(shù)據(jù)追加至表格數(shù)據(jù)數(shù)組中
  this.newRow = {};//新行數(shù)據(jù)置空
  }

好了,動(dòng)態(tài)展示、新增和刪除功能就講完了,后邊有空我們再來討論頁面上未實(shí)現(xiàn)的全選、快速檢索等功能。

附1:完整js

<script>
 var datas = [
 {
  code: "A2017-001",
  name: "3800充電器",
  states: "正常",
  date: "2017-01-21",
  admin: "andy"
 },
 {
  code: "A2017-002",
  name: "Lenovo Type-c轉(zhuǎn)接器",
  states: "正常",
  date: "2017-01-21",
  admin: "zero"
 }];
 new Vue({
 el: "#vueApp",
 data: {
  checkAll: false,
  checkedRows: [],
  facilities: datas,
  newRow:{}
 },
 methods: {
  addRow: function () {
  this.facilities.push(this.newRow);
  this.newRow = {};
  },
  saveRows:function () {//保存表格數(shù)據(jù)
  },
  delRows:function () {
  if (this.checkedRows.length <= 0){
   alert("您未選擇需要?jiǎng)h除的數(shù)據(jù)");
   return false;
  }
  if (!confirm("您確定要?jiǎng)h除選擇的數(shù)據(jù)嗎?")){
   return false;
  }
  for(var i=0;i<this.checkedRows.length;i++){
   var checkedRowIndex = this.checkedRows[i];
   this.facilities = $.grep(this.facilities,function (facility,j) {
   return j != checkedRowIndex;
   });
  }
  this.checkedRows = [];
  }
 }
 });
</script>

頁面源碼已共享至GitHub, 點(diǎn)擊這里 可查看下載,歡迎探討。

總結(jié)

以上所述是小編給大家介紹的使用Bootstrap + Vue.js實(shí)現(xiàn)表格的動(dòng)態(tài)展示、新增和刪除功能,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!

相關(guān)文章

  • vue之keepAlive使用案例詳解

    vue之keepAlive使用案例詳解

    這篇文章主要介紹了vue之keepAlive使用案例詳解,本篇文章通過簡要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下
    2021-09-09
  • vuejs實(shí)現(xiàn)折疊面板展開收縮動(dòng)畫效果

    vuejs實(shí)現(xiàn)折疊面板展開收縮動(dòng)畫效果

    這篇文章主要介紹了vuejs實(shí)現(xiàn)折疊面板展開收縮動(dòng)畫效果,文中通過代碼給大家分享兩種情況介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2018-09-09
  • element-ui table行點(diǎn)擊獲取行索引(index)并利用索引更換行順序

    element-ui table行點(diǎn)擊獲取行索引(index)并利用索引更換行順序

    這篇文章主要介紹了element-ui table行點(diǎn)擊獲取行索引(index)并利用索引更換行順序,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-02-02
  • 如何提升vue.js中大型數(shù)據(jù)的性能

    如何提升vue.js中大型數(shù)據(jù)的性能

    這篇文章主要介紹了提高vue.js中大型數(shù)據(jù)的性能,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,,需要的朋友可以參考下
    2019-06-06
  • Vue中動(dòng)態(tài)權(quán)限到按鈕的完整實(shí)現(xiàn)方案詳解

    Vue中動(dòng)態(tài)權(quán)限到按鈕的完整實(shí)現(xiàn)方案詳解

    這篇文章主要為大家詳細(xì)介紹了Vue如何在現(xiàn)有方案的基礎(chǔ)上加入對(duì)路由的增、刪、改、查權(quán)限控制,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2025-03-03
  • 基于Vue實(shí)現(xiàn)tab欄切換內(nèi)容不斷實(shí)時(shí)刷新數(shù)據(jù)功能

    基于Vue實(shí)現(xiàn)tab欄切換內(nèi)容不斷實(shí)時(shí)刷新數(shù)據(jù)功能

    在項(xiàng)目開發(fā)中遇到這樣需求,就是有幾個(gè)tab欄,每個(gè)tab欄對(duì)應(yīng)的ajax請(qǐng)求不一樣,內(nèi)容區(qū)域一樣,內(nèi)容為實(shí)時(shí)刷新數(shù)據(jù),實(shí)現(xiàn)方法其實(shí)很簡單的,下面小編給大家?guī)砹嘶赩ue實(shí)現(xiàn)tab欄切換內(nèi)容不斷實(shí)時(shí)刷新數(shù)據(jù)功能,需要的朋友參考下吧
    2017-04-04
  • vue開發(fā)調(diào)試神器vue-devtools使用詳解

    vue開發(fā)調(diào)試神器vue-devtools使用詳解

    這篇文章主要為大家詳細(xì)介紹了vue開發(fā)調(diào)試神器vue-devtools的使用方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-07-07
  • Vue實(shí)現(xiàn)Dialog封裝

    Vue實(shí)現(xiàn)Dialog封裝

    在寫業(yè)務(wù)的時(shí)候很常見的一個(gè)場景就是需要在不同的頁面調(diào)用同一個(gè)表單,常用的交互就是把表單以彈窗的形式展示,本文主要介紹了Vue實(shí)現(xiàn)Dialog封裝,感興趣的可以了解一下
    2021-07-07
  • npm打包失敗排查的全過程

    npm打包失敗排查的全過程

    使用npm報(bào)了很多錯(cuò),做的事情就是把錯(cuò)誤復(fù)制到百度上去搜索,看看哪個(gè)解決方案有效,下面這篇文章主要給大家介紹了關(guān)于npm打包失敗排查的全過程,需要的朋友可以參考下
    2022-11-11
  • vue中使用WX-JSSDK的兩種方法(推薦)

    vue中使用WX-JSSDK的兩種方法(推薦)

    這篇文章主要介紹了vue中使用WX-JSSDK的兩種方法,每種方法通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-01-01

最新評(píng)論

辉南县| 阳曲县| 石柱| 嘉祥县| 博罗县| 文安县| 本溪市| 荆门市| 浦东新区| 象山县| 牡丹江市| 镇赉县| 读书| 北辰区| 平塘县| 马龙县| 绿春县| 双流县| 七台河市| 镇雄县| 普定县| 长乐市| 侯马市| 河池市| 安丘市| 泰顺县| 雅江县| 彭州市| 巴彦县| 台南市| 蕉岭县| 花莲县| 揭东县| 柯坪县| 卫辉市| 瑞丽市| 根河市| 泗水县| 岳阳市| 惠水县| 元谋县|