【问题标题】:How to make a edit button in a h:dataTable to make a row editable如何在 h:dataTable 中制作编辑按钮以使行可编辑
【发布时间】:2019-05-15 08:17:46
【问题描述】:

我有一个 HtmlDatatable

  • 一个命令按钮可以编辑。
  • 一个命令按钮保存。
  • 一个 InputText 来显示信息。
    <h:form>            
      <h:dataTable var="entity" value="#{bean.entities}">
        <h:column>
         <f:facet name="header">ColumnA</f:facet>
         <h:commandButton value="edit" actionListenner="#{bean.edit()}" />
         <hcommandButton value="save" actionListenner="#{bean.save(entity)} rendered="false"/>
         <h:inputText value="#{entity.value}"/>
         </h:column>
      </h:dataTable>            
    </h:form>

我希望当我单击 CommandButton 编辑时出现 CommandButton 保存。如何从 bean 访问我的方法 edit() 中的命令按钮保存?

  • 唯一的方法是从按钮传递 clientId。
  • 或者是否有另一种方法,将对象本身作为 EL 的参数传递并直接在方法中使用它? #{bean.edit(buttonSave)}
    public void edit()
    {
      //TODO get the button save from the same row as the button triggered.

      //TODO switch rendered to true.
    }

【问题讨论】:

  • 了解 ajax...

标签: jsf


【解决方案1】:

不要尝试在 edit() 方法中访问您的命令按钮保存。向您的实体添加一个名为 editMode 之类的布尔属性,并尝试像这样将您的实体传递给您的编辑方法

<h:commandButton value="edit" actionListenner="#{bean.edit(entity)}" />

现在在您的 edit() 方法中,将您的实体添加为参数并在您的实体上启用编辑模式

public void edit(Entity entity)
{
    entity.setEditMode(true);
}

现在可以使用 EL 使你的 commandButton 的渲染属性像这样检查你的实体的 editMode

<h:commandButton value="edit" actionListenner="#{bean.edit(entity)}" rendered="#{!entity.editMode}"/>    
<h:commandButton value="save" actionListenner="#{bean.save(entity)}" rendered="#{entity.editMode}"/>

因此,如果 editMode 属性为 true,您现在应该会看到保存按钮。与隐藏编辑按钮的方式相同

【讨论】:

  • 感谢您的回答,它看起来不错,但我想知道 editMode 字段位于何处,它位于用“@Entity”对象注释的对象内吗?它是自动出现在对象中还是由开发人员添加的?
  • 它是由开发者添加的。如果您使用的是 JPA,您可以将注释 @Transient 添加到属性中,这样它就不会持久化到数据库中
  • 感谢您提供此解决方案,这是一个很好的解决方案。我还认为实体不应包含更改 UI 的变量。所以它应该包含在 Bean 视图本身中。所以我在bean视图中放了一个变量。现在我真的不知道只有一行处于编辑模式应该怎么做?所以我的原始问题仍然悬而未决。
猜你喜欢
  • 2014-06-27
  • 2016-01-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-12-25
  • 2023-03-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多