7

我知道这是如何保存记录

<apex:commandButton action="{!save}" value="Save"/>

我想要一个按钮来不保存当前记录(即取消)并导航到已保存记录的列表(即该对象类型的对象列表)。

像这样的东西...

<apex:commandButton action="{!cancel}" value="Cancel"/>
4

4 回答 4

9

对象的列表视图是您的基本 URL / 对象的 3 个字母前缀 / o,例如:

https://na1.salesforce.com/a0C/o

因此,您可以创建一个操作方法,该方法返回Pagereference带有适当 URL 的 a 并设置为重定向 ( pr.setRedirect(true))。

或者,您可以将控制器用作标准控制器的扩展,然后在标准控制器上调用取消

// controller extension
public class TimeSheetExtension
{
  ApexPages.standardController m_sc = null;

  public TimeSheetExtension(ApexPages.standardController sc)
  {
    m_sc = sc;
  }

  public PageReference doCancel()
  {
    return m_sc.cancel();
  }
}

// page
<apex:commandButton action="{!doCancel}" value="Cancel"/>

请注意,这并不一定会将您带到列表视图,它会将您返回到您在转到 VF 页面之前查看的最后一页。

于 2012-01-19T05:31:28.573 回答
8

您还应该将立即标记添加到您的取消按钮,以便表单在执行取消操作之前不会运行任何验证。

<apex:commandButton action="{!cancel}" immediate="true" value="Cancel"/>

请参阅http://blogs.developerforce.com/developer-relations/2008/12/using-the-immediate-attribute-on-commandlinks-and-commandbuttons.html

于 2012-05-21T13:12:29.717 回答
1

在应用取消操作visualforce时,您应该停止表单验证。根据您的要求使用以下任何一种方法来停止表单验证。

方法一:

在 visualforce 页面的 doctype 中使用 html-5 意味着您应该使用html-formnovalidateimmediate取消按钮。例如

<apex:commandButton action="{!cancel}" value="Cancel" immediate="true" 
                    html-formnovalidate="formnovalidate" />

方法二:

您应该immediate只需要使用关键字来停止表单验证。例如

 <apex:commandButton action="{!cancel}" value="Cancel" immediate="true"/>
于 2015-04-10T07:08:10.143 回答
0

其他答案之一建议调用标准控制器的取消操作,因此我想对此进行扩展,因为它使我朝着解决类似问题的方向前进。

如果您想取消作为 ajax 请求的编辑而不刷新整个页面,请将操作声明为 void 并且不返回页面引用,但仍然在标准控件上调用“取消”操作。确保命令按钮指定了重新渲染属性。

// controller extension
public class TimeSheetExtension
{
  ApexPages.standardController m_sc = null;

  public TimeSheetExtension(ApexPages.standardController sc)
  {
    m_sc = sc;
  }

  public void doCancel()
  {
    m_sc.cancel();
  }
}

// page
<apex:commandButton action="{!doCancel}" value="Cancel" rerender="container_id"/>

于 2021-04-13T01:44:29.480 回答