1

我想知道是否有可能創建一個自定義列,每次都會得到我今天的日期,而無需更新列表中的項目?sharepoint 2010:自定義列與javascript

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

我想過把一個這樣的代碼隱藏在頁面上,然後以某種方式引用日期div的innerHTML,當我創建一個計算列。

today = new 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> 

任何人有關於如何可以這樣做有什麼想法?

+0

我的同事寫了一篇文章解釋我們試圖做到這一點的方法,這些方法對我們來說並不奏效 - 可能會有所幫助。 http://blog.pentalogic.net/2011/08/how-not-to-develop-a-sharepointtoday-calculated-column/ – Ryan 2012-02-14 13:37:02

+0

是的,我已經看到了這個網頁在我的搜索後解決方案。我懷疑除了購買他提供的插件外,沒有別的解決方案。特別是對於一個看起來很小的問題。 – Jake 2012-02-14 23:17:03

+0

那麼我會等着你告訴我他是怎麼錯的,特別是對於那麼微小的事情;)一個想法 - 爲什麼不編輯你的q並告訴我們你想要達到的目標,而不是你想要做什麼 - 我想我可以猜測但不確定 - 那麼可能會有一種替代方法來處理這個問題。 – Ryan 2012-02-15 08:37:45

回答

3

你說你想顯示倒計時/正數列中顯示的日期和當天的日期,例如之間的天數

任務X | 2月20日截止|直接在自定義列5天后到期

不能做到這一點,因爲你不能保證服務器端代碼將在頁面視圖上運行(例如,您的自定義字段類型的代碼將無法在運行正常列表視圖),但您可以使用JavaScript(帶或不帶自定義列)。

post details 4 ways to get a countdown field working其中包括兩個由Christophe of pathtosharepoint.com名氣聽起來就像它符合您的要求: -

你可以採取的想法在這裏有一個自定義的列結合起來輸出@Ivan所示的javascript refs,或者您可以通過CEWP將JavaScript添加到頁面。

+0

)謝謝你,我認爲你指出了我的正確方向,我迫不及待想要嘗試它! – Jake 2012-02-15 18:09:56

2

您可以創建自己的字段類型和設置的JavaScript渲染,就像這樣:

<的FieldType >
...
< RenderPattern NAME = 「DisplayPattern」 >
<HTML> <! [CDATA [<腳本 src =「/ _ layouts/MyDateTime.js」> </script >]]> </HTML >
<HTML> < [CDATA [<DIV> <SCRIPT> GetCurrentTime(); 「]] > </HTML >
< DIV ID =」 日期 「風格=」 顯示!: none「> </div >
<HTML> <![CDATA [」);「 </SCRIPT > </DIV >]]> </HTML >
</RenderPattern >
< /的FieldType >

然後把與GetCurrentTime函數定義MyDateTime.js到佈局的文件夾,即可大功告成。有關聲明的自定義字段類型的詳細信息,請參閱:http://www.spsamples.com/2011/06/sharepoint-indicator-field-type.html

+0

您的答案似乎很棒我唯一遇到的問題是我在哪裏編寫自定義字段類型?我需要使用Visual Studio嗎?或SP設計師就夠了?如果我的問題看起來很愚蠢,我很抱歉。這只是我還沒有任何經驗,你剛纔告訴我的方法。請您善意闡述如何使用它?謝謝。 – Jake 2012-02-13 18:04:54

+0

要創建自定義字段類型,您需要編寫一個特殊的xml文件,即使在記事本中也可以執行此操作。一旦你爲你的字段類型寫了標記,你應該像fldtypes_MyFieldType.xml一樣命名(從fldtypes_開始是強制性的 - 這就是SharePoint知道這是字段類型定義的方式)並放入Program Files \ Common Files \ Microsoft Shared \ Web服務器擴展\ 14 \ TEMPLATE \ XML目錄。運行iisreset,SharePoint將解析您的新類型,並且可以使用它。以字段類型標記爲例,請參閱此文章:http://msdn.microsoft.com/en-us/library/gg132914.aspx – 2012-02-14 06:07:58

+0

請注意,您不必實現自定義字段類,因爲對於您而言,它是足以繼承標準字段類型(例如文本字段) – 2012-02-14 06:09:01

2
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 
}); 
}); 
+0

設置默認值 - 這如何解決OP的問題? – Ryan 2012-02-14 13:35:36

+0

我想我已經在這裏看到了類似的方法:http://sympmarc.com/2009/05/20/pre-filling-column-values-in-a-sharepoint-form/雖然我嘗試過,但沒有( – Jake 2012-02-14 23:08:31