SharePoint 2013 字段屬性之JSLink


  在SharePoint 2013中,SPField新增加了一個屬性是JSLink,使用客戶端腳本修改字段前台展示,我們可以用很多方法修改這個腳本的引用,然后來修改腳本,下面,我們舉一個簡單的例子。

具體過程

  A. 創建一個欄 -> B.使用工具修改JSLink的默認值 -> C.寫JSLink的腳本

  1、在新列表,創建一個字段PicUrl,如下圖:

clip_image001

  2、在layouts下新建一個文件夾,里面放JSLink.js(名字可以隨便取);

clip_image002

  3、使用SharePoint Manager 2013,找到相應字段修改其JSLink屬性,如下圖:

clip_image003

  4、JSLink.js內容及介紹,如下圖:

  重點就是下面的JS如何寫,模板建議大家不要動,重寫下面畫圈的方法即可。注意方框部分里面是字段名稱,不要寫錯,就可以。

  個人試想這里面還可以寫復雜一點的腳本,但是沒有試過,待以后需要的時候嘗試一下,留個博客,方便以后查找,呵呵。

clip_image004

 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();
View Code

  5、新建一條數據,如下圖所示:

clip_image005

  6、默認展示效果,如下圖:

clip_image006

  7、查看修改JSLink后展示,如下圖:

clip_image007

  本文是參考如下博客做的測試,測試過程中遇到點問題,想了很久又查了資料,才發現js應該怎么寫,所以拿出來說一下,如有需要,參考后面鏈接。

  附鏈接

  http://blog.csdn.net/abrahamcheng/article/details/12090265


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM