可编辑表格曲面中的动态列

时间:2013-06-03 09:33:58

标签: jsf dynamic primefaces datatable

我有一个带动态列的可编辑表格。编辑行时,所有字段都会正确显示。但是,当我单击“保存”图标时,我的第一个元素的值将被写入错误的列。 基本上我有一个未定义的翻译文件数量,我想显示和编辑它们。 这些文件的内容类似于

<entry key="contractlist_fleetmanager_status_ALL_STATUSES">All statuses</entry>

和de

<entry key="contractlist_fleetmanager_status_ALL_STATUSES">Alle Zustände</entry>

示例I有3个不同的翻译文件de,en和fr。列如下:en fr和de。但是在hashmap翻译项中,先得到一些空字段,然后是fr和en。

[de=Alle Zustände, null, null, null, null, null, null, null, null, fr=All statuses, en=All statuses, null, null, null, null, null]

当我调试并查看handleRowEdit时,selectedItem值被混淆了。第一个元素现在已经具有en字段的值而不是de字段。

[de=All statuses, null, null, null, null, null, null, null, null, fr=All statuses, en=All statuses, null, null, null, null, null]

这是因为en是我的primefaces表中的第一列(列是:en fr de)吗? 我该如何解决这个问题?

这是我的bean代码

private TranslationItem selectedItem;
    private List<TranslationItem> translationItems;
    private List<ColumnModel> columns = new ArrayList<ColumnModel>();

public Translation() {
        createDynamicColumns();
    }

public String handleRowEdit(TranslationItem selectedItem) throws IOException {
        setSelectedItem(selectedItem);
        getLog().debug("rowedit");
        return "";
    }
public List<ColumnModel> getColumns() {
        return columns;
    }

public List<TranslationItem> getTranslationItems() throws IOException {
        if (translationItems == null) {
            translationItems = new ArrayList<TranslationItem>();

            Properties[] props = new Properties[localeItems().length];
            int i = 0;
            for (String locale : localeItems()) {
                FileInputStream in = new FileInputStream(SiteConfig.getInstance().getDefaultConfigPath() + File.separator
                        + "language_" + locale + ".xml");
                props[i] = new Properties();
                props[i].loadFromXML(in);
                in.close();
                i++;
            }

            Enumeration<Object> keys = props[0].keys();

            while (keys.hasMoreElements()) {
                String key = (String) keys.nextElement();

                i = 0;
                Map<String, String> value = new LinkedHashMap<String, String>() {
                    @Override
                    public String put(String key, String value) {
                        return super.put(key, value);
                    }
                };
                for (String locale : localeItems()) {
                    value.put(locale, props[i].getProperty(key));
                    i++;
                }

                translationItems.add(new TranslationItem(key, value));
            }
        }
        return translationItems;
    }
public void createDynamicColumns() {

        String columnTemplate = "";
        for (String locale : localeItems()) {
            columnTemplate += locale + " ";
        }
        String[] columnKeys = columnTemplate.split(" ");
        columns.clear();

        for (String columnKey : columnKeys) {
            columns.add(new ColumnModel(columnKey, columnKey));
        }
    }
//inner class TranslationItem 
    public class TranslationItem {
        private String key;
        private Map<String, String> value;

        public TranslationItem() {

        }

        public TranslationItem(String key, Map<String, String> value) {
            this.key = key;
            this.value = value;
        }

        public String getKey() {
            return key;
        }

        public void setKey(String key) {
            this.key = key;
        }

        public Map<String, String> getValue() {
            return value;
        }

        public void setValue(Map<String, String> value) {
            this.value = value;
        }

        public String getValueLocale(String locale) {
            return value.get(locale);
        }
    }

    //inner class ColumnModel
    static public class ColumnModel implements Serializable {

        private String header;
        private String property;

        public ColumnModel(String header, String property) {
            this.header = header;
            this.property = property;
        }

        public String getHeader() {
            return header;
        }

        public String getProperty() {
            return property;
        }
    }

这是我的jsf代码

<p:dataTable id="translationtable" styleClass="ptable100" var="translation" value="#{adminTranslation.translationItems}" width="100%" height="200" widgetVar="translation"
                                        emptyMessage="#{msg.all_lists_no_records_found}" editable="true" rowKey="#{translation.key}" rowIndexVar="rowIndex" selection="#{adminTranslation.selectedItem}" selectionMode="single"
                                        resizableColumns="true" sortMode="multiple"
                                        paginator="true" rows="20" rowsPerPageTemplate="5,10,20,50,100" paginatorTemplate="{FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink}  #{msg.all_lists_numberOfRowsDisplayed_label} {RowsPerPageDropdown}"      
                >    
                    <p:ajax event="rowEdit" listener="#{adminTranslation.onEdit}" update="translationtable" />
    <p:columns value="#{adminTranslation.columns}" headerText="#{column.header}" var="column" columnIndexVar="colIndex" 
                        sortBy="#{translation.value[column.property]}" filterBy="#{translation.value[column.property]}" filterMatchMode="contains" 
                        filterPosition="top" filterStyle="float:right;width: 200px;" styleClass="ui-editable-column">  

                        <p:column >
                            <p:cellEditor> 
                        <f:facet name="output"><h:outputText value="#{translation.value[column.property]}"/></f:facet>  
                            <f:facet name="input"><h:inputText value="#{translation.value[column.property]}" styleClass="input ilarge" /></f:facet>
                           </p:cellEditor>
                        </p:column>

                </p:columns> 
<p:column >
                    <p:commandLink id="rowEditLink" actionListener="#{adminTranslation.handleRowEdit(translation)}" ajax="true" >  
<p:rowEditor id="edit"/>
                        </p:commandLink>
                </p:column>
            </p:dataTable>

我在tomcat 7上使用primefaces 3.2

0 个答案:

没有答案