最近学习vue,在此做个简单记录
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html >
<head>
<meta charset="utf-8" http-equiv="Access-Control-Allow-Origin" content="*">
<title>index</title>
<script src="http://cdn.bootcss.com/jquery/1.11.0/jquery.min.js" ></script>
<!-- 引入Vue -->
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<style>
.tablediv{
/* 设置div大小 */
width: 1000px;
/* 设置div位置 */
position:absolute;
top: 200px;
left: 500px;
/* 设置div字体居中 */
text-align:center;
}
table{
/* 设置table大小 */
width: 100%;
/* 设置字体大小 */
font-size: 30px;
}
th{
/* 设置th字体加粗 */
font-weight: 900;
/* 设置th背景颜色 */
background-color: rgb(0, 153, 255);
/* 设置th字体颜色 */
color: rgb(255, 254, 254);
}
/* 设置表格隔行变色 */
table tr:nth-child(even){
/* 背景颜色 */
background-color: rgba(199, 199, 199, 0.363);
}
/* 设置鼠标停留一行改变背景颜色 */
tr:hover{
/* 背景颜色 */
background-color: rgb(197, 197, 197) !important;
/* !important 设置优先级*/
}
h1{
/* 设置h1(标题)位置 */
position:absolute;
top: 10px;
left: 850px;
/* 字体大小 */
font-size: 50px;
}
.updbutton{
/* 设置button(修改按钮) */
width: 80px;
height: 30px;
margin-left: 10px;
background-color: rgb(0, 255, 64);
}
.updbutton:hover{
/* 设置button(修改按钮) 鼠标停留样式 */
/* 字体颜色 */
color: rgb(255, 255, 255);
/* 背景颜色 */
background-color: rgb(255, 145, 0);
}
.delbutton{
/* 设置button(删除按钮) */
width: 80px;
height: 30px;
background-color: rgb(255, 0, 0);
}
.delbutton:hover{
/* 设置button(删除按钮) 鼠标停留样式 */
/* 字体颜色 */
color: rgb(255, 255, 255);
/* 背景颜色 */
background-color: rgb(255, 145, 0);
}
.addbutton{
/* 设置button(添加按钮) */
/* 位置 大小 */
position: absolute;
left: -30%;
top:-20%;
width: 150px;
height: 50px;
/* 背景颜色 */
background-color: rgb(0, 4, 255);
/* 字体颜色 */
color: #ffffff;
font-size: 30px;
z-index: 900;
}
.addbutton:hover{
/* 设置button(添加按钮) 鼠标停留样式*/
/* 背景颜色 */
background-color: rgb(255, 123, 0);
/* 字体颜色 */
color: #ffffff;
font-size: 30px;
}
.heisebeijing{
/* 设置 添加修改操作时的黑色背景 */
/* 位置 大小 */
position: absolute;
left: -0.5px;
top:-0.5px;
width: 100%;
height: 100%;
/* 显示在表格之上,添加修改之下 */
z-index: 1000;
/* 透明 */
opacity: 0.8;
/* 背景颜色 */
background-color: #020202;
/* 设置初始隐藏 */
display: none;
}
.mydiv{
/* 添加/修改 div样式设置 */
/* 位置大小 */
position: absolute;
left: 30%;
top:15%;
width: 800px;
height: 450px;
/* 背景颜色 */
background-color: #fff;
/* 显示在表格,黑色背景之上 */
z-index: 1000;
/* 设置div字体居中 */
text-align:center;
/* 设置初始隐藏 */
display: none;
}
h2{
/* 添加/修改 标题设置 */
font-weight: 900;
font-size: 50px;
margin: 10px;
}
label{
/* 添加/修改 提示设置 */
font-weight: 400;
font-size: 30px;
margin: 10px;
}
input{
/* 弹出框的输入框 */
width: 300px;
height: 40px;
font-size: 30px;
/* 设置字体居中 */
text-align:center;
}
.btadd{
/* 弹出框的添加按钮 */
/* 位置 大小 */
position: absolute;
right: 20%;
bottom:10%;
width: 150px;
height: 50px;
/* 背景颜色 */
background-color: rgb(0, 255, 0);
/* 字体颜色 */
color: #ffffff;
font-size: 30px;
z-index: 900;
}
.btadd:hover{
/* 弹出框的添加按钮 鼠标停留样式*/
background-color: rgb(255, 94, 0);
}
.btqx{
/* 弹出框的取消按钮 */
/* 位置 大小 */
position: absolute;
left: 20%;
bottom:10%;
width: 150px;
height: 50px;
/* 背景颜色 */
background-color: rgb(3, 112, 255);
/* 字体颜色 */
color: #ffffff;
font-size: 30px;
z-index: 900;
}
.btqx:hover{
/* 弹出框的取消按钮 鼠标停留样式*/
background-color: rgb(255, 94, 0);
}
</style>
</head>
<body >
<div class="heisebeijing"></div><!-- 黑色背景 -->
<div class="mydiv"><!-- 添加/修改 -->
<h2 id="title">标题</h2>
<hr/>
<br>
<!-- onfocus 点击时选中所有 maxlength 长度限制 required="required" 不能为空 oninput = "value=value.replace(/[^\d]/g,'')" 只允许输入数字 -->
<label>编号</label><input id="id" oninput = "value=value.replace(/[^\d]/g,'')" type="text" onfocus="this.select()" maxlength="7" required="required">
<br> <br> <br> <br>
<label>名字</label><input id="name" type="text" onfocus="this.select()" maxlength="4" required="required">
<button class="btadd" id ="btadd" onclick="btadd()">添加</button>
<button class="btqx" onclick="btqx()">取消</button>
</div>
<h1>数据增删改</h1>
<div id="tablediv" class="tablediv"><!-- 防止table的div,方便设置表格的位置和大小 -->
<button class="addbutton" v-on:click="add()">添加</button>
<table border="1px solid #ccc" cellspacing="0" cellpadding="0"><!-- table,设置table框线 -->
<tr>
<th>NO.</th>
<th>编号</th>
<th>名字</th>
<th style="width: 250px;">操作</th>
</tr>
<template v-for="(tablelists, index) in tablelist" style="width: 100%;" >
<!-- 循环输出赋值 -->
<tr>
<td>{{index}}</td>
<td>{{tablelists.id}}</td>
<td>{{tablelists.name}}</td>
<td><button class="delbutton" v-on:click="deletea(index,tablelists)">X</button><button class="updbutton" v-on:click="update(index,tablelists.id,tablelists.name)">修改</button></td>
<!-- 删除按钮 修改按钮 参数 -->
</tr>
</template>
</table>
</div>
<script>
var shuju=[{name:'a',id:1323},{name:'a1',id:13223}];
//测试数据
var divn=0;//标志位,判断是添加还是修改
var topvm = new Vue({
el : "#tablediv",
data:{
tablelist:'',
},
methods:{
successtishi () {
this.$Message.success('追加成功');
},
deletea(index,tablelists){
//删除操作,此次操作只在页面上删除,没有删除数据库的
//两个参数,index是编号,
//tablelists是数据 console.log(tablelists.name);
topvm.tablelist.splice(index,1);
},
add(){
//添加操作,显示div,修改title名字
$(".heisebeijing").show();
$(".mydiv").show();
document.getElementById("title").innerHTML ="添加操作";
document.getElementById("btadd").innerHTML ="添加";
divn=-1;//修改标志位
},
update(index,id,name){
//修改操作,显示div,修改title名字,给input赋值
$(".heisebeijing").show();
$(".mydiv").show();
document.getElementById("id").value=id;
$("#id").attr("disabled",true); // 设置id不可修改
document.getElementById("name").value=name;
document.getElementById("title").innerHTML ="修改操作";
document.getElementById("btadd").innerHTML ="修改";
divn=index;//修改标志位
}
}
});
topvm.tablelist=shuju;
function guanbi(){
//关闭弹框函数,便于复用
$(".heisebeijing").hide();
$(".mydiv").hide();
document.getElementById("id").value="";
$("#id").attr("disabled",false); // 设置id可修改
document.getElementById("name").value="";
}
$(".heisebeijing").click(function(){
// 点击黑色背景,关闭弹出div
guanbi();
});
function btadd(){
// 添加操作
var id=document.getElementById("id").value;
var name=document.getElementById("name").value;
name = name.replace(/\s*/g,"");//去除空格
if(divn==-1){//判断是添加
shuju.push({id:id,name:name});
//此出也可以写做 topvm.tablelist.push({id:id,name:name});
}else{
shuju.splice(divn,1,{id:id,name:name});
}
guanbi();
}
function btqx(){
//取消操作
guanbi();
}
</script>
</body>
</html>