SharePoint JS Link 之(一) 改變column的顏色

來源:互聯網
上載者:User

標籤:sharepoint   js link   

在SharePoint2013之前,想要修改清單項目表單的樣式,是一件非常痛苦的事情。一般來說,任何的列表都包含三種表單:

1.      建立清單項目(new)

2.      編輯屬性(edit properties)

3.      查看清單項目(view properties)

 

除此之外,還有一種顯示方法,就是在視圖(view)顯示,比如:


在SharePoint 2013裡,新加了一個功能叫JSLink,有了它,不用寫任何後台代碼,只需要寫JS就能修改清單項目的顯示了。

 

比如,我想修改Language列的值顏色,不同的語言顯示不同的顏色,如何做到呢?

 

再次之前先建立一個空白js檔案。一個JSLink 檔案通常包含兩個部分:

1.      註冊相關資訊給SharePoint。

要修改那個表單/視圖? 要修改哪個列?新樣式的實現方法是哪個?

2.      新樣式的實現方法

這裡面返回新的樣式的html代碼。

 

首先說第一點。

 

(function () {    var FiledContext = {};   FiledContext.Templates = {};   FiledContext.Templates.Fields = {       //為我們要修改顏色的列,定義一個重寫樣式的方法       "Language": { "View": FiledTemplate }                   //Language是要修改顏色的列     //View表示要修改的是視圖上的顯示     //FieldTemplate 是新的樣式的實現方法   };      //註冊Context給SharePoint   SPClientTemplates.TemplateManager.RegisterTemplateOverrides(FiledContext); })();


 

第二點,返回新的樣式:

// 重寫樣式的方法function FiledTemplate(ctx) {    var Language = ctx.CurrentItem[ctx.CurrentFieldSchema.Name];    //在這裡返回新的html   switch (Language) {       case "English":           return "<spanstyle='color :#D9D919'>" + Language + "</span>";           break;       case "Korean (Korea)":           return "<span style='color :#2D882D'>" + Language +"</span>";           break;         }}


 

最終的代碼是:

(function () {    var FiledContext = {};   FiledContext.Templates = {};   FiledContext.Templates.Fields = {       //為我們要修改顏色的列,定義一個重寫樣式的方法       "Language": { "View": FiledTemplate }   };      //註冊Context給SharePoint   SPClientTemplates.TemplateManager.RegisterTemplateOverrides(FiledContext); })(); // 重寫樣式的方法function FiledTemplate(ctx) {    var Language = ctx.CurrentItem[ctx.CurrentFieldSchema.Name];    //在這裡返回新的html   switch (Language) {       case "English":           return "<span style='color :#D9D919'>" + Language +"</span>";           break;       case "Korean (Korea)":           return "<span style='color :#2D882D'>" + Language +"</span>";           break;         }}

 

把檔案儲存成ColumnColor.js,上傳到Style Library裡。其實你也可以傳到其他文件庫裡,或者是layout下面。


開啟文件庫的視圖,編輯頁面,找到文件庫視圖的web part,編輯webpart,在JSLink裡面填寫:

~sitecollection/Style Library/JSLink/ColumnColor.js


 

點OK,停止編輯頁面。 就會看到下面的效果:



代碼: http://download.csdn.net/detail/spfarm/8427007 

 

SharePoint JS Link 之(一) 改變column的顏色

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.