這篇文章將為大家詳細講解有關(guān)vue如何實現(xiàn)留言板todolist功能,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。
創(chuàng)新互聯(lián)-專業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設、高性價比琿春網(wǎng)站開發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫,直接使用。一站式琿春網(wǎng)站制作公司更省心,省錢,快速模板網(wǎng)站建設找我們,業(yè)務覆蓋琿春地區(qū)。費用合理售后完善,10余年實體公司更值得信賴。Vue是一款友好的、多用途且高性能的JavaScript框架,使用vue可以創(chuàng)建可維護性和可測試性更強的代碼庫,Vue允許可以將一個網(wǎng)頁分割成可復用的組件,每個組件都包含屬于自己的HTML、CSS、JavaScript,以用來渲染網(wǎ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)框默認為隱藏的
<!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">確認刪除么?</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">確認</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="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">確認刪除么?</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">確認</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">確認刪除么?</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">確認</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">確認刪除么?</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">確認</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">確認刪除么?</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);">確認</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="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">確認刪除么?</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);">確認 </button> </div> </div> </div> </div> </div> </body> </html>
關(guān)于“vue如何實現(xiàn)留言板todolist功能”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,使各位可以學到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。
另外有需要云服務器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務器、裸金屬服務器、高防服務器、香港服務器、美國服務器、虛擬主機、免備案服務器”等云主機租用服務以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應用場景需求。
新聞標題:vue如何實現(xiàn)留言板todolist功能-創(chuàng)新互聯(lián)
本文來源:http://jinyejixie.com/article6/peeig.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供營銷型網(wǎng)站建設、企業(yè)建站、自適應網(wǎng)站、用戶體驗、面包屑導航、網(wǎng)站排名
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)