博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

Dynamics CRM 2011 Lookup类型字段实现多选及保存、显示

Posted on 2013-03-05 17:31  RenShao  阅读(712)  评论(0编辑  收藏  举报

1.把lookup类型的字段单选变为多选代码:

document.getElementById("new_account").setAttribute("lookupstyle", "multi");

2.需要新建一个文本字段来存储多选产生的值,因为Lookup虽然能实现多选,但不能保存多个选项的值。保存后通过JS把多选的值以XML形式存入文本,显示时再从文本取值处理后给Lookup赋值。

//实例:多选Lookup字段new_account 、文本类型字段new_txtxml
function formOnload() {
 document.getElementById("new_account").setAttribute("lookupstyle", "multi");
 getarrfromxml();    
}
function formOnsave() {    
    PartyListSave("new_account", "new_txtxml");
  }
//解析xml为数组,赋值给lookup类型字段
function getarrfromxml() {   
    if (Xrm.Page.ui.getFormType() != 1) {
        var contentXml = Xrm.Page.getAttribute("new_txtxml").getValue();
        if (contentXml != null) {
            var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
            xmlDoc.loadXML(contentXml);
            var value = new Array();
            var nodeList = xmlDoc.getElementsByTagName("li");
            for (var i = 0; i < nodeList.length; i++) {
                value[i] = new Object();
                value[i].id = nodeList[i].getAttribute("oid");
                value[i].name = nodeList[i].getAttribute("title");
                value[i].typename = nodeList[i].getAttribute("otypename");
            }
            crmForm.all["new_account"].DataValue = value;
        }
    }
}
function PartyListSave(textControl, multiTextControl) {
    var url = window.location.host;
    var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
    xmlDoc.loadXML("<r></r>");
    var root = xmlDoc.firstChild;
    var tbl = document.getElementById(textControl + "_d").firstChild;
    var div = tbl.rows[0].cells[0].firstChild;
    var ul = div.childNodes[0];
    var targetObject = Xrm.Page.getAttribute(textControl);
    var targetObjectContent = Xrm.Page.getAttribute(multiTextControl);
    root.setAttribute("tabIndex", div.tabIndex);
    for (var i = 0; i < ul.childNodes.length; i++) {
        var uNode = root.appendChild(xmlDoc.createElement("li"));
        uNode.setAttribute("title", ul.childNodes[i].firstChild.getAttribute("title"));
        uNode.setAttribute("otypename", ul.childNodes[i].firstChild.getAttribute("otypename"));
        uNode.setAttribute("otype", ul.childNodes[i].firstChild.getAttribute("otype"));
        uNode.setAttribute("oid", ul.childNodes[i].firstChild.getAttribute("oid"));
        uNode.setAttribute("src", ul.childNodes[i].firstChild.firstChild.firstChild.src.replace("http://" + url, ""));
    }
    targetObjectContent.setValue(xmlDoc.xml);
    targetObject.setValue(null);
}