DIV CSS 佈局教程網

用 Vue 打造可新增、刪除、查詢與修改的成績單範例
編輯:關於JavaScript     

從框架更迭說起

前端生態變化得很快,前年是 AngularJS 獨佔鰲頭,到了這兩年則是 React 與 Vue 各領風騷。當年我硬著頭皮啃完幾套 AngularJS 教學,卻總被裡頭的概念繞得暈頭轉向;React 出自 Facebook,邊學邊用;後來又抽空翻了 Vue 的官方文件,一讀之下格外順眼,便忍不住想拿來實作一個小專案試試手。

正好週末有閒,於是動手做了一個小成績單。更早之前我也用 avalon 寫過類似功能,對照兩邊的開發過程會發現,這類 MVVM 框架其實都在做同一件事:把開發者從頻繁操作 DOM 的苦海裡解救出來,讓我們專心處理資料與業務邏輯。就這兩個範例來看,開發效率的提升是成倍計的。

完整範例程式碼

 <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>vue成績單</title>
<style type="text/css">
*{ margin:0; padding:0; }
.report_card{ width:800px; margin:0 auto; font-size:12px; }
.report_card table{ width:100%; border-collapse: collapse; text-align:center; }
.report_card caption{ font-size:14px; text-align:left; line-height:30px; font-weight:bold; }
.report_card table th,.report_card table td{ border:1px solid #ccc; }
.report_card table th{ height:36px; background:#f8f8f8; }
.report_card table td{ height:32px; background:#f8f8f8; }
.content{ width:100%; height:32px; line-height:32px; position:relative; }
.content input{ position:absolute; top:0; left:0; width:100%; color:#999; padding-left:10px;
 -webkit-box-sizing:border-box; box-sizing:border-box; height:30px; border:1px solid blue;
 -webkit-animation:borderAn 2s infinite; animation:borderAn 2s infinite; }
.studyForm select{ width:100px; height:28px; }
.searchInput{ width:200px; height:28px; }
.searchButton{ width:100px; height:32px; }
@-webkit-keyframes borderAn{ 0%{ border-color:transparent; } 100%{ border-color:blue; } }
@keyframes borderAn{ 0%{ border-color:transparent; } 100%{ border-color:blue; } }
.studyForm{ margin:10px 0; }
.studyForm input{ width:120px; height:30px; }
</style>
</head>
<body>
<div class="report_card" id="reportCard">
 <table class="studyForm">
 <caption>成績錄入/處理</caption>
 <tbody>
 <tr>
 <td width="170">學號:<input type="text" v-model="addArr.stuId"></td>
 <td width="170">姓名:<input type="text" v-model="addArr.name"></td>
 <td width="170">語文:<input type="text" v-model="addArr.ywScores"></td>
 <td width="170">數學:<input type="text" v-model="addArr.sxScores"></td>
 <td colspan="2" width="120">
 <a href="javascript:void(0);" v-on:click="submitStu">錄入</a>
 <a href="javascript:void(0);" v-on:click="resetStu">重置</a>
 </td>
 </tr>
 <tr>
 <td align="left"> 搜索:<input v-model="searchTxt" type="text" class="searchInput"> </td>
 <td> 排序字段: <select v-model='sortKey'>
 <option value="ywScores">語文</option>
 <option value="sxScores">數學</option>
 </select> </td>
 <td> 排序類型: <select v-model="sortClass">
 <option value="1">升序</option>
 <option value="-1">降序</option>
 </select> </td>
 <td colspan="3"></td>
 </tr>
 </tbody>
 </table>
 <table class="scoreList">
 <caption>成績列表</caption>
 <thead>
 <th width="170">學號</th>
 <th width="170">姓名</th>
 <th width="170">語文</th>
 <th width="170">數學</th>
 <th colspan="2" width="120">操作</th>
 </thead>
 <tbody>
 <tr v-for="item in studyArr | filterBy searchTxt | orderBy sortKey sortClass">
 <td><div class="content">{{item.stuId}}<input v-model="editArr.stuId" type="text" v-if="$index==nowEditCol"></div></td>
 <td><div class="content">{{item.name}}<input v-model="editArr.name" type="text" v-if="$index==nowEditCol"></div></td>
 <td><div class="content">{{item.ywScores}}<input v-model="editArr.ywScores" type="text" v-if="$index==nowEditCol"></div></td>
 <td><div class="content">{{item.sxScores}}<input v-model="editArr.sxScores" type="text" v-if="$index==nowEditCol"></div></td>
 <td>
 <a href="javascript:void(0);" v-on:click="startEdit($index)" v-if="$index!=nowEditCol">編輯</a>
 <a href="javascript:void(0);" v-on:click="cancelEdit" v-if="$index==nowEditCol">取消</a>
 <a href="javascript:void(0);" v-on:click="sureEdit($index)" v-if="$index==nowEditCol">確認</a>
 </td>
 <td><a href="javascript:void(0);" v-on:click="deleteStu($index)">刪除</a></td>
 </tr>
 </tbody>
 </table>
</div>
<script type="text/javascript" src="vue.js"></script>
<script type="text/javascript">
var studyArrJson=[
 {'stuId':'stu0001','name':'張三','ywScores':85,'sxScores':90},
 {'stuId':'stu0002','name':'李四','ywScores':88,'sxScores':85},
 {'stuId':'stu0003','name':'王五','ywScores':65,'sxScores':75},
 {'stuId':'stu0004','name':'劉六','ywScores':58,'sxScores':96}
];
var reportCardVm=new Vue({
 el:'#reportCard',
 data:{
 studyArr:studyArrJson, //成績花名冊
 addArr:{'stuId':'','name':'','ywScores':'','sxScores':''}, //新增的表單字段
 nowEditCol:-1, //當前編輯的行
 editStatus:false, //當前是否在編輯狀態
 searchTxt:'', //搜索字段
 sortKey:'ywScores', //排序鍵
 sortClass:'1' //升降排序,1為升,-1為降
 },
 methods:{
 startEdit:function(index){ this.nowEditCol=index; },
 cancelEdit:function(){ this.nowEditCol=-1; },
 sureEdit:function(index){ this.studyArr.$set(index,this.editArr); this.nowEditCol=-1; },
 deleteStu:function(index){ this.studyArr.splice(index,1); },
 submitStu:function(){
 var addArr={ 'stuId':this.addArr.stuId, 'name':this.addArr.name,
 'ywScores':this.addArr.ywScores, 'sxScores':this.addArr.sxScores };
 this.studyArr.push(addArr); this.resetStu();
 },
 resetStu:function(){
 this.addArr={ 'stuId':'', 'name':'', 'ywScores':'', 'sxScores':'' }
 }
 },
 computed:{
 //存放當前編輯的對象
 editArr:function(){
 var editO=this.studyArr[this.nowEditCol];
 return { 'stuId':editO.stuId, 'name':editO.name,
 'ywScores':editO.ywScores, 'sxScores':editO.sxScores }
 }
 }
})
</script>
</body>
</html> 

線上測試位址:https://jsbin.com/kavugufuci/edit?html,output

一個 Vue 實例由什麼組成

一個 Vue 物件就是一個 view model,基本上由底下幾個部分拼起來:

其中 data 放的是目前 view 的屬性,也就是頁面上可以直接綁定的資料;methods 放的是這個 view model 的方法;computed 同樣是屬性,但它是計算屬性——它的值可能被 data 裡的某個欄位直接牽動,你一改 data,它會自動跟著變,效果等同於 AngularJS 裡用 $watch 實作的監聽。Vue 也有提供 $watch,只是用計算屬性寫起來更簡潔、效率也更好。

屬性區塊:data

這是這個範例的 view model 屬性。要綁定到 HTML 上、需要具備響應能力的資料,都必須在這裡先宣告好;後續才會用到的欄位也要在初始化時先把位置留下來,事後用 JS 硬塞上去是不會生效的。各欄位的用途請見程式碼裡的註解。

方法區塊:methods

這是 view model 的方法。它既可以綁到 HTML 上,也可以在內部以 this. 開頭呼叫;這裡的 this 一律指向目前的 view model,因此能取用該 view model 上的所有屬性與方法。資料處理與業務邏輯,基本上都寫在這一塊。

計算屬性區塊:computed

這裡是計算屬性,它的值由 data 底下的 nowEditCol 決定,等於幫你寫了一個監聽 nowEditCol 的 $watch,只不過 Vue 內部已經替你處理完畢,正式專案裡很值得推薦。

整個範例採用的都是把 view model 直接綁到 DOM 元素上的寫法,這也正是主流 MVVM 框架的典型模式。

順帶聊聊 Vue、avalon、AngularJS 與 React 的取捨

這幾年我一直在關注 Vue、avalon、AngularJS、React 這幾套方案。考量到上手速度,我最先選的是 Vue 與 avalon;但 Vue 有相容性門檻,一旦採用就得放棄 Android 4.2 以下的原生瀏覽器,於是轉而使用 avalon,並用它做過幾個 H5 專案。

只是 avalon 屬於司徒正美的個人專案,在穩定性與長遠發展上總令人有些疑慮,跑大量測試案例時也確實遇到過一些 bug。所幸我經手的那幾個專案還沒踩進 avalon 的大坑,而它的相容性表現確實值得稱讚,看得出作者投入了大量心力。如果你的專案需要相容 IE6、7、8 這類非標準瀏覽器,同時又想享受 MVVM 帶來的開發效率,avalon 會是首選。而在目前瀏覽器環境日益改善的情況下,日後若再接到 H5 專案,我會優先選擇 Vue。

想更進一步認識 Vue,建議直接查閱官方文件:https://cn.vuejs.org/guide/,那也是最佳的入門起點。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved