我想知道是否可以创建一个自定义列,每次都可以获取今天的日期,而无需更新列表中的项目?
我的最终目标是能够计算目的地日期和今天日期之间剩余或超过的时间。
我考虑过在页面上隐藏这样的代码然后在创建计算列时以某种方式引用日期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 :(得分:3)
您已经说过要显示倒计时/倒数列,以显示日期和今天之间的天数,例如
任务X |截至2月20日|到期5天
您无法直接在自定义列中执行此操作,因为您无法确保服务器端代码将在页面视图上运行(例如,您的自定义字段类型CODE将无法在正常列表中运行视图),但你可以使用JavaScript(有或没有自定义列)。
此post details 4 ways to get a countdown field working包含两个Christophe of pathtosharepoint.com名称,听起来符合您的要求: -
你可以把这个想法与自定义列结合起来输出javascript引用,如@Ivan所示,或者你只需通过CEWP将javascript添加到页面。
答案 1 :(得分:2)
您可以创建自己的字段类型并设置javascript以进行渲染,如下所示:
&lt; FieldType&gt;
...
&lt; RenderPattern Name =“DisplayPattern”&gt;
&lt; HTML&gt;&lt;![CDATA [&lt; script SRC = “/ _布局/的 MyDateTime.js 强>” &GT;&LT; /脚本&GT;]]&GT;&LT; / HTML&GT;
&LT; HTML&GT;&LT;![CDATA并[d DIV&GT;&LT; SCRIPT&GT; GetCurrentTime();“]]&GT;&LT; / HTML&GT;
&lt; div id =“date”style =“display:none”&gt;&lt; / div&gt;
&LT; HTML&GT;&LT;![CDATA [“);&LT; / SCRIPT&GT;&LT; / DIV&GT;]]&GT;&LT; / HTML&GT;
&lt; / RenderPattern&gt;
&lt; / FieldType&gt;
然后将带有GetCurrentTime函数定义的MyDateTime.js放入layouts文件夹中,然后就完成了。有关声明自定义字段类型的详细信息,请参阅:http://www.spsamples.com/2011/06/sharepoint-indicator-field-type.html
答案 2 :(得分: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
});
});