如何从ManagedBean获取primefaces数据表列的顺序和宽度

 手机用户2502855107 发布于 2023-01-29 16:56

我正在使用primefaces 4.0,JSF Mojarra 2.2.2,这是我的数据代码:





   
             
   

 


这是我到目前为止所尝试的,但是我在控制台中得到空的字符串getHeaderText并且空宽度,我想知道我错过了什么

UIViewRoot view = FacesContext.getCurrentInstance().getViewRoot();
    DataTable tabler = (DataTable) view.findComponent(":form1:tabexam");
    List coler = tabler.getColumns();

    for (int i = 0; i < coler.size(); i++) {

        System.out.println("/////////////////");
        System.out.println(coler.get(i).getValueExpression("headerText").getValue(FacesContext.getCurrentInstance().getELContext()));
        System.out.println(coler.get(i).getHeaderText());
        System.out.println(coler.get(i).isRendered());
        System.out.println(coler.get(i).isResizable());
        System.out.println(coler.get(i).getWidth());
        System.out.println("/////////////////");

    }
 System.out.println(coler.size());

请注意,它coler.size()给出了数据表中显示的列数.但coler.get(i).getHeaderText()总是给出空字符串.

1 个回答
  • 你错了什么:

      不要p:column在里面筑巢p:columns

      DataTable.getColumns不会返回您期望的内容:

      public List<UIColumn> getColumns() {
          if(columns == null) {
              columns = new ArrayList<UIColumn>();
              FacesContext context = getFacesContext();
              char separator = UINamingContainer.getSeparatorChar(context);
      
              for(UIComponent child : this.getChildren()) {
                  if(child instanceof Column) {
                      columns.add((UIColumn) child);
                  }
                  else if(child instanceof Columns) {
                      Columns uiColumns = (Columns) child;
                      String uiColumnsClientId = uiColumns.getClientId(context);
      
                      for(int i=0; i < uiColumns.getRowCount(); i++) {
                          DynamicColumn dynaColumn = new DynamicColumn(i, uiColumns);
                          dynaColumn.setColumnKey(uiColumnsClientId + separator + i);
                          columns.add(dynaColumn);
                      }
                  }
              }
          }
      
          return columns;
      }
      

    这将返回一个合成仿真:仅columnKey按列,并且如您所述,返回正确的列数.

    一般来说,p:columns是一个组件,当你从控制器层访问它时,你必须引用它的DECLARATION(单个组件)而不是它的REPRESENTATION(多个列)

      因为你支持p:columns你,#{datatableBean.table}你应该已经拥有了你想要从DataTable组件获得的所有信息,这是以逆向工程方式提供的.

    更新

    哦,现在我意识到你想做什么.你必须使用事件监听器.

    来自PF展示:

    <p:dataTable var="car" value="#{tableBean.carsSmall}" widgetVar="cars" draggableRows="true">  
    
        <p:ajax event="colReorder" listener="#{tableBean.onColReorder}" update=":some:component" />  
    
        <p:ajax event="colResize" listener="#{tableBean.onColResize}" update=":some:component"/>    
    
        <!-- columns here -->  
    </p:dataTable> 
    

    在支持bean中监听这些事件并保持这些信息可以解决您的问题

    更新2

    哇,这一点都不简单.

    由于Primefaces错误的动态列调整大小(至少我的尝试)和不完整的列重新排序事件实现,这是出来的:

    <h:form id="form">
        <h:panelGroup id="model">
            <ui:repeat var="column2" value="#{testBean.columnList2}">
                <div>#{column2.title} - #{column2.width}</div>
            </ui:repeat>
        </h:panelGroup>
    
        <p:dataTable var="elem" value="#{testBean.elementList}" resizableColumns="true" draggableColumns="true">
            <p:ajax event="colReorder" listener="#{testBean.onColumnReorder}" update=":form:model" />  
            <p:ajax event="colResize" listener="#{testBean.onColumnResize}" update=":form:model"/>  
    
            <c:forEach var="column" items="#{testBean.columnList}">
                <p:column headerText="#{column.title}" rendered="#{column.visible}" >
                    <f:attribute name="myCol" value="#{column}"/>
                    <span>#{elem[column.property]}</span>
                </p:column>
            </c:forEach>
        </p:dataTable>
    </h:form>
    

    而这个豆子:

    @ManagedBean
    @ViewScoped
    public class TestBean implements Serializable
    {
        private static final long serialVersionUID = 1L;
    
        private List<MyColumn> columnList;
        private List<MyColumn> columnList2;
        private List<MyElement> elementList;
    
        public class MyColumn
        {
            private String title;
            private String property;
            private boolean visible = true;
            private boolean resizable = true;
            private int width = 100;
    
            public MyColumn(String title, String property, boolean visible, boolean resizable, int width)
            {
                super();
                this.title = title;
                this.property = property;
                this.visible = visible;
                this.resizable = resizable;
                this.width = width;
            }
    
            // getters and setters
        }
    
        public class MyElement
        {
            private String code;
            private String name;
            private String type;
    
            public MyElement(String code, String name, String type)
            {
                super();
                this.code = code;
                this.name = name;
                this.type = type;
            }
    
            // getters and setters
        }
    
        @PostConstruct
        public void init()
        {
            columnList = new ArrayList<>();
            columnList.add(new MyColumn("element code", "code", true, true, 100));
            columnList.add(new MyColumn("element name", "name", true, true, 100));
            columnList.add(new MyColumn("element type", "type", true, true, 100));
    
            columnList2 = new ArrayList<>(columnList);
    
            elementList = new ArrayList<>();
            elementList.add(new MyElement("001", "foo", "normal"));
            elementList.add(new MyElement("002", "bar", "strange"));
            elementList.add(new MyElement("003", "xyz", "normal"));
            elementList.add(new MyElement("004", "abc", "nothing"));
        }
    
        public void onColumnResize(ColumnResizeEvent event)
        {
            UIColumn column = event.getColumn();
            int width = event.getWidth();
    
            UIComponent colComponent = (UIComponent) column;
            MyColumn myCol = (MyColumn) colComponent.getAttributes().get("myCol");
    
            myCol.setWidth(width);
        }
    
        public void onColumnReorder(AjaxBehaviorEvent event)
        {
            DataTable table = (DataTable) event.getSource();
    
            columnList2.clear();
    
            for(UIColumn column : table.getColumns())
            {
                UIComponent colComponent = (UIComponent) column;
    
                MyColumn myCol = (MyColumn) colComponent.getAttributes().get("myCol");
                columnList2.add(myCol);
            }
        }
    
        // getters and setters
    
    }
    

    更新的代码,您可以在中找到有序列columnList2.现在也显示在表格中.现在你可以columnList2用来存储oreder和width到db.

    2023-01-29 16:59 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有