简体   繁体   中英

Primefaces datatable filterBy with the displayed velue

I'm working with a datatable and dynamics columns <p:columns/> and I have a filterBy on each columns. But some columns of my table have a formatted value (example: 0 and 1 to the db and displayed as "No" and "Yes") so the filterBy used the db value. I use a converter to format my value. edit: I use TMX key to display value in different language. This is a part of my problem.

Here my HTML

<p:dataTable 
                        id="employeeBeanPageItems" 
                        styleClass="table"
                        value="#{staffListController.model.staffSearch}" 
                        rows="15"
                        sortBy="#{_item.stfFullName}" 
                        var="_item"
                        draggableColumns="true"
                        widgetVar="itemsTable" 
                        selectionMode="single" 
                        rowKey="#{_item.stfId}"
                        resizableColumns="true"
                        scrollable="false"
                        tableStyle="width:auto"
                        emptyMessage="#{msg['error.no-result']}"

                        paginator="true"
                        paginatorTemplate="{CurrentPageReport} {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}"
                        rowsPerPageTemplate="10,15,20,50">

                        <p:ajax event="rowSelect" listener="#{staffListController.onRowSelect}" />     
                        <p:ajax event="colReorder" listener="#{staffListController.onColumnReorder}" update=":search:menuColonne"/>             
                        <p:columns  filterMatchMode="contains" headerText="#{msg[column.header]}" value="#{staffListController.columns}" var="column" columnIndexVar="colIndex" sortBy="#{_item[column.property]}" filterBy="#{_item[column.property]}">
                            <h:outputText value="#{_item[column.property]}" converter="StaffListConverter"/>              
                        </p:columns>
                    </p:dataTable>

Here my converter

@Override
public String getAsString(FacesContext context, UIComponent component, Object value) {
    // TODO Auto-generated method stub
        Label label = new Label(value.toString());

    if (value.equals("1") || value.equals("Y"))
    {
        label.setLabel(getRessourceBundle().getString("common.yes"));
    }
    else if (value.equals("0") || value.equals("N"))
    {
        label.setLabel(getRessourceBundle().getString("common.no"));
    }
    else if (value.equals("EMPLOYEE"))
    {
        label.setLabel(getRessourceBundle().getString("staff.employee"));
    }
    else if (value.equals("COMPDEV"))
    {
        label.setLabel(getRessourceBundle().getString("staff.cd"));
    }
    else if (value.equals("MAN"))
    {
        label.setLabel(getRessourceBundle().getString("MAN"));
    }
    else if (value.equals("WOMAN"))
    {
        label.setLabel(getRessourceBundle().getString("WOMAN"));
    }
    else if (value.equals("UNKNOWN"))
    {
        label.setLabel(getRessourceBundle().getString("UNKNOWN"));
    }

    return label.getLabel();
}

   /**
     * Label is to create an object with a String
    */  
  static public class Label implements Serializable {

        private static final long serialVersionUID = 1L;
        @Getter @Setter private String label;

        /**
         * Public constructor of Label 
         * @param String label
         *
         */
      public Label(String label) {
          this.label = label;

      }

        /**
         * Empty constructor
         *
         */
      public Label() {}

        @Override
        public String toString() {
            return label;
        }
  }

}

Maybe I should used a different way to format my data but I have no idea how. The aim stay to allow the filterBy using.

add: I tried the solution given by the first commentary with the following code in my model

public String getStfResourceLaptopFormat() {
   if (stfResourceLaptop != null)
   {
       if (stfResourceLaptop.equals("1"))
       {
           return "common.yes";
       }
       else if(stfResourceLaptop.equals("0"))
       {
           return "common.no";
       }
       else return null;
   }
   else
   {
       return null;
   }

}

The problem is that I have to return a TMK key then the filterBy use the key :/

<p:columns  filterMatchMode="contains" headerText="#{msg[column.header]}" value="#{staffListController.columns}" var="column" columnIndexVar="colIndex" sortBy="#{_item[column.property]}" filterBy="#{_item[column.property]}">
                            <h:outputText value="#{msg[_item[column.property]]}" />               
                        </p:columns>

Converter only convert the displayed value getAsString method does not change the object. So you have to change the value at creating the list of staffListController.model.staffSearch . When you fetching the value from DB change there itself in List

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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