0

HI 下面的代码创建了一个动态数据表,其中没有基于所选国家的列。现在我想在将删除所选行的每一行之前添加命令按钮。我能够创建表,但无法在适当的位置添加命令按钮。当我添加命令按钮时,它会连续创建。

public void loadDynamicList() throws Exception {
    int i = 0;
    dynamicList = new ArrayList<List<String>>();
    dynamicList.add(Arrays.asList(new String[] { "ID1" }));
    existingCountryList = new ArrayList<Country>();
    String countryCode = "US";
    existingCountryList.add(getCountryService().getCountryByCode(
            countryCode));
    Country country = getCountryService().getCountryByCode(countryCode);
    countryLanguageSet = country.getCountryLanguage();
    i = country.getCountryLanguage().size();
    dynamicHeaders = new String[i];
    int j = 0;
    for (CountryLanguage count : countryLanguageSet) {
        System.out.println(count.getLanguage().getLanguageName());
        dynamicHeaders[j] = count.getLanguage().getLanguageName();
        j++;
    }
}

public void populateDynamicDataTable() {
    debugLogger.debug("populateDynamicDataTable:Enter");
    // Create <h:dataTable value="#{myBean.dynamicList}" var="dynamicItem">.
    HtmlDataTable dynamicDataTable = new HtmlDataTable();
    dynamicDataTable.setValueExpression(
            "value",
            createValueExpression(
                    "#                {relationBean.dynamicList}",
                    List.class));
    dynamicDataTable.setVar("dynamicItem");

    // Iterate over columns.
    for (int i = 0; i < dynamicHeaders.length; i++) {
        // Create <h:column>.
        HtmlColumn column = new HtmlColumn();
        dynamicDataTable.getChildren().add(column);
        // Create <h:outputText value="dynamicHeaders[i]"> for <f:facet
        // name="header"> of column.
        HtmlOutputText header = new HtmlOutputText();
        header.setValue(dynamicHeaders[i]);
        column.setHeader(header);
        HtmlInputText input = new HtmlInputText();
        column.getChildren().add(input);
    }
    dynamicDataTableGroup = new HtmlPanelGroup();
    dynamicDataTableGroup.getChildren().add(dynamicDataTable);
    debugLogger.debug("populateDynamicDataTable:Exit");
}

public HtmlPanelGroup getDynamicDataTableGroup() throws Exception {
    // This will be called once in the first RESTORE VIEW phase.
    if (dynamicDataTableGroup == null) {
        loadDynamicList(); // Preload dynamic list.
        populateDynamicDataTable(); // Populate editable datatable.
    }

    return dynamicDataTableGroup;
}

public List<List<String>> getDynamicList() {
    return dynamicList;
}

public void setDynamicList(List<List<String>> dynamicList) {
    this.dynamicList = dynamicList;
}

public void setDynamicDataTableGroup(HtmlPanelGroup dynamicDataTableGroup) {
    this.dynamicDataTableGroup = dynamicDataTableGroup;
}

public ValueExpression createValueExpression(String valueExpression,
        Class<?> valueType) {
    FacesContext facesContext = FacesContext.getCurrentInstance();
    return facesContext
            .getApplication()
            .getExpressionFactory()
            .createValueExpression(facesContext.getELContext(),
                    valueExpression, valueType);
}
4

1 回答 1

0

我得到了答案,我添加了一个 for 循环来检查。

HtmlColumn column = new HtmlColumn();
    dynamicDataTable.getChildren().add(column);
    if(i==0){
        HtmlCommandButton commandButton=new HtmlCommandButton();
        commandButton.setValue("Delete");
        column.getChildren().add(commandButton);
        commandButton.setActionExpression(createActionExpression("#{relationBean.deleteRow}", String.class));
        }
    // Create <h:outputText value="dynamicHeaders[i]"> for <f:facet name="header"> of column.
    HtmlOutputText header = new HtmlOutputText();
    header.setValue(dynamicHeaders[i]);
    column.setHeader(header);
于 2013-09-12T07:50:42.753 回答