Using JavaScript to auto-fill a repeating section but a filtered lookup control isn't filling...

  • 22 June 2020
  • 0 replies
  • 22 views

Badge +4

I am using JavaScript to update a repeating section by referencing another list by following a great blog that can be found here.  I will also attach my code that is very similar to the blog post:

NWF.FormFiller.Events.RegisterAfterReady(function () { NWF$(document).ready(function () { NWF$("#" + varDropdown).change(function () { ClearRSData(); //clear repeating section GetRSDataFromLookup(); //get repeating section data }); }); }); function ClearRSData() { var rsdata = NWF$('.rsdata'); //get the repeating section var labels = NWF$(rsdata).find('.rsText'); //count number of fields to give number of rows //iterate through rows NWF$.each(labels, function (index) { NWF$('.rsdata .nf-repeater-row:last').find('.nf-repeater-deleterow-image').click(); }); //for last row as index 0 row cannot be deleted NWF$('.rsdata .nf-repeater-row:last').each(function () { var $row = NWF$(this); //get the row if ($row.find('.rsBox').checked != true) { //clear the field values $row.find('.rsText input').val(''); $row.find('.rsChoice select').val(''); $row.find('.rsLookup select').val(''); $row.find('.rsCascLookup select').val(''); } }); } function GetRSDataFromLookup() { var lookupId = NWF.RuntimeFunctions.parseLookup(NWF$('#' + varDropdown).val(), true); //get the lookup value //if lookup value is not null if (lookupId != '') { NWF$('#errorMsg').empty(); //clear jQuery error messages //set the lookup list url to get repeating section xml data var requestUri = _spPageContextInfo.webAbsoluteUrl + '/_api/Web/Lists/getByTitle('BOM_Lookup')/items?$filter=Title eq '' + lookupId + '''; try { NWF$.ajax({ url: requestUri, type: 'GET', headers: { 'ACCEPT':'application/json;odata=verbose' }, success: GetRSDataFromLookupSuccess, error: GetRSDataFromLookupError }); } catch (err) { NWF$('#errorMsg').html('getListData Error: ' + err); //set error message } } } function GetRSDataFromLookupSuccess(data) { //check the result set is not empty if (data.d.results.length != 0) { var rsdata = data.d.results[0].Items; //get the xml data from the result parser = new DOMParser(); //intiate xml parser xmlDoc = parser.parseFromString(rsdata, 'text/xml'); // populate the parser with the xml data var items = xmlDoc.getElementsByTagName('Item'); //get the item tag from the xml //foreach item in the xml for (var i = 0; i < items.length; i++) { //get the repeating section last row NWF$('.rsdata .nf-repeater-row:last').each(function() { var $row = NWF$(this); //set the field values $row.find('.rsText input').val(xmlDoc.getElementsByTagName('rsText')[i].childNodes[0].nodeValue); $row.find('.rsChoice select').val(xmlDoc.getElementsByTagName('rsChoice')[i].childNodes[0].nodeValue); $row.find('.rsLookup select').val(xmlDoc.getElementsByTagName('rsLookup')[i].childNodes[0].nodeValue); $row.find('.rsCascLookup select').val(xmlDoc.getElementsByTagName('rsCascLookup')[i].childNodes[0].nodeValue); }); NWF$('.rsdata').find('a').click(); //add new row } NWF$('.rsdata .nf-repeater-row:last').find('.nf-repeater-deleterow-image').click(); //delete extra unnecessary row } } function GetRSDataFromLookupError() { alert('Failed to load Lookup Items'); }

The main difference between the blog post and my situation is that I am using a cascading lookup in each row of the repeating section.  I am auto-populating the lookup controls just like the other controls, and they are work fine except for the last one.  It is a lookup control filtered by the value of the previous lookup control.

Whenever I have the filter on, the value I am looking to have filled in is doesn't show up and I can't slect anything in the dropdown because there are no choices:

8227i87FBF483408A7318.png

However, when I turn off the filtering and allow all options in the lookup, the data is auto-populated and there are zero issues:

8228i7E28A044C589FDEA.png

 

 

Can anyone explain to me why this is happening and how I could fix it?  That would be greatly appreciated.

 

Thank you,

Owen


0 replies

Be the first to reply!

Reply