CRM行编辑控件

 

原创,转载请说明出处  王红福 http://www.cnblogs.com/hellohongfu/p/4792452.html

CRM 本身的表格可以根据定义显示列信息,但是出于性能考虑不能详细的展示详细信息,不能实现行编辑的功能,所以根据客户需要需要列出用户选择记录后的详细信息并且支持行编辑。 最张效果如文章是下方的图示。
解决方案:
  我们来用Javascript 前瑞来实现 ,CRM提供了REST服务供Javascript调用来获取CRM数据,我们在CRM列表页面想入我们要相的脚本。当用户选择用记录的时候执行自己的脚本去查询记录的详细信息。

代码如下:

 


 

var clientUrl = "http://172.16.30.12:5555/teachnet"


//The XRM OData end-point
var ODATA_ENDPOINT = "/XRMServices/2011/OrganizationData.svc";
function retrieveRecord(id, odataSetName, successCallback, errorCallback)
{
 
 //id is required
 if (!id)
 {
  alert("record id is required.");
  return;
 }
 else
 {
  id = encodeURIComponent(id);
 }
 //odataSetName is required, i.e. "AccountSet"
 if (!odataSetName)
 {
  alert("odataSetName is required.");
  return;
 }
 else
 { odataSetName = encodeURIComponent(odataSetName); }
console.log(clientUrl + ODATA_ENDPOINT + "/" + odataSetName + "(guid'" + id + "')");
 //Asynchronous AJAX function to Retrieve a CRM record using OData
 $.ajax({
  type: "GET",
  contentType: "application/json; charset=utf-8",
  datatype: "json",
  url: clientUrl + ODATA_ENDPOINT + "/" + odataSetName + "(guid'" + id + "')",
  beforeSend: function (XMLHttpRequest)
  {
   //Specifying this header ensures that the results will be returned as JSON.             
   XMLHttpRequest.setRequestHeader("Accept", "application/json");
  },
  success: function (data, textStatus, XmlHttpRequest)
  {
   if (successCallback)
   {
    successCallback(data.d, textStatus, XmlHttpRequest);
 
   }
  },
  error: function (XmlHttpRequest, textStatus, errorThrown)
  {
   if (errorCallback)
    errorCallback(XmlHttpRequest, textStatus, errorThrown);
   else
    errorHandler(XmlHttpRequest, textStatus, errorThrown);
  }
 });
}
 
 
 
 
 
var rowTR=$($("iframe")[0].contentWindow.document).find("#headerRowTR")
var currentDoc=$($("iframe")[0].contentWindow.document);
 
var button ="<input id='detail' onclick='detailClick()'  type='button' value='显示详细' >"

function detailClick(){

  alert('test');

}

 
 
//var td =$($("iframe")[0].contentWindow.document).find('#chartDesignerCommandBar').last();
 
 
 
currentDoc.find("#contentContainer").html(" <iframe src='http://www.baidu.com' />  ");
currentDoc.find("#headerRowTR").html('').append(button);
 
 
 
function successCallBack(data){
  console.log("successCallBack:"+data);
  var contact=data;
   
 
    var row =$("<div> FirstName : <input id='FirstName'  type='text  value='' ><br>LastName: <input id='LastName'  type='text  value='' ><br>Telephone1: <input id='Telephone1'  type='text  value='' ><br>EMailAddress1: <input id='EMailAddress1'  type='text  value='' ><br>Address1_Line1: <input id='Address1_Line1'  type='text  value='' ><br>Address1_City: <input id='Address1_City'  type='text  value='' ><br>Address1_StateOrProvince: <input id='Address1_StateOrProvince'  type='text  value='' ><br>Address1_PostalCode: <input id='Address1_PostalCode'  type='text  value='' ><br><input onclick='alert('chenggong')' value='Save' type='button'></div>");
 
  if (contact.FirstName)
                    row.find("#FirstName").attr("value", contact.FirstName);
                if (contact.LastName)
                    row.find("#LastName").attr("value", contact.LastName);
                if (contact.Telephone1)
                    row.find("#Telephone1").attr("value", contact.Telephone1);
                if (contact.EMailAddress1)
                    row.find("#EMailAddress1").attr("value", contact.EMailAddress1);
                if (contact.Address1_Line1)
                    row.find("#Address1_Line1").attr("value", contact.Address1_Line1);
                if (contact.Address1_City)
                    row.find("#Address1_City").attr("value", contact.Address1_City);
                if (contact.Address1_StateOrProvince)
                    row.find("#Address1_StateOrProvince").attr("value", contact.Address1_StateOrProvince);
                if (contact.Address1_PostalCode)
                    row.find("#Address1_PostalCode").attr("value", contact.Address1_PostalCode);
  
 
   currentDoc.find("#contentContainer").append(row);
}
function errorCallBack(data){
 console.log("errorCallBack:"+data);
}
 
function SelectedOnChange(id){
  currentDoc.find("#contentContainer").html('当前选择记录是: '+id);
  retrieveRecord(id,'ContactSet',successCallBack,errorCallBack)
}

 
var selectedId='';
function getSelectedId(){
  var id =currentDoc.find(".ms-crm-List-SelectedRow").attr('oid');
  if(id!=selectedId){
    SelectedOnChange(id);
    console.log(id);
  }
  selectedId=id;
}
 
 
setInterval("getSelectedId()",10)
 
 
最终效果
 

posted on 2015-09-08 17:45  HelloHongfu  阅读(805)  评论(0编辑  收藏  举报

导航