JSF PrimeFaces DataTable在第一次尝试时不会更新

时间:2019-12-04 08:46:10

标签: jsf primefaces

我已经开发了一个动态表,该表应在每次​​请求时重新填充表,相同的功能由以下代码执行:-

html

        <h:form id="form">
            <p:outputLabel value="Client: " />
            <p:inputText id="inp" value="#{test.id}" />
            <p:inputText id="inp1" value="#{test.title}" />
            <p:commandButton value="Call List"  action = "#{liveRangeService.LiveRangeServicesss(10)}"/>
            <p:commandButton value="Call List"  action = "#{liveRangeService.LiveRangeServicesss(20)}"/>


                <p:dataTable var="result" id="tbl" widgetVar="dtlTBL"
                                    value="#{liveRangeService.tableData}" 
                                    filteredValue="#{liveRangeService.filteredData}"
                                    paginator="false"
                                    scrollable="true"  rowIndexVar="rowindex"  scrollHeight="500" 
                                    scrollRows="50" liveScroll="true"
                                    filterDelay="1100"
                    >
                    <p:ajax event="rowSelect" listener="#{test.onRowSelect(rowindex)}"  />
                    <f:facet name="header">
                        <p:outputPanel layout="inline" styleClass="tabSpacer">
                            <h:outputText value="Global Filter:" />
                            <p:inputText id="globalFilter" onkeyup="PF('dtlTBL').filter()" style="width:150px;margin-left:10px;"/>
                        </p:outputPanel>
                    </f:facet>

                    <p:column width="50">
                        <f:facet name="header">
                            <h:outputText value="Sr." />
                        </f:facet>
                        <p:commandButton value="#{rowindex}" style="width: 49px" action="#{test.onRowSelect(rowindex)}"/>
                    </p:column>

                    <p:columns value="#{liveRangeService.tableHeaderNames}"
                               var="mycolHeader" 
                               width="#{colIndex==0?'10%':colIndex==1?'70%':colIndex==2?'10%':colIndex==3?'10%':'0'}" 
                               columnIndexVar="colIndex" 
                               sortBy="#{result[mycolHeader]}"
                               filterBy="#{result[mycolHeader]}"
                               filterMatchMode="contains"                        
                               >
                        <f:facet name="header">
                            <h:outputText value="#{mycolHeader}" />
                        </f:facet>
                        <h:outputText value="#{result[mycolHeader]}" />
                        <br />
                    </p:columns>
                </p:dataTable>
        </h:form>
@ManagedBean(name="liveRangeService", eager = true)
@Dependent

public class LiveRangeService implements Serializable {
    private static List< Map<String, String> > tableData;
    public static List< Map<String, String> > filteredData;
    private static List<String> tableHeaderNames;
    private String tableColWidths;

    public List<Map<String, String>> getTableData() {
        return tableData;
    }
    public List<String> getTableHeaderNames() {
        return tableHeaderNames;
    }

    public LiveRangeService() {

    }

    public static void LiveRangeServicesss(int noOfRows) {
        tableData = new ArrayList< Map<String, String> >();
        filteredData = new ArrayList< Map<String, String> >();
        tableHeaderNames = new ArrayList<String>();

        tableHeaderNames.add("ID");
        tableHeaderNames.add("Title");
        tableHeaderNames.add("Opn_Amt");
        tableHeaderNames.add("Smr_Amt");

        for (int i = 0; i < noOfRows; i++) {
            Map<String, String> playlist = new HashMap<String, String>();
            playlist.put("ID", "101000" + i);
            playlist.put("Title", "Share Capital - Mr. " + i);
            playlist.put("Opn_Amt", "0");
            playlist.put("Smr_Amt", "0");
            tableData.add(playlist);
        }
        filteredData=tableData;
        System.out.println("Filled " + filteredData.size() + ", " + noOfRows);
        String dlgTBL="form:tbl";
        PrimeFaces.current().ajax().update(dlgTBL);
    }

    public String getTableColWidths() {
        return tableColWidths;
    }

    public void setTableColWidths(String tableColWidths) {
        this.tableColWidths = tableColWidths;
    }

    public List<Map<String, String>> getFilteredData() {
        return filteredData;
    }

    public void setFilteredData(List<Map<String, String>> filteredData) {
        this.filteredData = filteredData;
    }
}
@ManagedBean(name="test")
@SessionScoped

public class Test implements Serializable {
    public String id;
    public String title;

    public Test() {
        id="1";
        title="Testing";
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public void onRowSelect(int row) {
        try {
            String i="ID";
            String t="Title";
            String ci="id";
            String ct="title";
            this.getClass().getDeclaredField(ci).set(this, LiveRangeService.filteredData.get(row).get(i));
            this.getClass().getDeclaredField(ct).set(this, LiveRangeService.filteredData.get(row).get(t));
            PrimeFaces.current().ajax().update("form:inp");
            PrimeFaces.current().ajax().update("form:inp1");
            PrimeFaces.current().executeScript("PF('dlg').hide();");            
        } catch (Exception e) {
            System.out.println("Error! " + e.getMessage() );
        }
    }
}

但是问题是当需要为其他查询更新表时,必须两次调用函数。我放置了两个按钮,其中一个按下按钮1,该表填充了10行,而在按下按钮2上,该表填充了20行。每个按钮需要按两次以更新表格。

请咨询。

1 个答案:

答案 0 :(得分:0)

我刚刚在Constructor中初始化了表,并且所有表均按要求工作。

targets:
  dev: local/ck4t9gry400040196szky9mr4
  default: dev