簡體   English   中英

sharepoint 2010:使用javascript的自定義列

[英]sharepoint 2010: custom column with javascript

我想知道是否可以創建一個自定義列,該列將每次獲取我今天的日期而無需更新列表中的項目?

我的最終目標是能夠計算出目標日期和今天之間的剩余時間或超出了多少時間。

我考慮過將這樣的代碼隱藏在頁面上,然后在創建計算列時以某種方式引用date div的innerHTML。

今天=新的Date();

  D = today.getDate(); M = today.getMonth() + 1; Y = today.getYear(); document.getElementById('date').innerHTML=D+"/"+M+"/"+Y; <div id="date" style="display:none"></div> 

任何人都對如何做到這一點有任何想法?

您已經說過要顯示“倒數/倒數”列以顯示日期和今天之間的天數,例如

任務X | 2月20日到期| 5天內到期

無法直接在自定義列中執行此操作,因為無法確保服務器端代碼將在頁面視圖上運行(例如,自定義字段類型CODE將不會在常規列表視圖上運行),但是您可以使用JavaScript(帶有或沒有自定義列)。

這篇文章詳細介紹了一種實現倒計時工作的4種方法,其中包括pathtosharepoint.comChristophe的兩種方法 ,聽起來很符合您的要求:

您可以在這里將其與自定義列結合起來以輸出@Ivan所示的javascript引用,也可以通過CEWP將javascript添加到頁面中。

您可以創建自己的字段類型並設置用於呈現的javascript,如下所示:

<FieldType>
...
<RenderPattern名稱=“ DisplayPattern”>
<HTML> <![CDATA [<script src =“ / _ layouts / MyDateTime.js ”> </ script>]]> </ HTML>
<HTML> <![CDATA [<div> <SCRIPT> GetCurrentTime();“]]> </ HTML>
<div id =“ date” style =“ display:none”> </ div>
<HTML> <![CDATA [“); </ SCRIPT> </ div>]]> </ HTML>
</ RenderPattern>
</ FieldType>

然后將帶有GetCurrentTime函數定義的MyDateTime.js放到layouts文件夾中,即可完成操作。 有關聲明自定義字段類型的更多詳細信息,請參見: http : //www.spsamples.com/2011/06/sharepoint-indicator-field-type.html

Include a content editor web part in the page (newform.aspx / editform.aspx) and use jQuery (or just plain JavaScript) to handle the setting of default values.

Edit: some example code:


In the lists newform.aspx, include a reference to jquery. If you look at the html code, you can see that each input tag gets an id based on the field's GUID, and a title that's set to the fields display name.


now, using jquery we can get at these fields using the jQuery selector like this:

By title:


$("input[title='DISPLAYNAMEOFFIELD']");

by id (if you know the field's internal guid, the dashes will ahve to be replaced by underscores:

// example field id, notice the guid and the underscores in the guid ctl00_m_g_054db6a0_0028_412d_bdc1_f2522ac3922e_ctl00_ctl04_ctl15_ctl00_ctl00_ctl04_ctl00_ctl00_TextField

$("input[id*='GUID']"); //this will get all input elements of which the id contains the specified GUID, i.e. 1 element


We wrap this in the ready() function of jQuery, so all calls will only be made when the document has fully loaded:


$(document).ready(function(){
 // enter code here, will be executed immediately after page has been loaded
});


By combining these 2 we could now set your dropdown's onchange event to the following


$(document).ready(function(){
 $("input[title='DISPLAYNAMEOFFIELD']").change(function() 
 {
      //do something to other field here 
 });
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM