溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

vue實現(xiàn)簡單學生信息管理的代碼解析

發(fā)布時間:2020-07-20 16:00:16 來源:億速云 閱讀:190 作者:小豬 欄目:web開發(fā)

這篇文章主要講解了vue實現(xiàn)簡單學生信息管理的代碼解析,內(nèi)容清晰明了,對此有興趣的小伙伴可以學習一下,相信大家閱讀完之后會有幫助。

<!DOCTYPE html>
<html lang="en">
<head>
 <meta charset="UTF-8">
 <title>學生信息管理</title>
 <link rel="stylesheet" href="./lib/bootstrap.css" >
 <script src="./lib/vue.js"></script>
 <style type="text/css">
 #app{
  margin: 10px;
 }
 </style>
</head>
<body>
<div id="app">
 <form class="form-inline">
 <div class="form-group">
  <label>學號:</label>
  <input type="text" class="form-control" v-model="stuNo">
 </div>&nbsp;&nbsp;
 <div class="form-group">
  <label>姓名:</label>
  <input type="email" class="form-control" v-model="name" @keyup.enter="add">
 </div>&nbsp;
 <input type="button" class="btn btn-primary" value="添加" @click="add">
 &nbsp;&nbsp;&nbsp;&nbsp;
 <div class="form-group">
  <label>搜索姓名關(guān)鍵字:</label>
  <input type="email" class="form-control" v-model="keywords" @keyup.enter="search(keywords)" v-focus>
 </div>
 </form>
 <br/>
 <table class="table table-bordered" >
 <thead>
 <th>學號</th>
 <th>姓名</th>
 <th>添加時間</th>
 <th>操作</th>
 </thead>
 <tbody v-for="(item,i) in search(keywords)" :key="item.stuNo" >
 <tr>
  <td>{{item.stuNo}}</td>
  <td>{{item.name}}</td>
  <td>{{item.cTime | dateFormat}}</td>
  <td><a href="" @click.prevent=" del(item.stuNo)">刪除</a></td>
 </tr>
 </tbody>
 </table>
</div>

<script>
 // 自定義自動獲取焦點的全局指令
 Vue.directive('focus',{
 // 當被綁定的元素插入到 DOM 中時……
 inserted: function (el) {
  // 聚焦元素
  el.focus()
 }
 })
 var vm = new Vue({
 el:'#app',
 data:{
  stuNo:'',
  name:'',
  keywords:'',
  list:[
  {
   stuNo:1710204016,
   name:'劉小紅',
   cTime:new Date()
  },
  {
   stuNo:1710204007,
   name:'李大明',
   cTime:new Date()
  }
  ]
 },
 methods:{
  add(){
  var newInfo = {stuNo:this.stuNo, name:this.name, cTime:new Date()}
  this.list.push(newInfo)
  this.stuNo=this.name=''
  },
  del(stuNo){
  this.list.some((item,i)=>{
   if(item.stuNo===stuNo){
   this.list.splice(i,1)
   return true;
   }
  })
  },
  search(keywords){
  // var newList = []
  // this.list.forEach(item=>{
  // if(item.name.indexOf(keywords)!=-1){
  //  newList.push(item)
  // }
  // })
  // return newList
  return this.list.filter(item=>{
   if(item.name.includes(keywords)){
   return item
   }
  })
  }
 },
 filters:{
  dateFormat:function(dateStr){
  var year = dateStr.getFullYear()
  var mouth = (dateStr.getMonth() + 1).toString().padStart(2,'0')
  var date = (dateStr.getDate()).toString().padStart(2,'0')
  var h = (dateStr.getHours()).toString().padStart(2,'0')
  var m = (dateStr.getMinutes()).toString().padStart(2,'0')
  var s = (dateStr.getSeconds()).toString().padStart(2,'0')
  return `${year}-${mouth}-${date} ${h}:${m}:${s}`
  }
 }
 })
</script>
</body>
</html>

看完上述內(nèi)容,是不是對vue實現(xiàn)簡單學生信息管理的代碼解析有進一步的了解,如果還想學習更多內(nèi)容,歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI