Closing parent form from child

Hi,
Can i close parent form automatically when is closing child form?

I tried to use inline list and library control populate from parent, but it is empty...


fd.spRendered(function() {
    //prepopulating fields of a new record
    //with the values from the parent form
    fd.control('SPDataTable2').$on('edit', function(editData) {
        //check that this is a new record
        if (editData.formType === 'New') {
            //prepopulating due date
			editData.field('Utilizator').value = fd.field('Author').value;
			editData.field('Title').value = fd.field('Title').value
            
           
           
        }
    });
});

Does it works only in Online ? i have On-prem. (see some info about this that's work only in Online)
If so, than i need to close parent form automatically when is closing child form...

Hello @ixxxl,

Yes, the Edit event of a List or Library control is currently only available for the SharePoint Online version.

Could you please provide more details about the case.

Do you want the parent form to be closed when the child item is saved? Or when the dialog with the cold form is closed and the changes are not saved?

@Margo
i want the parent form to be closed after the child item is saved.
This is parent


And this one is child

Parent is in display view, user creates like confirmation and when i save child , want to close parent.

@ixxxl,

You can periodically check the length of the control and close parent when a new record is added:

var newCount = 0;
var currentCount = 0;

function checkRecords() {

    newCount = fd.control('SPDataTable1').widget.dataItems().length;

    if(newCount > currentCount){
        currentCount = newCount;
        fd.close();
    }
    else if(newCount < currentCount) {
        currentCount = newCount;
    }
}


fd.spRendered(function() {
    fd.control('SPDataTable1').ready().then(function(dt) {
		currentCount = fd.control('SPDataTable1').widget.dataItems().length;
	    var myVar = setInterval(checkRecords, 2000);
    });
    
});

HI, @Margo
it show an error in console
image

function checkRecords() {
  var currentCount='';
    var newCount = fd.control('SPDataTable2').widget.dataItems().length;

    if(newCount > currentCount){
        currentCount = newCount;
        fd.close();
    }
    else if(newCount < currentCount) {
        currentCount = newCount;
    }
}


fd.spRendered(function() {

    var currentCount = fd.control('SPDataTable2').widget.dataItems().length;
    var myVar = setInterval(checkRecords, 2000);

});

I resolved with dataItems.
There is one more - currentCount - not definied if it is not in the code. and as i put - var currentCount=''; it closes on opening...

1 Like

@ixxxl,

I'm sorry, my bad!

Glad you've figured it out!

@Margo
There is one more - currentCount - not definied if it is not in the code. and as i put - var currentCount=''; it closes on opening...and i can't do anything...can't create child
what should i put in var currentCount ?

@ixxxl,

Please see the updated code:

var newCount = 0;
var currentCount = 0;

function checkRecords() {

    newCount = fd.control('SPDataTable1').widget.dataItems().length;

    if(newCount > currentCount){
        currentCount = newCount;
        fd.close();
    }
    else if(newCount < currentCount) {
        currentCount = newCount;
    }
}


fd.spRendered(function() {
    fd.control('SPDataTable1').ready().then(function(dt) {
		currentCount = fd.control('SPDataTable1').widget.dataItems().length;
	    var myVar = setInterval(checkRecords, 2000);
    });
    
});

@Margo

i open parent form, the child have some elements from other users. and i try to navigate to add one more new child, the form is closing...no erros in console.
it seems that works first one that newCount have for example 10 > than currentCount that by default have 0

newCount i think must be like currentCount +1 . but when.....
Some code after creating new item maybe?

@ixxxl,

Do you have any other code related to List or Library control? Could you please comment it out and check if the provided code is working.

@Margo
You are right.it works only your code, but when i put related, it only closes on opening

window.fd=fd;

   fd.spRendered(function () {

	//get all buttons
	function Store() {
/*	var selected = fd.field('Categorii_x0020_de_x0020_adresa_').value;
	 var s = '';
	for (var i = 0; i < selected.length; i++) {
		s += selected[i].LookupValue + '; ';
	
	}
	//Set field values with the values from the parent on form load
	fd.field('cat').value = s;*/


		if (typeof(Storage) !== "undefined") {
  // Code for localStorage/sessionStorage.
 sessionStorage.lastname = fd.field('cat').value;
 sessionStorage.boss = fd.field('Boss1').value;
} else {
  // Sorry! No Web Storage support..
  alert('Ahtung');
}
}
//fd.field('Categorii_x0020_de_x0020_adresa_').$on('change', Store);
Store();
})
window.fd = fd;
fd.spRendered(function () {
    //get all buttons
    function setFieldPropreties() {
        //Disable Sharepoint Save field 
  //  fd.toolbar.buttons[0].style = "display: none;";
  fd.toolbar.buttons[1].text = "Editează";
    fd.toolbar.buttons[1].text = "Închide";
        //Hide system fields
        $('.sysfields').hide();
        $('.motiv').hide();
        // Disable edit la cimpurile populate
 //      fd.field('Data_x0020_aprob_x0103_rii').required = true;
    //    fd.field('Data_x0020__x00ee_ntr_x0103_rii_').required = true;
 //       fd.field('Tip_x0020_de_x00Stare_x0020_ANI20_produs').required = true;
   //     fd.field('Versiune_x0020_ANI').required = true;
     fd.field('Statut_x0020_Confirmare').disabled = false;
  
  //      fd.field('UID').required = true;
            }
    // executes setFieldPropreties on form load
    setFieldPropreties();
});
fd.spRendered(function () {
          fd.control('SPDataTable1').ready().then(function (dt) {
          //dt parameter is the same as fd.control('SPDataTable0')
          console.log('SPDataTable1 is initialized');
          fd.control('SPDataTable1').buttons[0].text = 'Adăugați versiune ANI';
          //set width and height:
		          fd.control('SPDataTable1').dialogOptions = {
              width: 1280,
              height: 720
          }
      });
      fd.control('SPDataTable4').ready().then(function (dt) {
          //dt parameter is the same as fd.control('SPDataTable0')
          console.log('SPDataTable4 is initialized');
          fd.control('SPDataTable4').buttons[0].text = 'Button disabled';
          //set width and height:
          fd.control('SPDataTable4').dialogOptions = {
              width: 1280,
              height: 720
          }
      });
      fd.control('SPDataTable2').ready().then(function (dt) {
          //dt parameter is the same as fd.control('SPDataTable0')
          console.log('SPDataTable2 is initialized');
          fd.control('SPDataTable2').buttons[0].text = 'Adăugați confirmare ANI';
          //set width and height:
          fd.control('SPDataTable2').dialogOptions = {
              width: 1280,
              height: 720
          }
      });
      fd.control('SPDataTable3').ready().then(function (dt) {
          //dt parameter is the same as fd.control('SPDataTable0')
          console.log('SPDataTable3 is initialized');
          fd.control('SPDataTable3').buttons[0].text = 'Adăugați comentarii ANI';
          //set width and height:
          fd.control('SPDataTable3').dialogOptions = {
              width: 1280,
              height: 720
          }
      });
  });
  
  fd.spRendered(function(ctx) {
    var dt = fd.control('SPDataTable2');
    dt.ready().then(function() {
        setRootFolder();
    });

    //set root folder when Category field changes
    fd.field('Title').$on('change', function() {
        setRootFolder();
    });

    function setRootFolder(){
        var category = (fd.field('ID').value +'_' +fd.field('Title').value)
       
            dt.baseRootFolder = category;
            dt.rootFolder = category;
        
    }
});

fd.spRendered(function () {
    function setFieldPropreties1(){
    if (fd.field('Statut_x0020_Confirmare').value == null || fd.field('Statut_x0020_Confirmare').value == ""){
	fd.field('Statut_x0020_Confirmare').placeholder ='Indicați statut de confirmare';
fd.control('Button1').disabled = true;
}else {
fd.control('Button1').disabled = false;
}
}
// Calling setPercentComplete when the user changes the status
fd.field('Statut_x0020_Confirmare').$on('change',setFieldPropreties1);
   setFieldPropreties1();
});

alert('0')
fd.spRendered(function() {

    fd.control('SPDataTable2').$on('edit', function(editData) {
        //Set Resolutiondate field value when TaskStatus field changes
		alert('111')
        editData.field('Statut_x0020_confirmare').$on('change', function(value) {
            if (value === 'însușit') {
            editData.field('Utilizator').value = _spPageContextInfo.userLoginName;
			editData.field('Utilizator').disabled = true;
			editData.field('VID').disabled = true;
			editData.field('Title').value = fd.field('Title').value
            } else {
              //  editData.field('Resolutiondate').value = null;
            }
        });
    });
});
var newCount = 0;
var currentCount = 0;

function checkRecords() {

    newCount = fd.control('SPDataTable2').widget.dataItems().length;

    if(newCount > currentCount){
        currentCount = newCount;
        fd.close();
    }
    else if(newCount < currentCount) {
        currentCount = newCount;
    }
}


fd.spRendered(function() {
    fd.control('SPDataTable2').ready().then(function(dt) {
		currentCount = fd.control('SPDataTable2').widget.dataItems().length;
	    var myVar = setInterval(checkRecords, 2000);
    });
    
});

on SpDatatable2 - change button name - is it my change on a list library control that triggers count ?

@Margo
When i disable this code it works

  fd.spRendered(function(ctx) {
    var dt = fd.control('SPDataTable2');
    dt.ready().then(function() {
        setRootFolder();
    });

    //set root folder when Category field changes
    fd.field('Title').$on('change', function() {
        setRootFolder();
    });

    function setRootFolder(){
        var category = (fd.field('ID').value +'_' +fd.field('Title').value)
       
            dt.baseRootFolder = category;
            dt.rootFolder = category;
        
    }
});

How to combine root folder and count-closing for work ?:slight_smile:

@Margo
any solutions? :slight_smile:

@ixxxl,

Try it out like this:

var newCount = 0;
var currentCount = 0;

function checkRecords() {

    newCount = fd.control('SPDataTable1').widget.dataItems().length;

    if(newCount > currentCount){
        currentCount = newCount;
        fd.close();
    }
    else if(newCount < currentCount) {
        currentCount = newCount;
    }
}
fd.spRendered(function() {
    var dt = fd.control('SPDataTable1');
    dt.ready().then(function() {
        setRootFolder();
        currentCount = fd.control('SPDataTable1').widget.dataItems().length;
	    var myVar = setInterval(checkRecords, 2000);
    });

    //set root folder when Category field changes
    fd.field('Title').$on('change', function() {
        setRootFolder();
        currentCount = fd.control('SPDataTable1').widget.dataItems().length;
	    var myVar = setInterval(checkRecords, 2000);
    });

    function setRootFolder(){
        var category = fd.field('Title').value;
       
            dt.baseRootFolder = category;
            dt.rootFolder = category;
        
    } 
});

@Margo
now root folder doesn't appear in list library and i can't add new item ib list and library.
There is one more error in console
image
The code seems no errors

window.fd=fd;

   fd.spRendered(function () {

	//get all buttons
	function Store() {
/*	var selected = fd.field('Categorii_x0020_de_x0020_adresa_').value;
	 var s = '';
	for (var i = 0; i < selected.length; i++) {
		s += selected[i].LookupValue + '; ';
	
	}
	//Set field values with the values from the parent on form load
	fd.field('cat').value = s;*/


		if (typeof(Storage) !== "undefined") {
  // Code for localStorage/sessionStorage.
 sessionStorage.lastname = fd.field('cat').value;
 sessionStorage.boss = fd.field('Boss1').value;
} else {
  // Sorry! No Web Storage support..
  alert('Ahtung');
}
}
//fd.field('Categorii_x0020_de_x0020_adresa_').$on('change', Store);
Store();
})
window.fd = fd;
fd.spRendered(function () {
    //get all buttons
    function setFieldPropreties() {
        //Disable Sharepoint Save field 
  //  fd.toolbar.buttons[0].style = "display: none;";
  fd.toolbar.buttons[1].text = "Editează";
    fd.toolbar.buttons[1].text = "Închide";
        //Hide system fields
        $('.sysfields').hide();
        $('.motiv').hide();
        // Disable edit la cimpurile populate
 //      fd.field('Data_x0020_aprob_x0103_rii').required = true;
    //    fd.field('Data_x0020__x00ee_ntr_x0103_rii_').required = true;
 //       fd.field('Tip_x0020_de_x00Stare_x0020_ANI20_produs').required = true;
   //     fd.field('Versiune_x0020_ANI').required = true;
     fd.field('Statut_x0020_Confirmare').disabled = false;
  
  //      fd.field('UID').required = true;
            }
    // executes setFieldPropreties on form load
    setFieldPropreties();
});
fd.spRendered(function () {
          fd.control('SPDataTable1').ready().then(function (dt) {
          //dt parameter is the same as fd.control('SPDataTable0')
          console.log('SPDataTable1 is initialized');
          fd.control('SPDataTable1').buttons[0].text = 'Adăugați versiune ANI';
          //set width and height:
		          fd.control('SPDataTable1').dialogOptions = {
              width: 1280,
              height: 720
          }
      });
      fd.control('SPDataTable4').ready().then(function (dt) {
          //dt parameter is the same as fd.control('SPDataTable0')
          console.log('SPDataTable4 is initialized');
          fd.control('SPDataTable4').buttons[0].text = 'Button disabled';
          //set width and height:
          fd.control('SPDataTable4').dialogOptions = {
              width: 1280,
              height: 720
          }
      });
      fd.control('SPDataTable2').ready().then(function (dt) {
          //dt parameter is the same as fd.control('SPDataTable0')
          console.log('SPDataTable2 is initialized');
          fd.control('SPDataTable2').buttons[0].text = 'Adăugați confirmare ANI';
          //set width and height:
          fd.control('SPDataTable2').dialogOptions = {
              width: 1280,
              height: 720
          }
      });
      fd.control('SPDataTable3').ready().then(function (dt) {
          //dt parameter is the same as fd.control('SPDataTable0')
          console.log('SPDataTable3 is initialized');
          fd.control('SPDataTable3').buttons[0].text = 'Adăugați comentarii ANI';
          //set width and height:
          fd.control('SPDataTable3').dialogOptions = {
              width: 1280,
              height: 720
          }
      });
  });
  
  /*fd.spRendered(function(ctx) {
    var dt = fd.control('SPDataTable2');
    dt.ready().then(function() {
        setRootFolder();
    });

    //set root folder when Category field changes
    fd.field('Title').$on('change', function() {
        setRootFolder();
    });

    function setRootFolder(){
        var category = (fd.field('ID').value +'_' +fd.field('Title').value)
       
            dt.baseRootFolder = category;
            dt.rootFolder = category;
        
    }
});
*/
fd.spRendered(function () {
    function setFieldPropreties1(){
    if (fd.field('Statut_x0020_Confirmare').value == null || fd.field('Statut_x0020_Confirmare').value == ""){
	fd.field('Statut_x0020_Confirmare').placeholder ='Indicați statut de confirmare';
fd.control('Button1').disabled = true;
}else {
fd.control('Button1').disabled = false;
}
}
// Calling setPercentComplete when the user changes the status
fd.field('Statut_x0020_Confirmare').$on('change',setFieldPropreties1);
   setFieldPropreties1();
});


/*var newCount = 0;
var currentCount = 0;

function checkRecords() {

    newCount = fd.control('SPDataTable2').widget.dataItems().length;

    if(newCount > currentCount){
        currentCount = newCount;
        fd.close();
    }
    else if(newCount < currentCount) {
        currentCount = newCount;
    }
}


fd.spRendered(function() {
    fd.control('SPDataTable2').ready().then(function(dt) {
		currentCount = fd.control('SPDataTable2').widget.dataItems().length;
	    var myVar = setInterval(checkRecords, 2000);
    });
    
});
*/

var newCount = 0;
var currentCount = 0;

function checkRecords() {

    newCount = fd.control('SPDataTable2').widget.dataItems().length;

    if(newCount > currentCount){
        currentCount = newCount;
        fd.close();
    }
    else if(newCount < currentCount) {
        currentCount = newCount;
    }
}
fd.spRendered(function() {
    var dt = fd.control('SPDataTable2');
    dt.ready().then(function() {
        setRootFolder();
        currentCount = fd.control('SPDataTable2').widget.dataItems().length;
	    var myVar = setInterval(checkRecords, 2000);
    });

    //set root folder when Category field changes
    fd.field('Title').$on('change', function() {
        setRootFolder();
        currentCount = fd.control('SPDataTable2').widget.dataItems().length;
	    var myVar = setInterval(checkRecords, 2000);
    });

    function setRootFolder(){
        var category = fd.field('Title').value;
       
            dt.baseRootFolder = category;
            dt.rootFolder = category;
        
    } 
});

P.S
I changed to var category = (fd.field('ID').value +'_' +fd.field('Title').value) and now root folder works.
But the same result on opening main form it is closing after 2-3 seconds....

@ixxxl,

It is better to have one spRendered function and add run all code within it.

I can't tell which part of your code breaks the logic of the code that I've provided. You can debug your code, following the suggestions from this post:

@Margo
after days trying.. no result...

fd.spRendered(function(ctx) {
       var dt = fd.control('SPDataTable2');
       dt.ready().then(function() {
           setRootFolder();
       });
   
       //set root folder when Category field changes
       fd.field('Title').$on('change', function() {
           setRootFolder();
       });
   
       function setRootFolder(){
           var category = (fd.field('ID').value +'_' +fd.field('Title').value)
          
               dt.baseRootFolder = category;
               dt.rootFolder = category;
           
       }
   });
   


 var newCount = 0;
   var currentCount = 0;
   
   function checkRecords() {
   
       newCount = fd.control('SPDataTable2').widget.dataItems().length;
   
       if(newCount > currentCount){
           currentCount = newCount;
           fd.close();
       }
       else if(newCount < currentCount) {
           currentCount = newCount;
       }
   }
   
   
   fd.spRendered(function() {
       fd.control('SPDataTable2').ready().then(function(dt) {
           currentCount = fd.control('SPDataTable2').widget.dataItems().length;
           alert('current'+currentCount)
           var myVar = setInterval(checkRecords, 2000);
       });
       
   });

one open form it's closing after 2 seconds. The reason is setting root folder. Is it a way some kind assinhron start code maybe ... because when is setting root folder it didn't need to run part with closing parent..only after adding new item in child.

P.S . Trying your code published earlier - works on root folder, and the same is closing after 2 seconds..

@ixxxl,

This code works on my form:

var newCount = 0;
var currentCount = 0;

function checkRecords() {

    newCount = fd.control('SPDataTable1').widget.dataItems().length;

    if(newCount > currentCount){
        currentCount = newCount;
        alert('new item')
    }
    else if(newCount < currentCount) {
        currentCount = newCount;
        alert('deleted')
    }
}

    function setRootFolder(){
        //set base root folder to the current item's ID:
        fd.control('SPDataTable1').baseRootFolder = String(fd.field('Title').value);
        //set current folder to the current item's ID:
        fd.control('SPDataTable1').rootFolder = String(fd.field('Title').value);
        newCount = 0;
        currentCount = 0;
    }

fd.spRendered(function() {
    fd.control('SPDataTable1').ready().then(function(dt) {
    	setRootFolder();
		
	    setTimeout(function(){
	    	currentCount = fd.control('SPDataTable1').widget.dataItems().length;
	    	var myVar = setInterval(checkRecords, 2000);
	    	}, 2000);
    });
   fd.field('Title').$on('change', function() {
       setRootFolder();
    });
    
});

@Margo
Now it works thank you !!
One more thing that i found. When i open the form and in list and library there are more then 30 items it have arrows to list to another 30 items. when i press next - it alerts deleted as in the code:


I press ok, and then press back arrow - the alert is new item. and after that i added closing form, and its closes without adding new item in list\library control
image

@Margo
Hello
There is a problem, when library and list control have more than 30 items .. Because in display view of parent form it show only 30(for example) and when you create new item it shows now 30 items... and code can't count items in table. it works only undo 30 items. until it can count in view.