【问题标题】:Primefaces datatable filterBy with the displayed veluePrimefaces 数据表过滤器与显示的值
【发布时间】:2016-08-10 09:31:30
【问题描述】:

我正在使用数据表和动态列<p:columns/>,并且我在每列上都有一个 filterBy。但是我表的某些列具有格式化值(例如:0 和 1 到 db 并显示为“No”和“Yes”),因此 filterBy 使用了 db 值。我使用转换器来格式化我的值。 编辑:我使用 TMX 键以不同的语言显示值。这是我的问题的一部分。

这是我的 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>

这是我的转换器

@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;
        }
  }

}

也许我应该使用不同的方式来格式化我的数据,但我不知道如何。目标停留在允许 filterBy 使用。

添加: 我尝试了第一个评论给出的解决方案,在我的模型中使用以下代码

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;
   }

}

问题是我必须返回一个 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>

【问题讨论】:

    标签: jsf primefaces filter datatable


    【解决方案1】:

    Converter 只转换显示的值getAsString 方法不改变对象。因此,您必须在创建staffListController.model.staffSearch 列表时更改值。当您从 DB 中获取值时,List 中会发生变化

    【讨论】:

    • 感谢您的回答。是的,你是对的。我用以下示例尝试了类似的方法 public String getStfResourceLaptopFormat() { if (stfResourceLaptop != null) { if (stfResourceLaptop.equals("1")) { return "common.yes"; } else if(stfResourceLaptop.equals("0")) { return "common.no"; } 否则返回空值; } 否则 { 返回空值;问题是我必须返回一个 TMX 密钥,然后搜索附加 TMX 密钥
    猜你喜欢
    • 2017-01-21
    • 2014-01-04
    • 1970-01-01
    • 2013-08-01
    • 2012-11-01
    • 2014-03-01
    • 1970-01-01
    • 2015-01-21
    • 1970-01-01
    相关资源
    最近更新 更多