12 May 2010

PARENT-CHILD RELATION USING J-SCRIPT

1. List Creation:
Create two list in the web application which needs this functionality to be implemented.
  1. Category: create list named “CATEGORY “ with default Title field in it.
  2. Sub-Category: Create list named “SUB-CATEGORY” with Title & a lookup from the Category List (Title Column).
Script for Content Editor WP.
Note: For adding script get the List GUID and Default View GUID of the Sub Category List

<script type="text/javascript"> _spBodyOnLoadFunctionNames.push("PageOnLoad");
function PageOnLoad()
{ // This function will add onchange event to the Country DropDown field and call
OnChanged function. var lookupElement = GetElementByTypeAndTitle('SELECT','Category');
if ( lookupElement != null)
{
lookupElement.onchange = function()
{
OnChanged(lookupElement.options[lookupElement.selectedIndex].text)
};
} }
function OnChanged(FilterValue)
{ // siteName - the root / and possibly sub site you are working on
// lookupListName - the guid for the list you want to use as lookup
// lookupViewName - the guid for the view filtering the content of the list
// NOTE: TO DERIVE THE GUID FOR THE LIST NAME AND VIEW NAME
// Open the List -> Settings -> List Settings -> Click on "All Items" view,
// From the URL you can get the ListName GUID and ListView GUID.
// textField - the field you want to show to the user
// valueField - this is most of the time the internal ID field
// FilterField - the field you want to be a filter
// FilterValue - the filter's value var
siteName="";
var lookupListName="{90533341-04F8-4353-BBEB-D6D9A0BAAC1F}";
var lookupViewName="{C6C467A6-C04D-448D-8015-D4A622784745}";
var FilterField ="Category";
var textField ="ows_LinkTitle";
var valueField ="ows_ID";
var filterElement = GetElementByTypeAndTitle('SELECT','Sub-Category');


filterElement.innerHTML = ""; var reqstring = siteName + "/_vti_bin/owssvr.dll?CS=109&XMLDATA=1&RowLimit=0&List=" + lookupListName +"&View=" + lookupViewName;


if (FilterValue != "") reqstring= reqstring +
"&FilterField1=" + escape(FilterField) + "&FilterValue1=" + escape(FilterValue);


var req = new ActiveXObject("MSXML2.XMLHTTP"); req.open("GET",reqstring,false);
req.send();
// -- loading response in XML Document
var doc = new ActiveXObject("MSXML2.DOMDocument"); doc.loadXML(req.responseText); var data = doc.documentElement.childNodes(1);
for (i=0;i<data.childNodes.length;i++)
{
var optionText = data.childNodes(i).attributes.getNamedItem(textField).value;
var optionValue = data.childNodes(i).attributes.getNamedItem(valueField).value;
var opt = document.createElement("OPTION");


filterElement.options.add(opt);
opt.innerText = optionText;
opt.value = optionValue;
} }
function GetElementByTypeAndTitle(elementType, elementTitle)
{
//get the Element by tag name.
var allElements = document.getElementsByTagName(elementType);
for (var i = 0; i < allElements.length; i++)
{
//compare the Title.
if (allElements[i].title == elementTitle) return allElements[i];
}
return null;
}
</script>


No comments:

Post a Comment

Thanks for your valuable comments

Rate Now: