Ihave been working on populating a field with items from another list depending on what I pick in the previous field and have got a bit further on. I now have a list where one of the lookup fields fills depending on what you have picked in the previous field.
Here is the code that I am using: note that I use a button to call the script as I put this in the editform and if I leave the code on the onblur event it will remove any existing data that I have in the filtered lookup that may already be correct.
<script>
function RepopulateList(fieldValue)
{
// — retrieve the list for the elements
var siteName = "http://www.xyz.com/site";
//the next field is the name of the field that you want to appear as filtered
var controlName="urn:schemas-microsoft-com:office:office#Control";
//this is the list GUID of the second list that you are picking up the filtered data from
var lookupListName = "{99E28AFE-5643-4F69-AF52-DE3DB3D563D2}";
//this is theGUID of the view in that second list – create a view that has ID and the items you wish to filter (in that order)
var lookupViewName = "{1D516144-CA30-4BCD-A742-E8F2BB8EA863}";
var listEl = document.getElementsByName(controlName)
// — emptying the field that you want to filter
if(listEl.length>0)
{
listEl(0).innerHTML = "";
// — getting the filtered lookup filterField is the name of the other field that the filtered list is dependant on (i.e. mine picks controls that are relevant to the workstream I have selected)
var filterField = "Workstream";
var filterValue = fieldValue;
var reqstring = siteName + "/_vti_bin/owssvr.dll?CS=109&XMLDATA=1&RowLimit=0&List=" + lookupListName + "&View=" + lookupViewName +"&FilterField1=" +filterField + "&FilterValue1=" + 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);
//this loops through the items picked up from the list and puts them in the filtered dropdown
for (i=0;i<data.childNodes.length;i++)
{
var optionText = data.childNodes(i).attributes(1).value;
var optionValue = data.childNodes(i).attributes(0).value;
var opt = document.createElement("OPTION");
listEl(0).options.add(opt);
opt.innerText = optionText;
opt.value = optionValue;
}
}
}
</script>
<script language="javascript">
var workstream1 = document.forms[0].elements["urn:schemas-microsoft-com:office:office#Workstream"];
function getCA_onclick()
{
alert("The Control dropdown will now be repopulated, please select the correct one");
var chosen = workstream1.options[workstream1.selectedIndex].text;
RepopulateList(chosen);
}
</script>