Ajax呈现属性在JSF2中的h:dataTable中不起作用

时间:2010-03-11 12:14:02

标签: java jsf jsf-2 glassfish

我在JSF 2.0中使用一个简单的应用程序时遇到了一些问题。

我尝试使用ajax支持构建ToDo列表。我有一些todo字符串,我使用数据表显示。在这个数据表中,我有一个commandLink来删除任务。现在的问题是数据表不会被重新渲染。

    <h:dataTable id="todoList" value="#{todoController.todos}" var="todo">
        <h:column>
                <h:commandLink value="X" action="#{todoController.removeTodo(todo)}">
                    <f:ajax execute="@this" render="todoList" />
                </h:commandLink>
        </h:column>
        <h:column>
            <h:outputText value="#{todo}"/>
        </h:column>
    </h:dataTable>

感谢您的帮助。

编辑(TodoController):

@ManagedBean
@SessionScoped
public class TodoController {

private String todoStr;
private ArrayList<String> todos;

public TodoController() {
    todoStr="";
    todos = new ArrayList<String>();
}

public void addTodo() {
    todos.add(todoStr);
}

public void removeTodo(String deleteTodo) {
    todos.remove(deleteTodo);
}

/* getter / setter */
}

7 个答案:

答案 0 :(得分:9)

(看起来我没有足够的声誉评论别人的答案)

我认为FRotthowe建议用另一个元素包装表,并使用绝对引用(即命名文档根目录中的所有父容器)从&lt; f:ajax&gt;引用它。标签

这样的事情:

<h:form id="form">
    <h:panelGroup id ="wrapper">
        <h:dataTable value="#{backingBean.data}" var="list">
            <h:column>
                <h:commandButton value="-" action="#{backingBean.data}">
                    <f:ajax render=":form:wrapper"/>
                </h:commandButton>
            </h:column>
    </h:dataTable>
    </h:panelGroup>
</h:form>

但是,使用绝对引用始终是问题的根源,并随着视图的增长呈指数级地增加重构时间。

是否有办法从&lt; f:ajax&gt;中呈现表格? tag(阻止jsf在ajax事件中添加那些恼人的“:number_of_row”)?

答案 1 :(得分:2)

我认为&lt; h:dataTable&gt; 是否包含在表单中?

在对这个问题进行了一段时间的努力之后,我搜索了一个简单而干净的解决方案:将 @form 添加到&lt; f:ajax&gt;的呈现属性中,瞧!

完整示例:

<h:form id="theForm">
    <h:dataTable id="table" value="#{tasksMgr.allTasks}" var="task">

        <h:column>
           #{task.name}
        </h:column>

        <h:column>
            <h:commandButton id="removeTaskButton" value="X" action="#{tasksMgr.removeTask(task)}">
                <f:ajax render="@form" />
            </h:commandButton>
        </h:column>
    </h:dataTable>
</h:form>

帮助我的完整文章在这里:http://blogs.steeplesoft.com/2009/10/jsf-2-hdatatable-and-ajax-updates/

答案 2 :(得分:2)

使用此代码:

<h:form id="form">
<h:panelGroup id="panel">
    <h:dataTable id="todoList" value="#{todoController.todos}" var="todo">
        <h:column>
                <h:commandLink value="X" action="#{todoController.removeTodo(todo)}">
                    <f:ajax render=":form:panel" />
                </h:commandLink>
        </h:column>
        <h:column>
            <h:outputText value="#{todo}"/>
        </h:column>
    </h:dataTable>
</h:panelGroup>
</h:form>

答案 3 :(得分:1)

我遇到了同样的问题并且认为如果你把一个元素(例如)放在桌子周围并重新渲染它,它就会起作用。然而,我不太确定为什么会这样。任何人吗?

答案 4 :(得分:1)

由于某种原因,JSF正在修改&lt; f:ajax&gt;中的id。按钮中的标签。如果我写这个xhtml:

<h:form id="form">
    <h:commandButton value="Button">
        <f:ajax render="table"/>
    </h:commandButton>

    <h:dataTable id="table" value="#{tableData.data}">
        <h:column>
            <h:commandButton value="Button">
                <f:ajax render="tocoto"/>
            </h:commandButton>
        </h:column>
    </h:dataTable>
</h:form>

我得到这个HTML:

<form id="j_idt7" name="j_idt7" method="post" action="/WebApplication1/faces/index.xhtml" enctype="application/x-www-form-urlencoded">
    <input type="hidden" name="j_idt7" value="j_idt7" />
    <input id="j_idt7:j_idt8" type="submit" name="j_idt7:j_idt8" value="Button" onclick="mojarra.ab(this,event,'action',0,'j_idt7:table');return false" />

    <table id="j_idt7:table"><tbody>
        <tr>
            <td><input id="j_idt7:table:0:j_idt10" type="submit" name="j_idt7:table:0:j_idt10" value="Button" onclick="mojarra.ab(this,event,'action',0,'j_idt7:table:0');return false" /></td>
        </tr>
    </tbody></table>

     <input type="hidden" name="javax.faces.ViewState" id="javax.faces.ViewState" value="-7634557012794378167:1020265910811114103" autocomplete="off" />
</form>

请注意,第二个按钮中的id是'j_idt7:table:0',而我希望得到'j_idt7:table',就像我在第一个按钮中那样。

这不能解决问题,但可能有助于找到解决方法。

答案 5 :(得分:0)

您需要在您的h:form

中添加prependId =“false”

答案 6 :(得分:0)

我遇到了同样的问题。

“有效”的一件事是将richfaces添加到您的项目中,并替换

h:datatable 

通过

rich:datatable. 

将生成正确的ID。

相关问题