[译]使用JavaScript来操纵数据视图DataView新建视图的默认值
觉得这篇文章比较有参考价值,把大意翻译过来,没有忠实于原文。
原文链接:http://blogs.msdn.com/sharepointdesigner/archive/2007/06/13/using-javascript-to-manipulate-a-list-form-field.aspx
为什么要这样做?
很多人抱怨经过SharePoint Designer设计的数据视图不够智能化,其中之一就是:SPD没有办法来设定字段的默认值,当然我们可以通过SharePoint中的WEB界面来设定某个列表中的字段的默认值,但是那样做的话,只能设置固定的值。比如我想设计一个表单,这个表单有一栏叫做“审批人”,系统根据用户在前一个页面的选择来产生的一个URL查询参数。
比如http://www.server.com/page.aspx?RequestName=RequestValue,这样来设置“审批人”的值为RequestValue。
机制:
首先,找到这个字段的HTML控件的ID,然后对这个控件进行设置。SharePoint对视图中的字段的渲染的特点如下表:
此外,最重要的是,字段的Title属性等于字段的“友好名称”,比如第一段提到的,“审批人”系统在渲染这个控件时,这个HTML的控件就TITLE就为“审批人”。
然后,再设置这个控件的值为URL查询参数的值。
特别:对于查阅项如果超过20个,那么Select标记会被系统内的INPUT标记包复,应先使用DropDown这个方法把包复的控件展开。
把文中的
<<QUERYSTRING VARIABLE NAME>> 换你传递的查询字串,
<<FIELD DISPLAY NAME>>换成你字段的中英文名称就OK了。
然后 通过SPD把下面的代码插入你页面的HTML中去。
<script type="text/javascript">
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
// This javascript sets the default value of a lookup field identified
![](/Images/OutliningIndicators/None.gif)
// by <<FIELD DISPLAY NAME>> to the value stored in the querysting variable
![](/Images/OutliningIndicators/None.gif)
// identified by <<QUERYSTRING VARIABLE NAME>>
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
// Customize this javascript by replacing <<FIELD DISPLAY NAME>> and
![](/Images/OutliningIndicators/None.gif)
// <<QUERYSTRING VARIABLE NAME>> with appropriate values.
![](/Images/OutliningIndicators/None.gif)
// Then just paste it into NewForm.aspx inside PlaceHolderMain
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
_spBodyOnLoadFunctionNames.push("fillDefaultValues");
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
function fillDefaultValues() {
![](/Images/OutliningIndicators/InBlock.gif)
var qs = location.search.substring(1, location.search.length);
![](/Images/OutliningIndicators/InBlock.gif)
var args = qs.split("&");
![](/Images/OutliningIndicators/InBlock.gif)
var vals = new Object();
![](/Images/OutliningIndicators/InBlock.gif)
for (var i=0; i < args.length; i++) {
![](/Images/OutliningIndicators/InBlock.gif)
var nameVal = args[i].split("=");
![](/Images/OutliningIndicators/InBlock.gif)
var temp = unescape(nameVal[1]).split('+');
![](/Images/OutliningIndicators/InBlock.gif)
nameVal[1] = temp.join(' ');
![](/Images/OutliningIndicators/InBlock.gif)
vals[nameVal[0]] = nameVal[1];
![](/Images/OutliningIndicators/InBlock.gif)
}
![](/Images/OutliningIndicators/InBlock.gif)
setLookupFromFieldName("<<FIELD DISPLAY NAME>>", vals["<<QUERYSTRING VARIABLE NAME>>"]);
![](/Images/OutliningIndicators/InBlock.gif)
}
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
function setLookupFromFieldName(fieldName, value) {
![](/Images/OutliningIndicators/InBlock.gif)
if (value == undefined) return;
![](/Images/OutliningIndicators/InBlock.gif)
var theSelect = getTagFromIdentifierAndTitle("select","Lookup",fieldName);
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
// if theSelect is null, it means that the target list has more than
![](/Images/OutliningIndicators/InBlock.gif)
// 20 items, and the Lookup is being rendered with an input element
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
if (theSelect == null) {
![](/Images/OutliningIndicators/InBlock.gif)
var theInput = getTagFromIdentifierAndTitle("input","",fieldName);
![](/Images/OutliningIndicators/InBlock.gif)
ShowDropdown(theInput.id); //this function is provided by SharePoint
![](/Images/OutliningIndicators/InBlock.gif)
var opt=document.getElementById(theInput.opt);
![](/Images/OutliningIndicators/InBlock.gif)
setSelectedOption(opt, value);
![](/Images/OutliningIndicators/InBlock.gif)
OptLoseFocus(opt); //this function is provided by SharePoint
![](/Images/OutliningIndicators/InBlock.gif)
} else {
![](/Images/OutliningIndicators/InBlock.gif)
setSelectedOption(theSelect, value);
![](/Images/OutliningIndicators/InBlock.gif)
}
![](/Images/OutliningIndicators/InBlock.gif)
}
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
function setSelectedOption(select, value) {
![](/Images/OutliningIndicators/InBlock.gif)
var opts = select.options;
![](/Images/OutliningIndicators/InBlock.gif)
var l = opts.length;
![](/Images/OutliningIndicators/InBlock.gif)
if (select == null) return;
![](/Images/OutliningIndicators/InBlock.gif)
for (var i=0; i < l; i++) {
![](/Images/OutliningIndicators/InBlock.gif)
if (opts[i].value == value) {
![](/Images/OutliningIndicators/InBlock.gif)
select.selectedIndex = i;
![](/Images/OutliningIndicators/InBlock.gif)
return true;
![](/Images/OutliningIndicators/InBlock.gif)
}
![](/Images/OutliningIndicators/InBlock.gif)
}
![](/Images/OutliningIndicators/InBlock.gif)
return false;
![](/Images/OutliningIndicators/InBlock.gif)
}
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
function getTagFromIdentifierAndTitle(tagName, identifier, title) {
![](/Images/OutliningIndicators/InBlock.gif)
var len = identifier.length;
![](/Images/OutliningIndicators/InBlock.gif)
var tags = document.getElementsByTagName(tagName);
![](/Images/OutliningIndicators/InBlock.gif)
for (var i=0; i < tags.length; i++) {
![](/Images/OutliningIndicators/InBlock.gif)
var tempString = tags[i].id;
![](/Images/OutliningIndicators/InBlock.gif)
if (tags[i].title == title && (identifier == "" || tempString.indexOf(identifier) == tempString.length - len)) {
![](/Images/OutliningIndicators/InBlock.gif)
return tags[i];
![](/Images/OutliningIndicators/InBlock.gif)
}
![](/Images/OutliningIndicators/InBlock.gif)
}
![](/Images/OutliningIndicators/InBlock.gif)
return null;
![](/Images/OutliningIndicators/InBlock.gif)
}
![](/Images/OutliningIndicators/None.gif)
</script>
原文链接:http://blogs.msdn.com/sharepointdesigner/archive/2007/06/13/using-javascript-to-manipulate-a-list-form-field.aspx
为什么要这样做?
很多人抱怨经过SharePoint Designer设计的数据视图不够智能化,其中之一就是:SPD没有办法来设定字段的默认值,当然我们可以通过SharePoint中的WEB界面来设定某个列表中的字段的默认值,但是那样做的话,只能设置固定的值。比如我想设计一个表单,这个表单有一栏叫做“审批人”,系统根据用户在前一个页面的选择来产生的一个URL查询参数。
比如http://www.server.com/page.aspx?RequestName=RequestValue,这样来设置“审批人”的值为RequestValue。
机制:
首先,找到这个字段的HTML控件的ID,然后对这个控件进行设置。SharePoint对视图中的字段的渲染的特点如下表:
字段类型 | ID属性中包括的字串 | 使用的HTML标记 |
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 |
此外,最重要的是,字段的Title属性等于字段的“友好名称”,比如第一段提到的,“审批人”系统在渲染这个控件时,这个HTML的控件就TITLE就为“审批人”。
然后,再设置这个控件的值为URL查询参数的值。
特别:对于查阅项如果超过20个,那么Select标记会被系统内的INPUT标记包复,应先使用DropDown这个方法把包复的控件展开。
把文中的
<<QUERYSTRING VARIABLE NAME>>
<<FIELD DISPLAY NAME>>换成你字段的中英文名称就OK了。
然后 通过SPD把下面的代码插入你页面的HTML中去。
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)