0

I'm looking at editng an item in a list then when save is pressed a modal dialog appears which allows adding an item to a second list. When the second list is saved then the modal dialog closes and the orgingal list is also saved. I've achieved most of this using PreSaveItem(), when the parent save button is pressed the child list appears in the Modal and when the child list is saved I recieve the alert message confirming child item is saved, clicking ok in the alert returns to the parent item but the parent does not save automatically. Pressing save again brings up the Modal dialog.

It appears the closure of the modal dialog does not return true for the PreSaveitem



function PreSaveItem() 
{  

alert("Saving");
return OpenDialog('http://sharesite/sites/mysite1/Lists/Time3/NewForm.aspx');
return true; 
}




//User Defined Function to Open Dialog Framework
 function OpenDialog(strPageURL) 
{
   var dialogOptions = SP.UI.$create_DialogOptions();
   dialogOptions.url = strPageURL;// URL of the Page
   dialogOptions.width = 750; // Width of the Dialog
   dialogOptions.height = 500; // Height of the Dialog
   dialogOptions.dialogReturnValueCallback = Function.createDelegate( null, CloseCallback); // Function to capture dialog closed event
   SP.UI.ModalDialog.showModalDialog(dialogOptions); // Open the Dialog
   return false;
 }

// Dialog close event capture function
 function CloseCallback(strReturnValue, target) 
{
   if (strReturnValue === SP.UI.DialogResult.OK) // Perform action on Ok.
     {
    alert("Thank you for adding time value");

    }
   if (strReturnValue === SP.UI.DialogResult.cancel) // Perform action on Cancel.
    {
    alert( "You clicked cancel!");
    }
 }

4
  • My javascript skills are amateur at best, so I apologize for noob questions. In your PreSaveItem() function, you're calling the OpenDialog() function with return OpenDialog(url). OpenDialog() returns false, so wouldn't that cause the PreSaveItem() function to stop the first list item from saving?
    – wjervis
    Commented May 27, 2014 at 11:12
  • I used IE Dev tools to follow your functions, and it is indeed returning false (not sure what is causing it, removing the return false; in OpenDialog() did nothing).
    – wjervis
    Commented May 27, 2014 at 11:31
  • Hi Thanks for your help. I think somehow the code in the braces alert("Thank you for adding time value"); needs to be able to alter the outcome. Any ideas on this? Commented May 27, 2014 at 13:38
  • Sorry, I'm from a C# background, JavaScript is still new to me.
    – wjervis
    Commented May 27, 2014 at 14:24

1 Answer 1

2

EDIT

as iv noted here, you need to understand that there is two things going on here.

1) you have the parent list that your opening up that you edit but havent saved and then you open up a child within the parent form that you also open up and want to creat.

2) saving the child sends a postback from the parent form to save the child details. This loses focus on your parent element.

the child element in JS ModalDialog is essentially an iframe within the parent.

so you have two options.

1) do as i said above by calling as you have but instead have some logic in code to call the return function back to the parent to save.

2) save the parent first using partial postback and then open and save the child

EDIT

Ok i have one solution for you.

It means you have to add the new item on the item edit tho before you edit the item it loads the new popup form! i will keep on looking into the main solution. so far i can get the page to load the parent and when you click save it loads the child that you can save but you need to click on save again to clear and save the edited parent!

ExecuteOrDelayUntilScriptLoaded(PreSaveAction,"sp.js");

function callthismethod()
{
      var dialogOptions = SP.UI.$create_DialogOptions();
      dialogOptions.url ='http://sharesite/sites/mysite1/Lists/Time3/NewForm.aspx';
      dialogOptions.width = 750;
      dialogOptions.height = 500; 
      dialogOptions.dialogReturnValueCallback = Function.createDelegate( null, CloseCallback); 
      SP.UI.ModalDialog.showModalDialog(dialogOptions); 
}

EDIT

You ow me big time ;) , everyone online said its not possible due to multiple partial postbacks going on... but its not ;).

to explain. you have the parent and the parent gets changed... you then click save and that opens the child and you save. It causes a postback but you already have a save item event that is already called causing JS to get confused!

solution....

do the same as above. this time before we call the save item, when we load the parent edit form we want to get the button save event name for later use. we then call the dialog for the child and on save (ok) on the child we can then call the save item event for the parent. the child is saved and closed. On the parent iv put an if statment to tell that we have already done the child so no need to call it but we can go a do a postback using the save button instead! so this is where the button event name comes in handy to call the postback event to save the parent list :)

<script type="text/javascript">


var setTrue = false;
var target;
var contName;

function PreSaveItem()
{   
    //on first load global varible setTrue is false so we can load the popup. 
    //but when true it will save parent list imitating save click event.
    if ("function"==typeof(PreSaveAction)&& setTrue == false)
    {
              //get the clicked save button so that we can use the id (name) later to save when doing a postback.
        target = event.target || event.srcElement;
        contName = target.name;
              //call the save action to load the child list
        PreSaveAction();
    }
    else
    {
         //do postback with parent save button manually
            WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions(contName, "", true, "", "", false, true));
    }
}

//function that creates child popup to add new item to a child list
function PreSaveAction() 
{  
      var dialogOptions = SP.UI.$create_DialogOptions();
      dialogOptions.url = 'http://sharesite/sites/mysite1/Lists/Time3/NewForm.aspx'';
      dialogOptions.width = 750;
      dialogOptions.height = 500; 
      dialogOptions.dialogReturnValueCallback = Function.createDelegate( null, CloseCallback); 
      SP.UI.ModalDialog.showModalDialog(dialogOptions); 
}

//actions (save/cancel) for the child form
function CloseCallback(strReturnValue, target) 
{
    if (strReturnValue === SP.UI.DialogResult.OK) 
    {
       //if we save than set global bool to true
       setTrue = true;
       alert("Thank you for adding time value");
       //call presave item for parent list save
       PreSaveItem();

    }
    if (strReturnValue === SP.UI.DialogResult.cancel) 
    {
        setTrue = false;
        alert( "You clicked cancel!");
    }
 }

</script>
17
  • Hi I've tried both your codes. The 1st one with the button works as expected, the button triggers the dialog box. The second code behaves exactly the same as my original code. When the parent list is edited and save button pressed the dialog appears displaying the child list. Pressing save closes the dialog and a thanks alert appears. However the problem is that the parent list is still open and unsaved. Pressing Save again repeats the cycle! Does your example behave the same? Commented May 28, 2014 at 8:05
  • i am slightly confused? so you have two dialogs... one is the parent and the second is the child... from the parent you could click on a button as an example that would open a child, from the child you can create a new item and click ok to close the child and then close or update the parent?? sorry alot of confusion going on here!
    – Ali Jafer
    Commented May 28, 2014 at 8:35
  • ahh ok so your looking to edit the parent list, that will inturn open a child dialog that creates an item and then when you close the dialog you want to update the parent?
    – Ali Jafer
    Commented May 28, 2014 at 8:37
  • yes. is it possible? Commented May 29, 2014 at 7:59
  • ok i can see that your editing the code for the child but not the parent? how are you calling the child from the parent, is this when your editing the parent list item, i know its on the save button, is it a custom form or out of the box that you listing out for? sorry for the questions, i want to know exactly what you want so i can recreate it on my site to test.
    – Ali Jafer
    Commented May 29, 2014 at 8:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.