欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

vue實現(xiàn)留言板todolist功能

 更新時間:2017年08月16日 09:45:06   投稿:lijiao  
這篇文章主要介紹了vue實現(xiàn)留言板todolist功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下

通過前面兩篇文章的的學習,我們掌握了vue的基本用法. 本文,就利用這些基礎知識來實現(xiàn)一個留言板, 老外把他稱之為todolist.

第一步、使用bootstrap做好布局

<!DOCTYPE html>
<html>
<head lang="en">
  <meta charset="UTF-8">
  <title></title>
  <link rel="stylesheet" href="lib/bootstrap.min.css"/>
  <script src="lib/jquery-1.7.2.js"></script>
  <script src="lib/bootstrap.js"></script>
</head>
<body>
<div class="container">
  <form role="form">
    <div class="form-group">
      <label for="username">用戶名:</label>
      <input type="text" id="username" class="form-control" placeholder="輸入用戶名">
    </div>
    <div class="form-group">
      <label for="age">年 齡:</label>
      <input type="text" id="age" class="form-control" placeholder="輸入年齡">
    </div>
    <div class="form-group">
      <input type="button" value="添加" class="btn btn-primary">
      <input type="button" value="重置" class="btn btn-danger">
    </div>
  </form>
  <hr>
  <table class="table table-bordered table-hover">
    <caption class="h2 text-info">用戶信息表</caption>
    <tr class="text-danger">
      <th class="text-center">序號</th>
      <th class="text-center">名字</th>
      <th class="text-center">年齡</th>
      <th class="text-center">操作</th>
    </tr>
    <tr class="text-center">
      <td>1</td>
      <td>張三</td>
      <td>20</td>
      <td>
        <button class="btn btn-primary btn-sm">刪除</button>
      </td>
    </tr>
    <tr class="text-center">
      <td>2</td>
      <td>李四</td>
      <td>22</td>
      <td>
        <button class="btn btn-primary btn-sm">刪除</button>
      </td>
    </tr>
    <tr>
      <td colspan="4" class="text-right">
        <button class="btn btn-danger btn-sm">刪除全部</button>
      </td>
    </tr>
    <tr>
      <td colspan="4" class="text-center text-muted">
        <p>暫無數(shù)據(jù)....</p>
      </td>
    </tr>
  </table>
</div>
</body>
</html>

第二步、增加模態(tài)框,模態(tài)框默認為隱藏的

<!DOCTYPE html>
<html>
<head lang="en">
  <meta charset="UTF-8">
  <title></title>
  <link rel="stylesheet" href="lib/bootstrap.min.css"/>
  <script src="lib/jquery-1.7.2.js"></script>
  <script src="lib/bootstrap.js"></script>
</head>
<body>
<div class="container">
  <form role="form">
    <div class="form-group">
      <label for="username">用戶名:</label>
      <input type="text" id="username" class="form-control" placeholder="輸入用戶名">
    </div>
    <div class="form-group">
      <label for="age">年 齡:</label>
      <input type="text" id="age" class="form-control" placeholder="輸入年齡">
    </div>
    <div class="form-group">
      <input type="button" value="添加" class="btn btn-primary">
      <input type="button" value="重置" class="btn btn-danger">
    </div>
  </form>
  <hr>
  <table class="table table-bordered table-hover">
    <caption class="h2 text-info">用戶信息表</caption>
    <tr class="text-danger">
      <th class="text-center">序號</th>
      <th class="text-center">名字</th>
      <th class="text-center">年齡</th>
      <th class="text-center">操作</th>
    </tr>
    <tr class="text-center">
      <td>1</td>
      <td>張三</td>
      <td>20</td>
      <td>
        <button class="btn btn-primary btn-sm" data-toggle="modal" data-target="#layer">刪除</button>
      </td>
    </tr>
    <tr class="text-center">
      <td>2</td>
      <td>李四</td>
      <td>22</td>
      <td>
        <button class="btn btn-primary btn-sm" data-toggle="modal" data-target="#layer">刪除</button>
      </td>
    </tr>
    <tr>
      <td colspan="4" class="text-right">
        <button class="btn btn-danger btn-sm">刪除全部</button>
      </td>
    </tr>
    <tr>
      <td colspan="4" class="text-center text-muted">
        <p>暫無數(shù)據(jù)....</p>
      </td>
    </tr>
  </table>

  <!--模態(tài)框 彈出框-->
  <div role="dialog" class="modal fade bs-example-modal-sm" id="layer">
    <div class="modal-dialog">
      <div class="modal-content">
        <div class="modal-header">
          <button type="button" class="close" data-dismiss="modal">
            <span>&times;</span>
          </button>
          <h4 class="modal-title">確認刪除么?</h4>
        </div>
        <div class="modal-body text-right">
          <button data-dismiss="modal" class="btn btn-primary btn-sm">取消</button>
          <button data-dismiss="modal" class="btn btn-danger btn-sm">確認</button>
        </div>
      </div>
    </div>
  </div>


</div>
</body>
</html>

第三步、定義userList用來保存用戶,userName保存用戶名, age保存用戶變量,  然后把userName和age 通過 v-model指令綁定到對應的輸入框,實現(xiàn)輸入框與變量中的數(shù)據(jù)雙向驅(qū)動,在表格的行中輸出userList

<!DOCTYPE html>
<html>
<head lang="en">
  <meta charset="UTF-8">
  <title></title>
  <link rel="stylesheet" href="lib/bootstrap.min.css"/>
  <script src="lib/jquery-1.7.2.js"></script>
  <script src="lib/bootstrap.js"></script>
  <script src="../js/vue.js"></script>
  <script>
    window.onload = function () {
      var c = new Vue({
        el: '#box',
        data: {
          userList: [],
          userName : '',
          age : ''
        }
      });
    }
  </script>
</head>
<body>
<div class="container" id="box">
  <form role="form">
    <div class="form-group">
      <label for="username">用戶名:</label>
      <input type="text" id="username" v-model="userName" class="form-control" placeholder="輸入用戶名">
    </div>
    <div class="form-group">
      <label for="age">年 齡:</label>
      <input type="text" id="age" v-model="age" class="form-control" placeholder="輸入年齡">
    </div>
    <div class="form-group">
      <input type="button" value="添加" class="btn btn-primary">
      <input type="button" value="重置" class="btn btn-danger">
    </div>
  </form>
  <hr>
  <table class="table table-bordered table-hover">
    <caption class="h2 text-info">用戶信息表</caption>
    <tr class="text-danger">
      <th class="text-center">序號</th>
      <th class="text-center">名字</th>
      <th class="text-center">年齡</th>
      <th class="text-center">操作</th>
    </tr>
    <tr class="text-center" v-for="value in userList">
      <td>{{$index+1}}</td>
      <td>{{value.name}}</td>
      <td>{{value.age}}</td>
      <td>
        <button class="btn btn-primary btn-sm" data-toggle="modal" data-target="#layer">刪除</button>
      </td>
    </tr>
    <tr>
      <td colspan="4" class="text-right">
        <button class="btn btn-danger btn-sm">刪除全部</button>
      </td>
    </tr>
    <tr>
      <td colspan="4" class="text-center text-muted">
        <p>暫無數(shù)據(jù)....</p>
      </td>
    </tr>
  </table>

  <!--模態(tài)框 彈出框-->
  <div role="dialog" class="modal fade bs-example-modal-sm" id="layer">
    <div class="modal-dialog">
      <div class="modal-content">
        <div class="modal-header">
          <button type="button" class="close" data-dismiss="modal">
            <span>&times;</span>
          </button>
          <h4 class="modal-title">確認刪除么?</h4>
        </div>
        <div class="modal-body text-right">
          <button data-dismiss="modal" class="btn btn-primary btn-sm">取消</button>
          <button data-dismiss="modal" class="btn btn-danger btn-sm">確認</button>
        </div>
      </div>
    </div>
  </div>
</div>
</body>
</html>

第四步、添加用戶,點擊添加按鈕,把輸入框中的數(shù)據(jù)作為一個對象 push 到數(shù)組userList,添加完之后,把userName, age清空,那么兩個輸入框的內(nèi)容就會被清空

<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
<link rel="stylesheet" href="lib/bootstrap.min.css"/>
<script src="lib/jquery-1.7.2.js"></script>
<script src="lib/bootstrap.js"></script>
<script src="../js/vue.js"></script>
<script>
window.onload = function () {
var c = new Vue({
el: '#box',
data: {
userList: [],
userName : '',
age : ''
}
});
}
</script>
</head>
<body>
<div class="container" id="box">
<form role="form">
<div class="form-group">
<label for="username">用戶名:</label>
<input type="text" id="username" v-model="userName" class="form-control" placeholder="輸入用戶名">
</div>
<div class="form-group">
<label for="age">年 齡:</label>
<input type="text" id="age" v-model="age" class="form-control" placeholder="輸入年齡">
</div>
<div class="form-group">
<input type="button" value="添加" class="btn btn-primary">
<input type="button" value="重置" class="btn btn-danger">
</div>
</form>
<hr>
<table class="table table-bordered table-hover">
<caption class="h2 text-info">用戶信息表</caption>
<tr class="text-danger">
<th class="text-center">序號</th>
<th class="text-center">名字</th>
<th class="text-center">年齡</th>
<th class="text-center">操作</th>
</tr>
<tr class="text-center" v-for="value in userList">
<td>{{$index+1}}</td>
<td>{{value.name}}</td>
<td>{{value.age}}</td>
<td>
<button class="btn btn-primary btn-sm" data-toggle="modal" data-target="#layer">刪除</button>
</td>
</tr>
<tr>
<td colspan="4" class="text-right">
<button class="btn btn-danger btn-sm">刪除全部</button>
</td>
</tr>
<tr>
<td colspan="4" class="text-center text-muted">
<p>暫無數(shù)據(jù)....</p>
</td>
</tr>
</table>

<!--模態(tài)框 彈出框-->
<div role="dialog" class="modal fade bs-example-modal-sm" id="layer">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">
<span>&times;</span>
</button>
<h4 class="modal-title">確認刪除么?</h4>
</div>
<div class="modal-body text-right">
<button data-dismiss="modal" class="btn btn-primary btn-sm">取消</button>
<button data-dismiss="modal" class="btn btn-danger btn-sm">確認</button>
</div>
</div>
</div>
</div>
</div>
</body>
</html>

第五步、結合數(shù)組的長度與v-show指令,實現(xiàn)提示信息的顯示與隱藏

<!DOCTYPE html>
<html>
<head lang="en">
  <meta charset="UTF-8">
  <title></title>
  <link rel="stylesheet" href="lib/bootstrap.min.css"/>
  <script src="lib/jquery-1.7.2.js"></script>
  <script src="lib/bootstrap.js"></script>
  <script src="../js/vue.js"></script>
  <script>
    window.onload = function () {
      var c = new Vue({
        el: '#box',
        data: {
          userList: [],
          userName : '',
          age : ''
        },
        methods : {
          addUser : function(){
            this.userList.push({
              name : this.userName,
              age : this.age
            });

            this.userName = ''; //添加完用戶之后,把輸入框的值清除
            this.age = '';
          }
        }
      });
    }
  </script>
</head>
<body>
<div class="container" id="box">
  <form role="form">
    <div class="form-group">
      <label for="username">用戶名:</label>
      <input type="text" id="username" v-model="userName" class="form-control" placeholder="輸入用戶名">
    </div>
    <div class="form-group">
      <label for="age">年 齡:</label>
      <input type="text" id="age" v-model="age" class="form-control" placeholder="輸入年齡">
    </div>
    <div class="form-group">
      <input type="button" v-on:click="addUser();" value="添加" class="btn btn-primary">
      <input type="button" value="重置" class="btn btn-danger">
    </div>
  </form>
  <hr>
  <table class="table table-bordered table-hover">
    <caption class="h2 text-info">用戶信息表</caption>
    <tr class="text-danger">
      <th class="text-center">序號</th>
      <th class="text-center">名字</th>
      <th class="text-center">年齡</th>
      <th class="text-center">操作</th>
    </tr>
    <tr class="text-center" v-for="value in userList">
      <td>{{$index+1}}</td>
      <td>{{value.name}}</td>
      <td>{{value.age}}</td>
      <td>
        <button class="btn btn-primary btn-sm" data-toggle="modal" data-target="#layer">刪除</button>
      </td>
    </tr>
    <tr v-show="userList.length!=0">
      <td colspan="4" class="text-right">
        <button class="btn btn-danger btn-sm">刪除全部</button>
      </td>
    </tr>
    <tr v-show="userList.length==0">
      <td colspan="4" class="text-center text-muted">
        <p>暫無數(shù)據(jù)....</p>
      </td>
    </tr>
  </table>

  <!--模態(tài)框 彈出框-->
  <div role="dialog" class="modal fade bs-example-modal-sm" id="layer">
    <div class="modal-dialog">
      <div class="modal-content">
        <div class="modal-header">
          <button type="button" class="close" data-dismiss="modal">
            <span>&times;</span>
          </button>
          <h4 class="modal-title">確認刪除么?</h4>
        </div>
        <div class="modal-body text-right">
          <button data-dismiss="modal" class="btn btn-primary btn-sm">取消</button>
          <button data-dismiss="modal" class="btn btn-danger btn-sm">確認</button>
        </div>
      </div>
    </div>
  </div>
</div>
</body>
</html>

第六步、實現(xiàn)刪除某行數(shù)據(jù)

<!DOCTYPE html>
<html>
<head lang="en">
  <meta charset="UTF-8">
  <title></title>
  <link rel="stylesheet" href="lib/bootstrap.min.css"/>
  <script src="lib/jquery-1.7.2.js"></script>
  <script src="lib/bootstrap.js"></script>
  <script src="../js/vue.js"></script>
  <script>
    window.onload = function () {
      var c = new Vue({
        el: '#box',
        data: {
          userList: [],
          userName : '',
          age : '',
          curIndex : -10
        },
        methods : {
          addUser : function(){
            this.userList.push({
              name : this.userName,
              age : this.age
            });

            this.userName = ''; //添加完用戶之后,把輸入框的值清除
            this.age = '';
          },
          deleteRow : function( n ){
            this.userList.splice( n, 1 );
          }
        }
      });
    }
  </script>
</head>
<body>
<div class="container" id="box">
  <form role="form">
    <div class="form-group">
      <label for="username">用戶名:</label>
      <input type="text" id="username" v-model="userName" class="form-control" placeholder="輸入用戶名">
    </div>
    <div class="form-group">
      <label for="age">年 齡:</label>
      <input type="text" id="age" v-model="age" class="form-control" placeholder="輸入年齡">
    </div>
    <div class="form-group">
      <input type="button" v-on:click="addUser();" value="添加" class="btn btn-primary">
      <input type="button" value="重置" class="btn btn-danger">
    </div>
  </form>
  <hr>
  <table class="table table-bordered table-hover">
    <caption class="h2 text-info">用戶信息表</caption>
    <tr class="text-danger">
      <th class="text-center">序號</th>
      <th class="text-center">名字</th>
      <th class="text-center">年齡</th>
      <th class="text-center">操作</th>
    </tr>
    <tr class="text-center" v-for="value in userList">
      <td>{{$index+1}}</td>
      <td>{{value.name}}</td>
      <td>{{value.age}}</td>
      <td>
        <button class="btn btn-primary btn-sm" data-toggle="modal" data-target="#layer" v-on:click="curIndex=$index">刪除</button>
      </td>
    </tr>
    <tr v-show="userList.length!=0">
      <td colspan="4" class="text-right">
        <button class="btn btn-danger btn-sm">刪除全部</button>
      </td>
    </tr>
    <tr v-show="userList.length==0">
      <td colspan="4" class="text-center text-muted">
        <p>暫無數(shù)據(jù)....</p>
      </td>
    </tr>
  </table>

  <!--模態(tài)框 彈出框-->
  <div role="dialog" class="modal fade bs-example-modal-sm" id="layer">
    <div class="modal-dialog">
      <div class="modal-content">
        <div class="modal-header">
          <button type="button" class="close" data-dismiss="modal">
            <span>&times;</span>
          </button>
          <h4 class="modal-title">確認刪除么?</h4>
        </div>
        <div class="modal-body text-right">
          <button data-dismiss="modal" class="btn btn-primary btn-sm">取消</button>
          <button data-dismiss="modal" class="btn btn-danger btn-sm" v-on:click="deleteRow(curIndex);">確認</button>
        </div>
      </div>
    </div>
  </div>
</div>
</body>
</html>

第七步、實現(xiàn)刪除全部行

<!DOCTYPE html>
<html>
<head lang="en">
  <meta charset="UTF-8">
  <title></title>
  <link rel="stylesheet" href="lib/bootstrap.min.css"/>
  <script src="lib/jquery-1.7.2.js"></script>
  <script src="lib/bootstrap.js"></script>
  <script src="../js/vue.js"></script>
  <script>
    window.onload = function () {
      var c = new Vue({
        el: '#box',
        data: {
          userList: [],
          userName: '',
          age: '',
          curIndex: -10
        },
        methods: {
          addUser: function () {
            this.userList.push({
              name: this.userName,
              age: this.age
            });

            this.userName = ''; //添加完用戶之后,把輸入框的值清除
            this.age = '';
          },
          deleteRow: function (n) {
            if (n == -1) { //當n=-1的時候,清空數(shù)組,就是刪除全部
              this.userList = [];
            } else {
              this.userList.splice(n, 1);
            }
          }
        }
      });
    }
  </script>
</head>
<body>
<div class="container" id="box">
  <form role="form">
    <div class="form-group">
      <label for="username">用戶名:</label>
      <input type="text" id="username" v-model="userName" class="form-control" placeholder="輸入用戶名">
    </div>
    <div class="form-group">
      <label for="age">年 齡:</label>
      <input type="text" id="age" v-model="age" class="form-control" placeholder="輸入年齡">
    </div>
    <div class="form-group">
      <input type="button" v-on:click="addUser();" value="添加" class="btn btn-primary">
      <input type="button" value="重置" class="btn btn-danger">
    </div>
  </form>
  <hr>
  <table class="table table-bordered table-hover">
    <caption class="h2 text-info">用戶信息表</caption>
    <tr class="text-danger">
      <th class="text-center">序號</th>
      <th class="text-center">名字</th>
      <th class="text-center">年齡</th>
      <th class="text-center">操作</th>
    </tr>
    <tr class="text-center" v-for="value in userList">
      <td>{{$index+1}}</td>
      <td>{{value.name}}</td>
      <td>{{value.age}}</td>
      <td>
        <button class="btn btn-primary btn-sm" data-toggle="modal" data-target="#layer"
            v-on:click="curIndex=$index">刪除
        </button>
      </td>
    </tr>
    <tr v-show="userList.length!=0">
      <td colspan="4" class="text-right">
        <button class="btn btn-danger btn-sm" v-on:click="curIndex=-1" data-toggle="modal" data-target="#layer">
          刪除全部
        </button>
      </td>
    </tr>
    <tr v-show="userList.length==0">
      <td colspan="4" class="text-center text-muted">
        <p>暫無數(shù)據(jù)....</p>
      </td>
    </tr>
  </table>

  <!--模態(tài)框 彈出框-->
  <div role="dialog" class="modal fade bs-example-modal-sm" id="layer">
    <div class="modal-dialog">
      <div class="modal-content">
        <div class="modal-header">
          <button type="button" class="close" data-dismiss="modal">
            <span>&times;</span>
          </button>
          <h4 class="modal-title">確認刪除么?</h4>
        </div>
        <div class="modal-body text-right">
          <button data-dismiss="modal" class="btn btn-primary btn-sm">取消</button>
          <button data-dismiss="modal" class="btn btn-danger btn-sm" v-on:click="deleteRow(curIndex);">確認
          </button>
        </div>
      </div>
    </div>
  </div>
</div>
</body>
</html>

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • vue3父組件和子組件如何傳值實例詳解

    vue3父組件和子組件如何傳值實例詳解

    近期學習vue3的父子組件之間的傳值,發(fā)現(xiàn)跟vue2.x的父子組件之間的傳值并沒有太大的區(qū)別,下面這篇文章主要給大家介紹了關于vue3父組件和子組件如何傳值的相關資料,需要的朋友可以參考下
    2022-08-08
  • axios進階實踐之利用最優(yōu)雅的方式寫ajax請求

    axios進階實踐之利用最優(yōu)雅的方式寫ajax請求

    之前給大家介紹了jQuery利用最優(yōu)雅的方式寫ajax請求的相關內(nèi)容,這篇文章主要給大家介紹了關于axios進階實踐之利用最優(yōu)雅的方式寫ajax請求的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面來一起看看吧。
    2017-12-12
  • 詳解Vue3常用的6種組件通信方式

    詳解Vue3常用的6種組件通信方式

    我們用Vue3開發(fā)項目時,常常需要面對的一個問題就是組件之間的通信,如何將數(shù)據(jù)發(fā)給對應的組件,這是不可避免的一個問題,該篇講述了Vue3常用的6種組件通信方式,需要的朋友可以參考下
    2024-09-09
  • vue+elementui 對話框取消 表單驗證重置示例

    vue+elementui 對話框取消 表單驗證重置示例

    今天小編就為大家分享一篇vue+elementui 對話框取消 表單驗證重置示例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-10-10
  • vue3通過組合鍵實現(xiàn)換行操作的示例詳解

    vue3通過組合鍵實現(xiàn)換行操作的示例詳解

    這篇文章主要為大家詳細介紹了vue3如何通過組合鍵,例如command+Enter、shift+Enter、alt + Enter,實現(xiàn)換行操作,感興趣的可以了解下
    2024-03-03
  • 深入理解Vue生命周期、手動掛載及掛載子組件

    深入理解Vue生命周期、手動掛載及掛載子組件

    本篇文章主要介紹了深入理解Vue生命周期和手動掛載,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-09-09
  • vue、react等單頁面項目部署到服務器的方法及vue和react的區(qū)別

    vue、react等單頁面項目部署到服務器的方法及vue和react的區(qū)別

    這篇文章主要介紹了vue、react等單頁面項目部署到服務器的方法,需要的朋友可以參考下
    2018-09-09
  • Vue中的Props(不可變狀態(tài))

    Vue中的Props(不可變狀態(tài))

    這篇文章主要介紹了Vue中的Props(不可變狀態(tài)),本文通過實例代碼給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下
    2018-09-09
  • 在 React、Vue項目中使用SVG的方法

    在 React、Vue項目中使用SVG的方法

    本篇文章主要介紹了在 React、Vue項目中使用SVG的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-02-02
  • vue路由守衛(wèi)+登錄態(tài)管理實例分析

    vue路由守衛(wèi)+登錄態(tài)管理實例分析

    這篇文章主要介紹了vue路由守衛(wèi)+登錄態(tài)管理,結合實例形式分析了vue路由守衛(wèi)與登錄態(tài)管理相關操作步驟與實現(xiàn)技巧,需要的朋友可以參考下
    2019-05-05

最新評論