在Key單UI的實務上,有個有趣的現象。假設Key單Web介面中有個下拉選單(DropDown, <SELECT>),張三因為承做甲業務,所以永遠都選第三個選項,而李四較常在處理乙業務,所以總是選第五個選項。如果介面在顯示時可以預設就停在使用者最常用的選項上,每次Key單時就可以少敲幾次鍵。

此時有幾種設計方法: 第一種是建立User的Profile,由其負責的業務來判斷預設選項,再不然就是開放User設定自己偏好的預設選項。我個人更偏好一種更直覺更簡單的做法---用Cookie記住User上回選什麼就好了,不用在Server/DB上找地方存User設定,也不用擔心User可能一陣子做甲業務,一陣子搞乙業務,設定得調來調去。總之,上次輸入什麼,這次就預先帶出什麼值,KISS(Keep It Simple and Stupid)永遠是最美妙滴!!

底下是一段用ASP寫的Sample Code(ASP.NET裡的寫法大同小異),Product Category下拉選單會用Cookie記住你上次選的值,下次再進入頁面時會預設停在上面。

有幾點補充一下:

1) 記得要設Cookie的Expires屬性,否則Cookie會在Browser關掉後立刻消失。

2) 設好Cookie後,每次發Request到Web時都會夾帶你的Cookie內容(是的,每個Request都要!),所以別存太多資料在Cookie中(記住! 它只是Cookie,別把它當Pizza),以免形成傳輸負擔。

3) oSel.options[i].value=='<%=sDefaultProdCatg%>'的寫法記得要Escape掉單引號,至於換行符號,應該是可以偷懶不處理囉。

<% Option Explicit %>
<%
Dim sDefaultProdCatg
Const cookieName="AfaProdKeyIn"
If (Request("txtClientName")<>"") Then 'Postback
 'Set Expires to keep this cookie for one month, 
 'If Expires is not set, cookie will be gone after browser closed
 Response.Cookies(cookieName).Expires = DateAdd("m", 1, Now)
 Response.Cookies(cookieName)=Request("selCategory")
 Response.Write "<script>alert('Inserted!');</script>"
 Response.End
Else
 sDefaultProdCatg = Replace(Request.Cookies(cookieName), "'", "\'")
End If
%>
<html><body>
<form method="POST" action="userPref.asp">
Client Name: <input type="text" name="txtClientName" ><br>
Product Category: 
 <select name="selCategory">
 <option value="Notebook">Notebook</option>
 <option value="PC">PC</option>
 <option value="LCD">LCD</option>
 </select><br>
<script type="text/javascript">
var oSel = document.getElementById("selCategory");
for (var i=0; i<oSel.options.length; i++)
 if (oSel.options[i].value=='<%=sDefaultProdCatg%>') {
 oSel.selectedIndex=i;
 break;
 }
</script>
<input type="submit" value="Add">
</form>
</body></html>

Comments

Be the first to post a comment

Post a comment