想做的事永远不会太晚
It's never too late to be what you might have been (George Eliot)
每个人都想改变人性 但是没有人想着改变自己
Everybody wants to change humanity but nobody thinks about changing himself (Leo Tolstoj)
把脸面对阳光 你将见不到阴影
Keep yur face to the sunshine,and you cannot see shadows (HelenKeller)
你所仅有的就是你自己 不容退让
Don't compromise yourself You are all you've got (Janis Joplin)
我们不记得日子 只记得那些时刻
We don't remember days,just moments (Cesare Pavese)
我们决心要有多快乐就有多快乐
We do as happy as we make up our minds to be (Arbraham Lincoln)
人生苦短 但总有时间善待别人
Life is not so short but that there is always time enough for courtesy (Ralph Waldo Emerson)
你在哪里 我就去哪里
Where you are I want to be (Anne Linnet)
所有的限制来自于自限
All limitations are self-imposed (W.Mitchell)
永远叹气不如痛哭一次
It is better to cry once that to sigh for ever (Proverb)
不为旧错流新泪
Waste not fresh tears over old griefs (Euripides)
2009年2月19日星期四
2008年12月30日星期二
Using Javascript to Manipulate a List Form Field
I am programing an Absence Request application in my company sharepoint just now, and i got a problem with how to validate two fields on a new list item form, then i serch by google and get a good solution that invoking JavaScript custom function. i plan to post that bolg helped me here for reviewing:
If you’re familiar with them, you may have noticed that in several of the application templates we use a bit of Javascript to set default form values based on the query string. Because we found this to be useful in many different cases throughout our applications, I wanted to share our method with you guys so that you can include it in the applications you develop.
When might you use this?
It’s pretty easy to set a field’s default value through the list settings in the browser UI, so why might you need Javascript to set a default field value? The reason is that field default values can only take static values or simple formulae based on the current user or today’s date. If that meets your needs, then I’d definitely recommend sticking with that method. Sometimes, though, you may want the form to fill with default values based on the user’s interaction with the previous page, and that’s exactly where this method comes in.
How does it work?
In short, we add some Javascript to the page that runs when the body is loaded. This Javascript parses the page’s query string, locates the HTML objects that are rendered by the relevant SharePoint fields, and sets their value based on the information in the query string.
getTagFromIdentifierAndTitle
The most important part of our solution is the “getTagFromIdentifier” function. This function finds the HTML element rendered by a given SharePoint FormField control. It takes the following parameters:
*tagName – The name of the tag rendered in the form’s HTML
*identifier – The string associated with the SharePoint type of the relevant field
*title – The value of the relevant HTML tag’s “title” attribute, which also matches the field’s display name
Here’s a partial table of SharePoint column types and their corresponding “identifiers” and “tagNames”:
SharePoint-Field-Type----identifier--------tagName-
Single-Line-of-Text------TextField---------input-
Multiple-Lines-of-Text---TextField---------input-
Number-------------------TextField---------input-
Currency-----------------TextField---------input-
Choice-(dropdown)--------DropDownChoice----select-
Lookup-(single)*---------Lookup------------select-
Lookup-(multiple)--------SelectCandidate;--SelectResult-select-
Yes/No-------------------BooleanField------input-
*Lookups are a bit more complicated because Lookup FormFields render differently when the target list contains more than 20 items. See the end of the post for an example.
function getTagFromIdentifierAndTitle(tagName, identifier, title) {
var len = identifier.length;
var tags = document.getElementsByTagName(tagName);
for (var i=0; i < tempstring =" tags[i].id;" title ="=" identifier ="=" qs =" location.search.substring(1," args =" qs.split(" vals =" new" i="0;" nameval =" args[i].split(" temp =" unescape(nameVal[1]).split('+');" type="text/javascript">> to the value stored in the querysting variable
// identified by <>
// Customize this javascript by replacing <> and
// <> with appropriate values.
// Then just paste it into NewForm.aspx inside PlaceHolderMain
_spBodyOnLoadFunctionNames.push("fillDefaultValues");
function fillDefaultValues() {
var qs = location.search.substring(1, location.search.length);
var args = qs.split("&");
var vals = new Object();
for (var i=0; i < nameval =" args[i].split(" temp =" unescape(nameVal[1]).split('+');">>", vals["<>"]);
}
function setLookupFromFieldName(fieldName, value) {
if (value == undefined) return;
var theSelect = getTagFromIdentifierAndTitle("select","Lookup",fieldName);
// if theSelect is null, it means that the target list has more than
// 20 items, and the Lookup is being rendered with an input element
if (theSelect == null) {
var theInput = getTagFromIdentifierAndTitle("input","",fieldName);
ShowDropdown(theInput.id); //this function is provided by SharePoint
var opt=document.getElementById(theInput.opt);
setSelectedOption(opt, value);
OptLoseFocus(opt); //this function is provided by SharePoint
} else {
setSelectedOption(theSelect, value);
}
}
function setSelectedOption(select, value) {
var opts = select.options;
var l = opts.length;
if (select == null) return;
for (var i=0; i < l; i++) {
if (opts[i].value == value) {
select.selectedIndex = i;
return true;
}
}
return false;
}
function getTagFromIdentifierAndTitle(tagName, identifier, title) {
var len = identifier.length;
var tags = document.getElementsByTagName(tagName);
for (var i=0; i < tags.length; i++) {
var tempString = tags[i].id;
if (tags[i].title == title && (identifier == "" tempString.indexOf(identifier) == tempString.length - len)) {
return tags[i];
}
}
return null;
}
[/script]
If you’re familiar with them, you may have noticed that in several of the application templates we use a bit of Javascript to set default form values based on the query string. Because we found this to be useful in many different cases throughout our applications, I wanted to share our method with you guys so that you can include it in the applications you develop.
When might you use this?
It’s pretty easy to set a field’s default value through the list settings in the browser UI, so why might you need Javascript to set a default field value? The reason is that field default values can only take static values or simple formulae based on the current user or today’s date. If that meets your needs, then I’d definitely recommend sticking with that method. Sometimes, though, you may want the form to fill with default values based on the user’s interaction with the previous page, and that’s exactly where this method comes in.
How does it work?
In short, we add some Javascript to the page that runs when the body is loaded. This Javascript parses the page’s query string, locates the HTML objects that are rendered by the relevant SharePoint fields, and sets their value based on the information in the query string.
getTagFromIdentifierAndTitle
The most important part of our solution is the “getTagFromIdentifier” function. This function finds the HTML element rendered by a given SharePoint FormField control. It takes the following parameters:
*tagName – The name of the tag rendered in the form’s HTML
*identifier – The string associated with the SharePoint type of the relevant field
*title – The value of the relevant HTML tag’s “title” attribute, which also matches the field’s display name
Here’s a partial table of SharePoint column types and their corresponding “identifiers” and “tagNames”:
SharePoint-Field-Type----identifier--------tagName-
Single-Line-of-Text------TextField---------input-
Multiple-Lines-of-Text---TextField---------input-
Number-------------------TextField---------input-
Currency-----------------TextField---------input-
Choice-(dropdown)--------DropDownChoice----select-
Lookup-(single)*---------Lookup------------select-
Lookup-(multiple)--------SelectCandidate;--SelectResult-select-
Yes/No-------------------BooleanField------input-
*Lookups are a bit more complicated because Lookup FormFields render differently when the target list contains more than 20 items. See the end of the post for an example.
function getTagFromIdentifierAndTitle(tagName, identifier, title) {
var len = identifier.length;
var tags = document.getElementsByTagName(tagName);
for (var i=0; i < tempstring =" tags[i].id;" title ="=" identifier ="=" qs =" location.search.substring(1," args =" qs.split(" vals =" new" i="0;" nameval =" args[i].split(" temp =" unescape(nameVal[1]).split('+');" type="text/javascript">> to the value stored in the querysting variable
// identified by <
// Customize this javascript by replacing <
// <
// Then just paste it into NewForm.aspx inside PlaceHolderMain
_spBodyOnLoadFunctionNames.push("fillDefaultValues");
function fillDefaultValues() {
var qs = location.search.substring(1, location.search.length);
var args = qs.split("&");
var vals = new Object();
for (var i=0; i < nameval =" args[i].split(" temp =" unescape(nameVal[1]).split('+');">>", vals["<
}
function setLookupFromFieldName(fieldName, value) {
if (value == undefined) return;
var theSelect = getTagFromIdentifierAndTitle("select","Lookup",fieldName);
// if theSelect is null, it means that the target list has more than
// 20 items, and the Lookup is being rendered with an input element
if (theSelect == null) {
var theInput = getTagFromIdentifierAndTitle("input","",fieldName);
ShowDropdown(theInput.id); //this function is provided by SharePoint
var opt=document.getElementById(theInput.opt);
setSelectedOption(opt, value);
OptLoseFocus(opt); //this function is provided by SharePoint
} else {
setSelectedOption(theSelect, value);
}
}
function setSelectedOption(select, value) {
var opts = select.options;
var l = opts.length;
if (select == null) return;
for (var i=0; i < l; i++) {
if (opts[i].value == value) {
select.selectedIndex = i;
return true;
}
}
return false;
}
function getTagFromIdentifierAndTitle(tagName, identifier, title) {
var len = identifier.length;
var tags = document.getElementsByTagName(tagName);
for (var i=0; i < tags.length; i++) {
var tempString = tags[i].id;
if (tags[i].title == title && (identifier == "" tempString.indexOf(identifier) == tempString.length - len)) {
return tags[i];
}
}
return null;
}
[/script]
2008年12月29日星期一
Add JavaScript Date Validation into List Item forms
//--验证date有效性,end date必须等于或者大于start date
function PreSaveAction()
{
var date1 = getTagFromIdentifierAndTitle("INPUT","DateTimeFieldDate","Start Date");
var date2 = getTagFromIdentifierAndTitle("INPUT","DateTimeFieldDate","End Date");
var arrDate1 = date1.value.split("/");
var useDate1 = new Date(arrDate1[2], arrDate1[1]-1, arrDate1[0]);
var arrDate2 = date2.value.split("/");
var useDate2 = new Date(arrDate2[2], arrDate2[1]-1, arrDate2[0]);
if(useDate1 > useDate2)
{
alert("The End Date cannot happen earlier than the Start Date");
return false; // Cancel the item save process
}
return true; // OK to proceed with the save item
}
reference: http://edinkapic.blogspot.com/2007/10/add-javascript-date-validation-into.html
function PreSaveAction()
{
var date1 = getTagFromIdentifierAndTitle("INPUT","DateTimeFieldDate","Start Date");
var date2 = getTagFromIdentifierAndTitle("INPUT","DateTimeFieldDate","End Date");
var arrDate1 = date1.value.split("/");
var useDate1 = new Date(arrDate1[2], arrDate1[1]-1, arrDate1[0]);
var arrDate2 = date2.value.split("/");
var useDate2 = new Date(arrDate2[2], arrDate2[1]-1, arrDate2[0]);
if(useDate1 > useDate2)
{
alert("The End Date cannot happen earlier than the Start Date");
return false; // Cancel the item save process
}
return true; // OK to proceed with the save item
}
reference: http://edinkapic.blogspot.com/2007/10/add-javascript-date-validation-into.html
2008年4月19日星期六
Today's Sentences - I'll get back to you later.
I'll get back to you later. (我晚点再告诉你.)
例如有人下午要上台作报告, 早上他就会说 "I have to get my stuff together." 或是 "I have to organize my stuff."
We are trying to get rid of the mice in this house. 我们试著要把屋里的老鼠除掉.
Get rid of 指的是除去某些另人很讨厌的东西, 或是很讨厌的人. 例如有人很烦, 你就可以说,"I will get rid of him."
通常一样东西原来不是你的, 而你获得了这样东西, 就要用 get. 例如你得到一样生日礼物, 你就可以说, "I got a present." 或是你收到一封信, 这封信在你收到之前都不是你的, 所以你也可以用 get 当动词, 像是, "I got a letter from my sister." (我收到我姐寄来的一封信.) 同样的, 就"拿一张票"这件事来讲, 这张票原来也不是你的, 所以你就要说, "I got a ticket."
至于 take 和 bring 的意思是蛮接近的, 都是指去拿一件原来已经是属于你的东西, 但通常 take 是指"拿去", bring 指"拿来". 例如你说, "I will go home to take your ticket." 就是说这张票你可能早就买好了, 你只不过要回家去拿而已. 同样的, 你说 "I will bring you the ticket." 也是指这张票原来是你的, 你只不过是带来给人家而已.
最后补充一点, 另外还有一个字 fetch 它指的是指拿去又拿来, 例如你丢东西给狗去捡, 狗跑过去再把它捡回来这就是 fetch. 或是你去机场把人给接回来也可以用 fetch. 注意一下这几个字之间的区别.
例如有人下午要上台作报告, 早上他就会说 "I have to get my stuff together." 或是 "I have to organize my stuff."
We are trying to get rid of the mice in this house. 我们试著要把屋里的老鼠除掉.
Get rid of 指的是除去某些另人很讨厌的东西, 或是很讨厌的人. 例如有人很烦, 你就可以说,"I will get rid of him."
通常一样东西原来不是你的, 而你获得了这样东西, 就要用 get. 例如你得到一样生日礼物, 你就可以说, "I got a present." 或是你收到一封信, 这封信在你收到之前都不是你的, 所以你也可以用 get 当动词, 像是, "I got a letter from my sister." (我收到我姐寄来的一封信.) 同样的, 就"拿一张票"这件事来讲, 这张票原来也不是你的, 所以你就要说, "I got a ticket."
至于 take 和 bring 的意思是蛮接近的, 都是指去拿一件原来已经是属于你的东西, 但通常 take 是指"拿去", bring 指"拿来". 例如你说, "I will go home to take your ticket." 就是说这张票你可能早就买好了, 你只不过要回家去拿而已. 同样的, 你说 "I will bring you the ticket." 也是指这张票原来是你的, 你只不过是带来给人家而已.
最后补充一点, 另外还有一个字 fetch 它指的是指拿去又拿来, 例如你丢东西给狗去捡, 狗跑过去再把它捡回来这就是 fetch. 或是你去机场把人给接回来也可以用 fetch. 注意一下这几个字之间的区别.
订阅:
评论 (Atom)