最新文章专题视频专题问答1问答10问答100问答1000问答2000关键字专题1关键字专题50关键字专题500关键字专题1500TAG最新视频文章推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37视频文章20视频文章30视频文章40视频文章50视频文章60 视频文章70视频文章80视频文章90视频文章100视频文章120视频文章140 视频2关键字专题关键字专题tag2tag3文章专题文章专题2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章专题3
问答文章1 问答文章501 问答文章1001 问答文章1501 问答文章2001 问答文章2501 问答文章3001 问答文章3501 问答文章4001 问答文章4501 问答文章5001 问答文章5501 问答文章6001 问答文章6501 问答文章7001 问答文章7501 问答文章8001 问答文章8501 问答文章9001 问答文章9501
当前位置: 首页 - 科技 - 知识百科 - 正文

vue实现留言板todolist功能

来源:懂视网 责编:小采 时间:2020-11-27 22:32:46
文档

vue实现留言板todolist功能

vue实现留言板todolist功能:通过前面两篇文章的的学习,我们掌握了vue的基本用法. 本文,就利用这些基础知识来实现一个留言板, 老外把他称之为todolist. 第一步、使用bootstrap做好布局 <!DOCTYPE html> <html> <head lang=en> <
推荐度:
导读vue实现留言板todolist功能:通过前面两篇文章的的学习,我们掌握了vue的基本用法. 本文,就利用这些基础知识来实现一个留言板, 老外把他称之为todolist. 第一步、使用bootstrap做好布局 <!DOCTYPE html> <html> <head lang=en> <

通过前面两篇文章的的学习,我们掌握了vue的基本用法. 本文,就利用这些基础知识来实现一个留言板, 老外把他称之为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>暂无数据....</p>
 </td>
 </tr>
 </table>
</div>
</body>
</html>

第二步、增加模态框,模态框默认为隐藏的

<!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>暂无数据....</p>
 </td>
 </tr>
 </table>

 <!--模态框 弹出框-->
 <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>
 <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指令绑定到对应的输入框,实现输入框与变量中的数据双向驱动,在表格的行中输出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>暂无数据....</p>
 </td>
 </tr>
 </table>

 <!--模态框 弹出框-->
 <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>
 <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>

第四步、添加用户,点击添加按钮,把输入框中的数据作为一个对象 push 到数组userList,添加完之后,把userName, age清空,那么两个输入框的内容就会被清空

<!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>暂无数据....</p>
</td>
</tr>
</table>

<!--模态框 弹出框-->
<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>
<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>

第五步、结合数组的长度与v-show指令,实现提示信息的显示与隐藏

<!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>暂无数据....</p>
 </td>
 </tr>
 </table>

 <!--模态框 弹出框-->
 <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>
 <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>

第六步、实现删除某行数据

<!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>暂无数据....</p>
 </td>
 </tr>
 </table>

 <!--模态框 弹出框-->
 <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>
 <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>

第七步、实现删除全部行

<!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的时候,清空数组,就是删除全部
 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>暂无数据....</p>
 </td>
 </tr>
 </table>

 <!--模态框 弹出框-->
 <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>
 <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>

声明:本网页内容旨在传播知识,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。TEL:177 7030 7066 E-MAIL:11247931@qq.com

文档

vue实现留言板todolist功能

vue实现留言板todolist功能:通过前面两篇文章的的学习,我们掌握了vue的基本用法. 本文,就利用这些基础知识来实现一个留言板, 老外把他称之为todolist. 第一步、使用bootstrap做好布局 <!DOCTYPE html> <html> <head lang=en> <
推荐度:
标签: 功能 VUE 留言板
  • 热门焦点

最新推荐

猜你喜欢

热门推荐

专题
Top