您好,登錄后才能下訂單哦!
這篇文章將為大家詳細(xì)講解有關(guān)vue如何實現(xiàn)留言板todolist功能,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。
Vue是一款友好的、多用途且高性能的JavaScript框架,使用vue可以創(chuàng)建可維護(hù)性和可測試性更強的代碼庫,Vue允許可以將一個網(wǎng)頁分割成可復(fù)用的組件,每個組件都包含屬于自己的HTML、CSS、JavaScript,以用來渲染網(wǎng)頁中相應(yīng)的地方,所以越來越多的前端開發(fā)者使用vue。
第一步、使用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="h3 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)框默認(rè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> </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="h3 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>×</span> </button> <h5 class="modal-title">確認(rèn)刪除么?</h5> </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">確認(rèn)</button> </div> </div> </div> </div> </div> </body> </html>
第三步、定義userList用來保存用戶,userName保存用戶名, age保存用戶變量, 然后把userName和age 通過 v-model指令綁定到對應(yīng)的輸入框,實現(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="h3 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>×</span> </button> <h5 class="modal-title">確認(rèn)刪除么?</h5> </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">確認(rèn)</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="h3 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>×</span> </button> <h5 class="modal-title">確認(rèn)刪除么?</h5> </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">確認(rèn)</button> </div> </div> </div> </div> </div> </body> </html>
第五步、結(jié)合數(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="h3 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>×</span> </button> <h5 class="modal-title">確認(rèn)刪除么?</h5> </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">確認(rèn)</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="h3 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>×</span> </button> <h5 class="modal-title">確認(rèn)刪除么?</h5> </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);">確認(rèn)</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) { //當(dāng)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="h3 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>×</span> </button> <h5 class="modal-title">確認(rèn)刪除么?</h5> </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);">確認(rèn) </button> </div> </div> </div> </div> </div> </body> </html>
關(guān)于“vue如何實現(xiàn)留言板todolist功能”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,使各位可以學(xué)到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。