這篇文章將為大家詳細(xì)講解有關(guān)vue如何實(shí)現(xiàn)留言板todolist功能,小編覺(jué)得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。
Vue是一款友好的、多用途且高性能的JavaScript框架,使用vue可以創(chuàng)建可維護(hù)性和可測(cè)試性更強(qiáng)的代碼庫(kù),Vue允許可以將一個(gè)網(wǎng)頁(yè)分割成可復(fù)用的組件,每個(gè)組件都包含屬于自己的HTML、CSS、JavaScript,以用來(lái)渲染網(wǎng)頁(yè)中相應(yīng)的地方,所以越來(lái)越多的前端開(kāi)發(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">序號(hào)</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>暫無(wú)數(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">序號(hào)</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>暫無(wú)數(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用來(lái)保存用戶,userName保存用戶名, age保存用戶變量, 然后把userName和age 通過(guò) v-model指令綁定到對(duì)應(yīng)的輸入框,實(shí)現(xiàn)輸入框與變量中的數(shù)據(jù)雙向驅(qū)動(dòng),在表格的行中輸出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">序號(hào)</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>暫無(wú)數(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>
第四步、添加用戶,點(diǎn)擊添加按鈕,把輸入框中的數(shù)據(jù)作為一個(gè)對(duì)象 push 到數(shù)組userList,添加完之后,把userName, age清空,那么兩個(gè)輸入框的內(nèi)容就會(huì)被清空
<!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">序號(hào)</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>暫無(wú)數(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ù)組的長(zhǎng)度與v-show指令,實(shí)現(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">序號(hào)</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>暫無(wú)數(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í)現(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">序號(hào)</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>暫無(wú)數(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>
第七步、實(shí)現(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í)候,清空數(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">序號(hào)</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>暫無(wú)數(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如何實(shí)現(xiàn)留言板todolist功能”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,使各位可以學(xué)到更多知識(shí),如果覺(jué)得文章不錯(cuò),請(qǐng)把它分享出去讓更多的人看到。
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無(wú)理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場(chǎng)景需求。
新聞標(biāo)題:vue如何實(shí)現(xiàn)留言板todolist功能-創(chuàng)新互聯(lián)
本文來(lái)源:http://chinadenli.net/article6/peeig.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供營(yíng)銷型網(wǎng)站建設(shè)、企業(yè)建站、自適應(yīng)網(wǎng)站、用戶體驗(yàn)、面包屑導(dǎo)航、網(wǎng)站排名
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)