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