在SharePoint 2013中,SPField新增加了一個屬性是JSLink,使用客戶端腳本修改字段前台展示,我們可以用很多方法修改這個腳本的引用,然后來修改腳本,下面,我們舉一個簡單的例子。
具體過程
A. 創建一個欄 -> B.使用工具修改JSLink的默認值 -> C.寫JSLink的腳本
1、在新列表,創建一個字段PicUrl,如下圖:
2、在layouts下新建一個文件夾,里面放JSLink.js(名字可以隨便取);
3、使用SharePoint Manager 2013,找到相應字段修改其JSLink屬性,如下圖:
4、JSLink.js內容及介紹,如下圖:
重點就是下面的JS如何寫,模板建議大家不要動,重寫下面畫圈的方法即可。注意方框部分里面是字段名稱,不要寫錯,就可以。
個人試想這里面還可以寫復雜一點的腳本,但是沒有試過,待以后需要的時候嘗試一下,留個博客,方便以后查找,呵呵。
1 // Create a namespace for our functions so we don't collide with anything else 2 var PicUrlReWrite= PicUrlReWrite|| {}; 3 4 // Create a function for customizing the Field Rendering of our fields 5 PicUrlReWrite.CustomizeFieldRendering = function () 6 { 7 var fieldJsLinkOverride = {}; 8 fieldJsLinkOverride.Templates = {}; 9 fieldJsLinkOverride.Templates.Fields = 10 { 11 // Make sure the Priority field view gets hooked up to the GetPriorityFieldIcon method defined below 12 'PicUrl': { 'View': PicUrlReWrite.ReWriteFieldValue } 13 }; 14 15 // Register the rendering template 16 SPClientTemplates.TemplateManager.RegisterTemplateOverrides(fieldJsLinkOverride); 17 }; 18 19 // Create a function for getting the Priority Field Icon value (called from the first method) 20 PicUrlReWrite.ReWriteFieldValue = function (ctx) { 21 var PicUrl = ctx.CurrentItem.PicUrl; 22 23 // In the following section we simply determine what the rendered html output should be. In my case I'm setting an icon. 24 25 return "<img src='"+ PicUrl +"' width='700' height='300'></img>"; 26 27 }; 28 29 // Call the function. 30 31 // We could've used a self-executing function as well but I think this simplifies the example 32 33 PicUrlReWrite.CustomizeFieldRendering();
5、新建一條數據,如下圖所示:
6、默認展示效果,如下圖:
7、查看修改JSLink后展示,如下圖:
本文是參考如下博客做的測試,測試過程中遇到點問題,想了很久又查了資料,才發現js應該怎么寫,所以拿出來說一下,如有需要,參考后面鏈接。
附鏈接
http://blog.csdn.net/abrahamcheng/article/details/12090265







