表格不想在输入搜索标签后更新

时间:2016-07-15 19:07:07

标签: jsf primefaces

我有一个奇怪的问题,我确定它与h:表单展示位置代码相关。我有一个表,在表格上方我有一个搜索表单,我可以在其中放置一些标签,例如名称或姓氏,然后刷新表格。它奏效了!但由于某些原因,它刚刚停止工作,我不知道为什么。现在,为了检查搜索结果,我必须刷新页面或将表格中的分页从10更改为15,之后将显示结果。这是一些代码:

XHTML:

<h:form>
         <div class="row">
            <div class="panel-heading text-center">
                <div class="bootstrap-filestyle input-group inn">
                    <div class="search-criteria" style="width: 500px">
                        <h:inputText value="#{clientBean.tags}"   styleClass="form-control"
                            type="text">
                            <f:passThroughAttribute name="placeholder"
                                value="Imię, nazwisko, adres..." />
                        </h:inputText>
                    </div>
                    <p:commandButton type="submit" style="float:left"
                        styleClass="btn btn-primary" value="Szukaj"
                        actionListener="#{clientBean.getAllClients()}">
                        <i class="icon-search icon-white"></i>
                    </p:commandButton>
                </div>
            </div>
          </div>

      <h:form>
        <p:dataTable id="clientsTable" style="white-space: nowrap"
            var="client" value="#{clientBean.getAllClients()}" paginator="true"
            rows="15"
            paginatorTemplate="{CurrentPageReport} {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}"
            rowsPerPageTemplate="10,15">

            <p:column headerText="Imię">
                <h:outputText value="#{client.name}" />
            </p:column>
            <p:column headerText="Nazwisko">
                <h:outputText value="#{client.lastName}" />
            </p:column>
            <p:column headerText="Numer telefonu">
                <h:outputText value="#{client.phoneNumber}" />
            </p:column>
            <p:column headerText="Adres">
                <h:outputText value="#{client.address}" />
            </p:column>
            <p:column>
                <a href="klienci/#{client.ID}"
                    class="btn btn-success edit resized-font"><span
                    class="glyphicon glyphicon-pencil"></span> Edytuj</a>

                <a href="klienci/#{client.ID}"
                    class="btn btn-danger delete resized-font"><span
                    class="glyphicon glyphicon-trash"></span> Usuń</a>

                <a href="klienci/#{client.ID}" class="btn btn-primary resized-font"><span
                    class="glyphicon glyphicon-book"></span> Informacje</a>
            </p:column>
        </p:dataTable>
        </h:form>
    </h:form>
来自ClientBean的重要代码:

private String tags;

public Set<Client> getAllClients() {

    if (tags == null) {
        Set<Client> clients = new HashSet<Client>(clientDao.findAll());
        return clients;
    }

    return getClients();
}

public  Set<Client> getClients() {

    Set<Client> mergedClientSet = new HashSet<>();
    String[] tags = getTags().split(" ");

    for(int i=0; i<tags.length; i++){
        mergedClientSet.addAll(searchService.getClientWithParameters(tags[i]));
    }

    return mergedClientSet;
}

public String getTags() {
    return tags;
}

public void setTags(String tags) {
    this.tags = tags;
}

1 个答案:

答案 0 :(得分:0)

首先,您违反了W3C XHTML规范,嵌套表单,您无法做到。

你想要达到的目标非常简单。

以下是您的工作方式:

JSF Facelet:

<h:form prependId="true" id="main-form">
     <div class="row">
        <div class="panel-heading text-center">
            <div class="bootstrap-filestyle input-group inn">
                <div class="search-criteria"
                     style="width: 500px">
                    <h:inputText value="#{clientBean.tags}" 
                                 styleClass="form-control">
                        <f:passThroughAttribute name="placeholder"
                                                value="Imię, nazwisko, adres..." />
                    </h:inputText>
                </div>
                <p:commandButton style="float:left"
                                 styleClass="btn btn-primary" value="Szukaj"
                                 actionListener="#{clientBean.doTagsSearch}" 
                                 update=":main-form:clients-table">
                    <i class="icon-search icon-white"></i>
                </p:commandButton>
            </div>
        </div>
      </div>

    <p:dataTable id="clients-table"
                 style="white-space: nowrap"
                 var="client"
                 value="#{clientBean.clients}"
                 paginator="true"
                 rows="15"
                 paginatorTemplate="{CurrentPageReport} {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}"
                 rowsPerPageTemplate="10,15">

        <p:column headerText="Imię">
            <h:outputText value="#{client.name}" />
        </p:column>
        <p:column headerText="Nazwisko">
            <h:outputText value="#{client.lastName}" />
        </p:column>
        <p:column headerText="Numer telefonu">
            <h:outputText value="#{client.phoneNumber}" />
        </p:column>
        <p:column headerText="Adres">
            <h:outputText value="#{client.address}" />
        </p:column>
        <p:column>
            <a href="klienci/#{client.ID}"
                class="btn btn-success edit resized-font"><span
                class="glyphicon glyphicon-pencil"></span> Edytuj</a>

            <a href="klienci/#{client.ID}"
                class="btn btn-danger delete resized-font"><span
                class="glyphicon glyphicon-trash"></span> Usuń</a>

            <a href="klienci/#{client.ID}" class="btn btn-primary resized-font"><span
                class="glyphicon glyphicon-book"></span> Informacje</a>
        </p:column>
    </p:dataTable>
</h:form>

ManagedBean代码段:

private String tags;
private ArrayList<Client> clients;

@PostConstruct
public void init() {
    clients = clientDao.findAll();//must return an ArrayList of Client
}

public void doTagsSearch() {
    if (tags == null) {
        clients = clientDao.findAll();
    } else {
        clients = getClientsByTags();
    }

}

public ArrayList<Client> getClientsByTags() {
   //use your tags logic.
   //must return an ArrayList of Clients.
     ...
}

public String getTags() {
    return tags;
}

public void setTags(String tags) {
    this.tags = tags;
}

public String getClients() {
    return clients;
}

public void setClients(ArrayList<Client> clients) {
    this.clients = clients;
}

}