简体   繁体   English

Primefaces数据表filterBy与显示的velue

[英]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. 我正在使用数据表和动力学列<p:columns/> ,我在每列上都有一个filterBy。 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. 但我的表的某些列有一个格式化的值(例如:0和1到db并显示为“No”和“Yes”)所以filterBy使用了db值。 I use a converter to format my value. 我使用转换器来格式化我的价值。 edit: I use TMX key to display value in different language. 编辑:我使用TMX键以不同语言显示值。 This is a part of my problem. 这是我的问题的一部分。

Here my HTML 这是我的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. 目标留下来允许filterBy使用。

add: I tried the solution given by the first commentary with the following code in my model add:我尝试了第一个评论给出的解决方案,并在我的模型中使用了以下代码

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 :/ 问题是我必须返回TMK密钥然后filterBy使用密钥:/

<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. 转换器只转换显示值getAsString方法不改变对象。 So you have to change the value at creating the list of staffListController.model.staffSearch . 因此,您必须在创建staffListController.model.staffSearch列表时更改值。 When you fetching the value from DB change there itself in List 从DB中获取值时,会在List中更改自身

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

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