简体   繁体   English

Datatable中的Primefaces全局搜索不起作用

[英]Primefaces global search in Datatable not working

This is my xhtml page 这是我的xhtml页面

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:p="http://primefaces.org/ui"
      xmlns:f="http://java.sun.com/jsf/core">

  <h:body>
    <ui:composition template="#{templateSelection.selectedTemplate}">
      <ui:define name="docTitle">User Management</ui:define>
      <ui:define name="body">

        <h:form>

          <p:dataTable id="usrDT" var="usr" value="#{userListController.userBeans}"
                       rows="15" paginator="true"
                       rowIndexVar="row"
                       paginatorTemplate="{CurrentPageReport}  {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}"
                       paginatorPosition="bottom"
                       filteredValue="#{userListController.filteredUserBeans}"
          >
            <f:facet name="header">
              User Management
              <p:outputPanel>
                <h:outputText value="Search all fields:"/>
                <p:inputText id="globalFilter" onkeyup="PF('usrDT').filter()" style="width:150px"
                             placeholder="Enter keyword"/>
              </p:outputPanel>
            </f:facet>
            <p:column filterBy="#{row + 1}" headerText="Sl No" width="4%">
              <h:outputText value="#{row + 1}"/>
            </p:column>
            <p:column filterBy="#{usr.userName}" headerText="User Name" width="30%">
              <h:outputText value="#{usr.userName}"/>
            </p:column>
            <p:column filterBy="#{usr.userLoginId}" headerText="User ID" width="8%">
            <h:outputText value="#{usr.userLoginId}"/>
          </p:dataTable>
        </h:form>
      </ui:define>
    </ui:composition>
  </h:body>
</html>

This is my bean 这是我的豆

@ManagedBean
@ViewScoped
public class UserListController {

    @ManagedProperty("#{userServiceLayer}")
    private UserServiceLayer userServiceLayer;
    private List<UserBean> userBeans;
    private List<UserBean> filteredUserBeans;

    @PostConstruct
    public void init() {
        userBeans = userServiceLayer.getAllUserListForSearch(state, sessionScId);
    }

    public List<UserBean> getFilteredUserBeans() {
        return filteredUserBeans;
    }

    public void setFilteredUserBeans(List<UserBean> filteredUserBeans) 
    {
        this.filteredUserBeans = filteredUserBeans;
    }

    public List<UserBean> getUserBeans() {
        return userBeans;
    }

    public void setUserBeans(List<UserBean> userBeans) {
        this.userBeans = userBeans;
    }
}

I am creating a datatable which shows the user list and i want here to add search functionality. 我正在创建一个显示用户列表的数据表,我想在这里添加搜索功能。 I have added this as describes here https://www.primefaces.org/showcase/ui/data/datatable/filter.xhtml . 我已按照https://www.primefaces.org/showcase/ui/data/datatable/filter.xhtml的说明进行添加。 Searching in Single column is working fine...But global searching is not working .... 在单列中搜索可以正常工作...但是全局搜索不起作用....

There is an attribute called widgetVar which is help you to solve the above problem. 有一个名为widgetVar的属性,可以帮助您解决上述问题。 you have to use like below Add widgetVar in your datatable widgetVar="usrDT" (it will enable filter option in your grid) 您必须像下面那样使用在数据表中添加widgetVar widgetVar =“ usrDT” (它将在网格中启用过滤器选项)

if you want to clear the filter then you can use below code RequestContext.getCurrentInstance().execute("PF('usrDT').clearFilters()"); 如果要清除过滤器,则可以使用下面的代码RequestContext.getCurrentInstance()。execute(“ PF('usrDT')。clearFilters()”);

NOTE: widgetVar value is userdefined 注意: widgetVar值是用户定义的

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

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