Salesforce 如何使用Visualforce添加自定义确认对话框

Salesforce 如何使用Visualforce添加自定义确认对话框,salesforce,apex-code,visualforce,Salesforce,Apex Code,Visualforce,正如下面我的Apex代码所示 一旦用户使用“apex:commandButton”执行“saveSearchItems”操作, 如果验证结果为“false”, 我想向带有“验证失败,是否继续?”的用户消息显示确认对话框 当用户按“确定”按钮时,我希望保存项目(通过执行saveSearchItems方法),如果用户按“否”,我希望停止保存(不执行任何操作) 有谁能告诉我如何使用Apex&Visualforce实现这一点。 非常感谢 /** * Save list of SearchItems

正如下面我的Apex代码所示

一旦用户使用“apex:commandButton”执行“saveSearchItems”操作, 如果验证结果为“false”, 我想向带有“验证失败,是否继续?”的用户消息显示确认对话框

当用户按“确定”按钮时,我希望保存项目(通过执行saveSearchItems方法),如果用户按“否”,我希望停止保存(不执行任何操作)

有谁能告诉我如何使用Apex&Visualforce实现这一点。 非常感谢

 /**
  * Save list of SearchItems 
  */
 public void saveSearchItems()  { 

    // Validate values of SearchItems 
       Boolean validateResult = validateSearchItems(this.searchItemList);

    // Save SearchItems   
       saveSearchItems(this.searchItemList);         
 }

Apex代码在服务器上执行,而不是在客户端执行,所以不能从Apex显示对话框。因此,您必须将验证移动到客户端(使用javascript onclick处理程序),或者必须将保存过程拆分为两个对服务器的Ajax调用,一个用于验证另一个用于保存,然后使用中间验证结果选择性地显示对话框。选择取决于验证的复杂性(
validateArchitems

虽然mmix是正确的,但实际上有一种方法可以模拟这一点,它只是需要创造性地使用apex:actionFunction和少量javascript。您基本上将操作分解为两个调用,以处理http的客户机/服务器模型的实际情况

第一个是验证调用,用于确定是否应向用户显示弹出窗口,第二个是根据用户响应在需要时进行保存的调用。请记住,我的解决方案依赖于javascript确认框,因此这里没有花哨的ui:)

仅供参考-在我的示例中,我确实引用了jquery,只需将jquery最小化库保存为jquery.txt,并使用名称“jquery”添加到您的静态资源中,就可以了

使用以下代码创建控制器类:

public class ActionFunctionCLS {

public Boolean causeValidationWarning {get; set;} // Just used for controlling the test behavoir from the ui. Not a part of the solution.
public String validationWarning {get; set;}

// Validates the record (or whatever) is good. If it is then we call save, else we set a warning message that 
// tells the ui to display a javascript popup box to the user confirming they really want to proceed.
public PageReference ValidateAndSaveRecord() {
    PageReference result = null;

    if(causeValidationWarning){
      validationWarning = 'This is a validation warning, hit Ok if you\'re sure you want to proceed.';
    }
    else {
      validationWarning = '';
      result = SaveRecord();
    }
    return result;
}

// Clears the validation warning so the ui won't try and display a confirmation box to the user
public void clearvalidationWarning(){
   validationWarning = '';
}

// Actually saves the record and then performs some post save function. Could be anything from displaying a save message (which means
// we'd need to clear the validation warning to keep the confimation pop from appearing), or it could even be a redirect somewhere else.
public PageReference SaveRecord() {
    ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.CONFIRM, 'Save Complete!', 'And I totally could have redirected you or something.');
    ApexPages.addMessage(myMsg);
  clearvalidationWarning();
    return null;
}

// Just to prove that our Validation & Save logic won't affect other functionality
public PageReference DoSomething() {
  ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.INFO, 'I did some other work.');
  ApexPages.addMessage(myMsg);
  return null;
}
}

然后创建一个可视力页面:

<apex:page controller="ActionFunctionCLS" id="pg" showHeader="true" sidebar="true">
<apex:includeScript value="{!$Resource.jquery}"/>

<script>
    var j$ = jQuery.noConflict(); //Needed for jQuery usage

    j$(document).ready(function(){           
        var validationWarningMsg = "{!validationWarning}";
        if(validationWarningMsg != ''){ // There was a validation warning, make sure the user still wants to save    
            if(confirm(validationWarningMsg)){
                saveRecordAction();
            }
            else {
                clearvalidationWarningAction();
            }
        }
    });
</script>

<apex:form id="myForm">
     <apex:pageMessages />
     <apex:actionfunction name="saveRecordAction" action="{!SaveRecord}" />
     <apex:actionfunction name="clearvalidationWarningAction" action="{!clearvalidationWarning}" />

     <apex:pageBlock tabStyle="Account">
        <apex:pageBlockButtons >
            <apex:commandButton value="Save Record" action="{!ValidateAndSaveRecord}" />
            <apex:commandButton value="Do Other Work" action="{!DoSomething}" />
       </apex:pageBlockButtons>

       <apex:pageBlockSection title="Stuff" columns="1" >
        <apex:inputCheckbox value="{!causeValidationWarning}" label="Cause a validation warning" />
       </apex:pageBlockSection>
    </apex:pageBlock>
</apex:form>

var j$=jQuery.noConflict()//jQuery使用需要
j$(文档).ready(函数(){
var validationWarningMsg=“{!validationWarning}”;
如果(validationWarningMsg!=''){//出现验证警告,请确保用户仍要保存
如果(确认(验证警告消息)){
saveRecordAction();
}
否则{
clearvalidationWarningAction();
}
}
});

然后测试!apex:actionFunction是我最喜欢的功能之一,它允许您从javascript直接对控制器方法进行ajax调用,而无需任何难看的编码,非常简洁

这里发生的事情是save按钮正在调用ValidateAndSaveRecord(),它运行我们想象的验证,然后在一切正常的情况下实际保存。如果不是,则在控制器上设置一个字符串属性并返回。页面上的javascript在页面加载时查找此字符串,如果找到,将显示确认弹出窗口

当用户单击Ok时,会调用另一个ajax来保存(),但会跳过验证,因为验证已经完成。如果他们单击cancel,仍会发出一个呼叫以清除警告字符串,这样下次回发不会导致页面再次显示弹出窗口。这一步非常重要


我认为我的解决方案可能会让人有些心痛,因为它严重依赖于取消呼叫,但根据我的经验,它是相当可靠的。享受。

嗨,mmix!非常感谢您的宝贵反馈。祝你玩得愉快!