Lucke
Lucke

Reputation: 316

Primefaces datatable LazyDataModel create new row

I have a primefaces datatable which uses LazyDataModel instead Collection.

I need to implement a button which create a new row in the table (empty row in last row of actual page in datatable).

I have tried adding.

@Component("table")
@Scope("session")
public class TablaPaginada implements Serializable {
    private LazyDataModel<User> users;
    @PostConstruct
    private void init() {
        usuarios = new LazyDataModel<User>() {
            private static final long serialVersionUID = 8885722005055879976L;
            @Override
            public List<User> load(int first, int pageSize, String sortField,
                    SortOrder sortOrder, Map<String, Object> filters) {
                List<User> data = getUsers(first, pageSize, sortOrder, filters);
                return data;
            }
        };
        users.setRowCount(totalRowUsers());
    }
    public void newRow() {
        //I would do this if it was List instead LazyDataModel
        //this.users.add(new User());
        users.getWrappedData().add(new User()); //this does not work.
    }
}

This would be the xhtml code:

<h:form>
<p:dataTable rowsPerPageTemplate="5,10,15" value="#{table.users}" var="user" paginator="true"
             paginatorTemplate="{RowsPerPageDropdown} {FirstPageLink} {PreviousPageLink} {CurrentPageReport} {NextPageLink} {LastPageLink}"
             rows="5" lazy="true">

    <p:column headerText="id" sortBy="#{user.id}" filterBy="#{user.id}">
        <h:outputText value="#{user.id}" />
    </p:column>
    <p:column headerText="name" sortBy="#{user.name}" filterBy="#{user.name}">
        <h:outputText value="#{user.name}" />
    </p:column>
    <p:column headerText="lastName" sortBy="#{user.lastName}" filterBy="#{user.lastName}">
        <h:outputText value="#{user.lastName}" />
    </p:column>
    <p:column headerText="money" sortBy="#{user.money}" filterBy="#{user.money}">
        <h:outputText value="#{user.money}" />
    </p:column>
</p:dataTable>
<p:commandButton value="new row" action="#{table.newRow()}" update="@form"/>
</h:form>

EDIT:

my code does not create new row.

Upvotes: 0

Views: 1076

Answers (1)

Rapster
Rapster

Reputation: 524

From PF documentation: <p:commandButton value="Add" actionListener="#{dtBasicView.addCar}" oncomplete="PF('dt').addRow()" process="@this"/>

And for Lazy Datatable you may run into this issue which has a workaround: https://github.com/primefaces/primefaces/issues/3901

Upvotes: 1

Related Questions