使用backing bean方法添加jsf命令按钮

时间:2013-09-12 07:17:53

标签: jsf

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

1 个答案:

答案 0 :(得分: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);