简体   繁体   English

Ajax渲染属性在JSF2中的ah:dataTable中不起作用

[英]Ajax render attribute don't work in a h:dataTable in JSF2

I have some problem's with a simple application in JSF 2.0. 我在JSF 2.0中有一个简单的应用程序存在一些问题。

I try to build a ToDo List with ajax support. 我尝试用ajax支持构建待办事项列表。 I have some todo strings which I display using a datatable. 我有一些待办事项字符串,使用数据表显示。 Inside this datatable I have a commandLink to delete a task. 在此数据表中,我具有一个CommandLink来删除任务。 The problem is now that the datatable don't get re-rendered. 现在的问题是,数据表不会重新呈现。

    <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>

Thanks for your help. 谢谢你的帮助。

Edit (TodoController): 编辑(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 */
}

(Looks like I don't have enough reputation to comment on others' answers) (看起来我没有足够的声誉来评论他人的回答)

I think FRotthowe suggests wrapping the table with another element and referencing it using absolute reference (ie. naming all the parent containers from the root of the document) from the <f:ajax> tag. 我认为FRotthowe建议用另一个元素包装表,并使用<f:ajax>标记中的绝对引用(即,从文档根目录命名所有父容器)对其进行引用。

Something like this: 像这样:

<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>

But, using absolute references is always a source of problems and increases exponentially the refactoring time as the view grows. 但是,使用绝对引用始终是问题的根源,并且随着视图的增长,重构时间呈指数增长。

Isn't there a way to just render the table from a <f:ajax> tag (prevent jsf from adding those annoying ":number_of_row" in the ajax event)? 有没有办法只从<f:ajax>标记呈现表(防止jsf在ajax事件中添加那些烦人的“:number_of_row”)?

use this code: 使用此代码:

<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>

I assume that the <h:dataTable> is enclosed by a form? 我假设<h:dataTable>用表格括起来了吗?

After struggling with this issue for a while, I googled an easy and clean solution: add @form to render attribute of <f:ajax> and voila! 经过一段时间的努力后,我用一个简单干净的方法在Google上搜索:将@form添加到<f:ajax>的 render属性,瞧!

Full example: 完整示例:

<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>

The full article which helped me is here: http://blogs.steeplesoft.com/2009/10/jsf-2-hdatatable-and-ajax-updates/ 完整的文章对我有帮助: http//blogs.steeplesoft.com/2009/10/jsf-2-hdatatable-and-ajax-updates/

I ran into the same problem and figured that if you put an element (eg ) around the table and rerender that, it will work. 我遇到了同样的问题,并认为,如果您在表格周围放置一个元素(例如)并将其重新呈现,它将起作用。 I am however not quite sure why it is that way. 但是我不太确定为什么会这样。 Anyone? 任何人?

For some reason JSF is modifying the id in the <f:ajax> tag in the button. 由于某些原因,JSF正在修改按钮中<f:ajax>标记中的id。 If I write this xhtml: 如果我写这个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>

I get this html: 我得到这个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>

Notice that the id in the second button is 'j_idt7:table:0' while I'm expecting to get 'j_idt7:table' as I do in the first one. 请注意,第二个按钮中的id是“ j_idt7:table:0”,而我希望像第一个按钮中那样获得“ j_idt7:table”。

That doesn't solve the problem but may help to find a workaround. 那不能解决问题,但可能有助于找到解决方法。

您需要在您的h:form中添加prependId =“ false”

I ran into the same problem. 我遇到了同样的问题。

One thing that "works" is adding richfaces to your project, and replacing the “有效”的一件事是为项目添加丰富的外观 ,并替换

h:datatable 

by a 由一个

rich:datatable. 

Which will generate the correct id's. 这将生成正确的ID。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM