效果
代码
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Documenttitle>
<link rel="stylesheet" href="./test.css">
head>
<body>
<div id="app">
<div class="wrapper">
<h1>图书管理h1>
<div class="option">
<label for="id" >编号:label>
<input type="text" id="id" v-model.trim="id" :disabled="idFlag" v-focus>
<label for="name">名称:label>
<input type="text" id="name" v-model.trim="name" @keyup.enter="submit" >
<button @click="submit" :disabled="submitFlag">提交button>
div>
<div class="count">
<span>图书总数:span>
<span>{{booksCount}}span>
div>
<table>
<thead>
<tr>
<th>编号th>
<th>名称th>
<th>时间th>
<th>操作th>
tr>
thead>
<tbody>
<tr :key="item.id" v-for="item in books">
<td>{{item.id}}td>
<td>{{item.name}}td>
<td>{{item.date | formatDate('yyyy-MM-dd hh:mm:ss')}}td>
<td>
<a href="" @click.prevent="modifyBook(item.id)">修改a>
<span>|span>
<a href="" @click.prevent="deleteBook(item.id)">删除a>
td>
tr>
tbody>
table>
div>
div>
<script src="./vue.js">script>
<script src="./test.js">script>
body>
html>
.wrapper {
width: 530px;
margin: 50px auto;
text-align: center;
}
.wrapper .option,
.wrapper .count {
height: 35px;
line-height: 35px;
background-color: #f3dcab;
border-bottom: 1px solid #c2d89a;
}
.wrapper table {
width: 100%;
border-collapse: collapse;
}
.wrapper thead {
height: 40px;
line-height: 40px;
background-color: #f3dcab;
font-weight: 700;
}
.wrapper tbody td {
height: 35px;
line-height: 35px;
border: 1px dashed #f3dcab;
}
Vue.filter('formatDate', function (value, arg) {
return dateFormat(value, arg)
})
Vue.directive('focus',{
inserted:function(el) {
el.focus()
}
})
var vm = new Vue({
el: '#app',
data: {
id: '',
name: '',
books: [],
idFlag: false,
submitFlag: false
},
methods: {
submit: function () {
if (this.idFlag == false) {
if(this.id==''||this.name=='') {
alert('请输入内容')
return
}
var newBook = {}
newBook.id = this.id
newBook.name = this.name
newBook.date = +new Date()
this.books.push(newBook)
} else {
var book = this.books.find((item) => {
return item.id == this.id
})
book.name=this.name
}
this.id=''
this.name=''
this.idFlag =false
},
modifyBook: function (id) {
var book = this.books.find(function (item) {
return item.id == id
})
this.id = book.id
this.name = book.name
this.idFlag = true
},
deleteBook: function (id) {
var index = this.books.findIndex(function (item) {
return item.id == id
})
this.books.splice(index, 1)
}
},
computed: {
booksCount: function () {
return this.books.length
}
},
watch: {
id:function(value) {
this.submitFlag= this.books.some(function(item) {
return item.id == value
})
},
name:function(value) {
this.submitFlag = this.books.some(function(item) {
return item.name == value
})
}
},
mounted: function () {
serverData = [{
id: 1,
name: '三国演义',
date: +new Date()
}, {
id: 2,
name: '水浒传',
date: +new Date()
}, {
id: 3,
name: '红楼梦',
date: +new Date()
}, {
id: 4,
name: '西游记',
date: +new Date()
}]
this.books = serverData
}
})
function dateFormat(date, format) {
if (typeof date === "string") {
var mts = date.match(/(\/Date\((\d+)\)\/)/);
if (mts && mts.length >= 3) {
date = parseInt(mts[2]);
}
}
date = new Date(date);
if (!date || date.toUTCString() == "Invalid Date") {
return "";
}
var map = {
"M": date.getMonth() + 1,
"d": date.getDate(),
"h": date.getHours(),
"m": date.getMinutes(),
"s": date.getSeconds(),
"q": Math.floor((date.getMonth() + 3) / 3),
"S": date.getMilliseconds()
};
format = format.replace(/([yMdhmsqS])+/g, function (all, t) {
var v = map[t];
if (v !== undefined) {
if (all.length > 1) {
v = '0' + v;
v = v.substr(v.length - 2);
}
return v;
} else if (t === 'y') {
return (date.getFullYear() + '').substr(4 - all.length);
}
return all;
});
return format;
}