-1

I have developed a dynamic table which should refill table at every request, the same function is being performed by the code as follows:-

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

but the problem is that when the table is needed to be updated for other query, the call of function has to be done 2 times ie. i have placed two buttons, one pressing button 1, the table is populated with 10 rows and on pressing button 2, the table is populated with 20 rows. Each button is needed to be pressed two times to update the table.

Please advice.

Kukeltje
  • 12,223
  • 4
  • 24
  • 47
  • check stackoverflow by searching for generic (non-datatable related) ['jsf update on second click'](https://www.google.com/search?client=firefox-b-d&q=jsf+update+on+second+click+site%3Astackoverflow.com) You'll find lots of info. Btw it is always good to try to generalize problems...!!! – Kukeltje Dec 04 '19 at 09:29

1 Answers1

0

i have just initialized the tables in Constructor, and all worked same as required.

@ManagedBean(name="liveRangeService")
@SessionScoped

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

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

    public LiveRangeService() {
        tableData = new ArrayList< Map<String, String> >();
        filteredData = new ArrayList< Map<String, String> >();
        tableHeaderNames = new ArrayList<String>();
        LiveRangeServicesss (-1, false);
        System.out.println("in Constructor:");
    }

    public LiveRangeService(int noOfRows, boolean showDialogue) {

    }

    public void LiveRangeServicesss(int noOfRows, boolean showDialogue) {
        tableData.clear();
        tableHeaderNames.clear();
        filteredData.clear();
        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" + noOfRows + i);
            playlist.put("Title", "Share Capital - Mr. " + noOfRows + " - " + i);
            playlist.put("Opn_Amt", "0");
            playlist.put("Smr_Amt", "0");
            tableData.add(playlist);
        }
        filteredData=tableData;
       System.out.println("table size: " + tableData.size() + ", " + filteredData.size());
    }
  • Please **don't** add the code that is the solution in the **QUESTION**, please add it in the answer. I reverted the edit to the question – Kukeltje Jan 05 '20 at 21:14
  • and is effectively https://stackoverflow.com/questions/5765853/how-and-when-should-i-load-the-model-from-database-for-hdatatable your 'answer'? – Kukeltje Jan 05 '20 at 21:16
  • @Kukeltje i have uploaded the solution i reached upon but as far as the link u mentioned, i have no technical know how about the solution, it was hit and trial. – Nasir Abbas Jan 05 '20 at 21:56