基於SharePoint平台開發時,人員選取器使用頻率是非常高的,但是原生的人員選取器使用太麻煩,而且非常笨拙,非常不友好,特別是對呆在政府部門的老爺們,要讓他們手動輸入人員,簡直就是癡心妄想。總之一句話,越簡單越好。
為了讓客戶滿意,必須要對人員選取器進行改造,原生的PeopleEditor徹底拋棄。只能另闢蹊徑,尋找適合的JQuery外掛程式,建立新的人員選取器,分析了一下需求,可以歸納新的人員選取器必須支援如下情況:
技術服務人員的多選,比如像會議、通知需要對多人進行發送,當然也要支援刪除。
對於單選的人員選取器,可以刪除選中的人員。
不管單選還是多選,支援Jquey AutoComplete那樣索引功能。
找來找去,發現Jquery Chosen功能十分強大,完全滿足我的需求,更多的功能參照Chosen官網:
http://harvesthq.github.io/chosen/
利用Jquery Chosen進行改造
多選的人員選取器
支援多選,點擊X即可取消選中,當然還支援索引,如下所示:
配置也是十分簡單,首先你的有一個Select,譬如:
<asp:DropDownList runat="server" ClientIDMode="Static" ID="ddlPeopleChosen" data-placeholder="選擇與會者..."class="chzn-select" multiple style="width:397px;" ></asp:DropDownList>
注意下:data-placeholder意為著未選人員時的預設文本,multiple意味著支援多選。
接下來,需要對其添加資料來源,注意,對於單人員選取器,Chosen作者說如果要顯示預設的文本提示,需要加入一個空的Option到Select中(第一個)。
注意:我的人員不是從AD中取出,而是我們有一個存放人員的List(人事檔案),為了確保該List的人員都可以登陸OA,特意和Web.AllUser中進行比較,當然也可以不必要,這樣做保險點。
public static void GetFromCache(SPWeb _currentWeb) { #region 從緩衝中讀 if (System.Web.HttpContext.Current.Cache["peopleList"] == null) { //People 集合:將SharePoint中的User作為資料來源集合加入DropDownList中 List<People> peopleList = new List<People>(); //Note: on single selects, the first element is assumed to be selected by the browser. //To take advantage of the default text support, //you will need to include a blank option as the first element of your select list. peopleList.Add(new People()); People p = null; SPList employeeList = _currentWeb.Site.AllWebs["rsgl"].Lists["人事檔案"]; //擷取所有可訪問網站的使用者 SPUserCollection userCollection = _currentWeb.AllUsers; //轉換為List集合 List<SPUser> listUsers = userCollection.Cast<SPUser>().ToList(); foreach (SPListItem item in employeeList.Items) { string displayName = item["Title"].ToStringOrEmpty(); //迴圈便利擷取SPUser foreach (SPUser user in listUsers) { if (displayName == user.Name) { string loginName = user.LoginName; p = new People { LoginName = loginName, DisplayName = displayName }; peopleList.Add(p); } } } System.Web.HttpContext.Current.Cache["peopleList"] = peopleList; } #endregion }