2016-11-21 87 views
2

替代的ListView表格單元格我有一個具有它只需要在某些情況下可編輯單個列列表視圖。如果用戶需要更改列,我希望他們單擊​​該行的編輯按鈕,然後用textField替換單元格中的標籤。當我調用替換時,我可以看到TextField現在已經在標籤的位置,儘管它永遠不會呈現。我有一個AjaxLink來處理事件。我正在使用容器重新繪製listView。這裏是我的列表視圖:動態框文本檢票

parent = new WebMarkupContainer("emp-table-parent"); 

    parent.add(new AjaxLink<Object>(FIRST_NAME_HEADER_LINK) { 

     /** 
     * 
     */ 
     private static final long serialVersionUID = -1937727929649333407L; 

     @Override 
     public void onClick(AjaxRequestTarget target) { 

      changeGlyphUpdateList(target, parent.get(FIRST_NAME_HEADER_LINK).get(FIRST_NAME_HEADER_ICON), 
        parent.get(LAST_NAME_HEADER_LINK).get(LAST_NAME_HEADER_ICON), 
        parent.get(EMAIL_HEADER_LINK).get(EMAIL_HEADER_ICON), parent.get(ELIGIBILITY_CLASS_HEADER_LINK).get(ELIGIBILITY_CLASS_HEADER_ICON), 
        parent.get(EMPLOYER_HEADER_LINK).get(EMPLOYER_HEADER_ICON)); 

     } 

    }.add(new Label("first-name-header-label", Model.of("First Name")), new WebComponent(FIRST_NAME_HEADER_ICON)), 
      new AjaxLink<Object>(LAST_NAME_HEADER_LINK) { 

       /** 
       * 
       */ 
       private static final long serialVersionUID = -3438649095509412910L; 

       @Override 
       public void onClick(AjaxRequestTarget target) { 

        changeGlyphUpdateList(target, parent.get(LAST_NAME_HEADER_LINK).get(LAST_NAME_HEADER_ICON), 
          parent.get(FIRST_NAME_HEADER_LINK).get(FIRST_NAME_HEADER_ICON), 
          parent.get(EMAIL_HEADER_LINK).get(EMAIL_HEADER_ICON), 
          parent.get(ELIGIBILITY_CLASS_HEADER_LINK).get(ELIGIBILITY_CLASS_HEADER_ICON), 
          parent.get(EMPLOYER_HEADER_LINK).get(EMPLOYER_HEADER_ICON)); 

       } 
    }.add(new Label("last-name-header-label", Model.of("Last Name")), new WebComponent(LAST_NAME_HEADER_ICON)), 
      new AjaxLink<Object>(EMAIL_HEADER_LINK) { 

       /** 
       * 
       */ 
       private static final long serialVersionUID = 2890934302751793454L; 

       @Override 
       public void onClick(AjaxRequestTarget target) { 

        changeGlyphUpdateList(target, parent.get(EMAIL_HEADER_LINK).get(EMAIL_HEADER_ICON), 
          parent.get(LAST_NAME_HEADER_LINK).get(LAST_NAME_HEADER_ICON), 
          parent.get(FIRST_NAME_HEADER_LINK).get(FIRST_NAME_HEADER_ICON), 
          parent.get(ELIGIBILITY_CLASS_HEADER_LINK).get(ELIGIBILITY_CLASS_HEADER_ICON), 
          parent.get(EMPLOYER_HEADER_LINK).get(EMPLOYER_HEADER_ICON)); 

       } 

    }.add(new Label("email-header-label", Model.of("Email")), new WebComponent(EMAIL_HEADER_ICON)), 
      new AjaxLink<Object>(ELIGIBILITY_CLASS_HEADER_LINK) { 

       /** 
       * 
       */ 
       private static final long serialVersionUID = -4022209586109961448L; 

       @Override 
       public void onClick(AjaxRequestTarget target) { 

        changeGlyphUpdateList(target, parent.get(ELIGIBILITY_CLASS_HEADER_LINK).get(ELIGIBILITY_CLASS_HEADER_ICON), 
          parent.get(EMAIL_HEADER_LINK).get(EMAIL_HEADER_ICON), 
          parent.get(LAST_NAME_HEADER_LINK).get(LAST_NAME_HEADER_ICON), 
          parent.get(FIRST_NAME_HEADER_LINK).get(FIRST_NAME_HEADER_ICON), 
          parent.get(EMPLOYER_HEADER_LINK).get(EMPLOYER_HEADER_ICON)); 

       } 

    }.add(new Label("eligibility-class-header-label", Model.of("Elig. Class")), new WebComponent(ELIGIBILITY_CLASS_HEADER_ICON)), 
      new AjaxLink<Object>(EMPLOYER_HEADER_LINK) { 

       /** 
       * 
       */ 
       private static final long serialVersionUID = -738777257301408437L; 

       @Override 
       public void onClick(AjaxRequestTarget target) { 

        changeGlyphUpdateList(target, parent.get(EMPLOYER_HEADER_LINK).get(EMPLOYER_HEADER_ICON), 
          parent.get(ELIGIBILITY_CLASS_HEADER_LINK).get(ELIGIBILITY_CLASS_HEADER_ICON), 
          parent.get(EMAIL_HEADER_LINK).get(EMAIL_HEADER_ICON), 
          parent.get(LAST_NAME_HEADER_LINK).get(LAST_NAME_HEADER_ICON), 
          parent.get(FIRST_NAME_HEADER_LINK).get(FIRST_NAME_HEADER_ICON)); 

       } 

    }.add(new Label("employer-header-label", Model.of("Employer")), new WebComponent(EMPLOYER_HEADER_ICON)), 
    new PageableListView<EmployeeSummaryPkt>("data", employeeSummaryModel.getObject(), 25) { 

       /** 
       * 
       */ 
       private static final long serialVersionUID = -1697070076764699904L; 

       @Override 
       protected void populateItem(final ListItem<EmployeeSummaryPkt> item) { 

        item.setDefaultModel(new CompoundPropertyModel<EmployeeSummaryPkt>(item.getModelObject())); 

        item.add(new Label("firstName"), 
          new Label("lastName"), 
          new Label("employeeEmail"), 
          new Link<Object>("eligibility-class-data-link") { 

           /** 
           * 
           */ 
           private static final long serialVersionUID = -3842291392813313171L; 

           @Override 
           public void onClick() { 

            //LINK TO ELIGIBILITY CLASS OR MAYBE THE SECTION WITHIN THE EMP? 

           } 
        }.add(new Label("employeeEligibilityClassSummaryPkt.name")), 
          new Link<Object>("employer-data-link") { 

           /** 
           * 
           */ 
           private static final long serialVersionUID = 6809571267919974106L; 

           @Override 
           public void onClick() { 

            getIndex().getHomePanel().setNewContent(new EmployerDetailPanel("panel-content", item.getModelObject().getEmployerSummaryPkt().getId())); 

           } 

        }.add(new Label("employerSummaryPkt.name")), 
          new Label("employeeDateOfBirth"), 
          new Label("employee-code", Model.of(item.getModelObject().getEmployeeName())).setOutputMarkupId(true), 
          new AjaxLink<Object>("edit-employee-link") { 

           /** 
           * 
           */ 
           private static final long serialVersionUID = 6061544430700059358L; 

           @Override 
           public void onClick(AjaxRequestTarget target) { 

            logr.log(Level.FINER, "onClick for edit employee"); 

            logr.log(Level.FINER, "employee code pre: " + item.get("employee-code").getClass().getSimpleName()); 
            item.get("employee-code").replaceWith(new TextField<String>("employee-code", new Model<String>(item.getModelObject().getEmployeeName())).setOutputMarkupId(true)); 
            logr.log(Level.FINER, "employee code post: " + item.get("employee-code").getClass().getSimpleName()); 
            target.addChildren(parent, TextField.class); 
            target.add(parent); 
           } 
        }); 
       } 

正如你可以看到,id爲「員工代碼」的標籤是我想要替換的標籤。在AjaxLink onClick中,您可以看到我獲取標籤的位置並將其替換。沒有什麼改變。任何方向或幫助非常感謝。

+0

的聲明'parent'似乎並沒有在代碼片段你貼? – WiseTree

+0

對不起。我編輯上面添加父代的片段。 – SteveManC

回答

3

一個ListView重新創建每個如果每個項目呈現,因此改變的listItem立即扔掉。

ListView#setReuseItems(true)應該有所幫助。

+0

正是我在找的東西。謝謝。希望這可以幫助有類似問題的人。 – SteveManC

2

到svenmeier的回答另一種也可能是隻添加已改爲AjaxRequestTarget特定項目。也就是說,更換

target.addChildren(parent, TextField.class); 
target.add(parent); 

隨着

target.add(item.get("employee-code")) 

這將導致只有一塊標記的是涉及變更重新呈現,而不是整個表。如果你的表很大並且包含很多元素,其模型涉及複雜的檢索機制,那麼渲染整個表將是一個更加費力的過程,因此只重新渲染項目將是更好的解決方案。

說了這麼多,你的具體情況svenmeier的解決方案是更好的,否則如果實現我的解決方案,稍後重新渲染整個表,更改將丟失。

+1

我同意,這是我在過去使用的方法,但它逃過了我,因爲我已經內置到該表列功能排序,因此爲什麼我與整個表上添加父。感謝你的回答。我相信@ svnemeier的答案會更合適,因爲如果他們排序,那麼textfield將被替換爲默認標籤。非常好的答案。 – SteveManC