Closing parent form from child

@mnikitina
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);
    });
    
});

@mnikitina

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.

@mnikitina
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 ?

@mnikitina
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:

@mnikitina
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;
        
    } 
});

@mnikitina
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:

@mnikitina
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();
    });
    
});

@mnikitina
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

@mnikitina
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.

Hello @ixxxl,

fd.control('SPDataTable2').widget.dataItems() - returns the items for the current page only.

You have two options here:

  1. Change the count of items per page in the list view settings;
  2. Use PnPjs to get the items from the specific folder:
    https://github.com/pnp/pnpjs/issues/890
1 Like

@mnikitina
Good day
when i use window.fd=fd;
another users can edit from console my fields.(if i disabled them, they can enable them etc.) Is it a way to prevent this?

Hello @ixxxl,

A user can modify anything on the form from the console, no matter if fd is defined globally or not.

The question is why would they need to modify the form?

A user can send requests from the console to create/update list items from the console, as long as he has permission.

@mnikitina

Because they want to destroy forms and to tell that developers maked something wrong:)
In russian language: Рабы издавна ломали свои инструменты,чтобы не работать:)) It's actual nowadays. Is it a way to have some log about changing in console? to find which user make it?
thank you for replay

@ixxxl,

User can't change the form for all users from the console, only for his current session. If someone is complaining and you can't reproduce the case - that the user you are looking for.

If you want to track changes of the form, you can enable versioning for the SitePages library to track who and when changed the form.

1 Like

@mnikitina

Thank you i have versioning enabled.

Blockquote
User can't change the form for all users from the console, only for his current session

I understand that, but in case when piece of form is blocked from with information from previos users, current can open this fields , change some info and save it. after that telling to support that he didn't do anything, and it's a bag in form. that's why a asked you about this. it seems that without window.fd=fd - user can't modify fields - console errors about fd undifinied