熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java核心技術 >> 正文

JSF動態生成固定表頭和行標的DataTable

2022-06-13   來源: Java核心技術 

  這個例子在jsf下通過JSF 動態生成DataTable 固定表頭 固定行標 點擊表頭可排序 希望可以供大家學習和參考

    自己在寫動態生成DataTable的時候也查閱了很多相關文章 以及實現固定表頭等等

     在解決固定表頭問題上我是用的兩張表(加行標是張表)實現的 因為我發現如果用JSF的化實現固定表頭幾乎不可能(如果有人有好的想法 比如用JS比較在行的朋友請告訴我解決方法 謝謝)

    實現給每個Header加上一個CommandLink的時候我遇到了很大的問題 就是無論如何我動態生成的這些CommandLink都沒有辦法觸發事件(並不報錯) 這個問題足足折磨了我

    最後終於發現問題的所在 是因為我在生成CommandLink的時候沒有給每個CommandLink  SetID 就加上這句話問題立馬解決



    我原來的代碼是鏈接數據庫的 但是這樣的化大家也沒有辦法參考 所以我對代碼少加修改 就算是個小例子吧 可重用

希望大家對我的代碼提出寶貴意見一起進步謝謝

  




Dynamicjsp




<%@ taglib uri= prefix=f%>
<%@ taglib uri= prefix=h%>
<Script language=JavaScript>
  var tdW;
  //Scroll
  function f_scroll(Col_TRow_TDivNm){
    if(Col_T!=){
      documentgetElementById(Col_T)scrollLeft = documentgetElementById(DivNm)scrollLeft;
    }
    if(Row_T!=){
      documentgetElementById(Row_T)scrollTop = documentgetElementById(DivNm)scrollTop;
    }
  }
</Script>
<html>
    <f:view>
        <head>
            <title>TABLE</title>
            <link rel=stylesheet type=text/css >
        </head>
        <body>
            <h:form>
                <font size= color=black
                    style=position:absolute; left: ; top: ; width:; height:>
                    <h:outputText value=Please Enter: /> </font>

                <h:inputText value= size=
                    style=position:absolute; left: ; top: ; width:; height: />

                <h:commandButton value=Search action=
                    style=position:absolute; left: ; top: ; width:; height: />

                <table BORDER=
                    STYLE=POSITION: ABSOLUTE; LEFT: px; TOP: px; right: px; bottom: px;>
                    <tr>
                        <td STYLE=textalign: right;>
                            <!Table start>
                            <!Table end>
                        </td>

                        <td>
                            <!Table start>
                            <Div ID=Table
                                STYLE=position: relative; top: ; borderleft: pt solid black;
                                      borderright: pt solid black; height: px; width: px; overflowx: hidden;>
                                <h:dataTable value=#{myBeanmyHeader} var=myHeader
                                    binding=#{myBeanheaderDataTable} bgcolor=white border=
                                    cellspacing= rendered=true styleClass=orders
                                    headerClass=ordersHeader rowClasses=evenColumnoddColumn
                                    style=position:absolute; left: ; top: 
                                          width: ; height: ; bordercollapse:collapse;
                                    id=ree>
                                </h:dataTable>
                            </Div>
                            <!Table end>
                        </td>
                    </tr>

                    <tr>
                        <td STYLE=verticalalign: top;>
                            <!Table start>
                            <Div ID=Table
                                STYLE=borderbottom: pt solid black; bordertop: pt solid black; 
                                   width: px; height: px; overflowy: hidden; position: relative; left: ;>
                                <h:dataTable value=#{myBeanmyNum} var=myNum
                                    binding=#{myBeannumDataTable} bgcolor=white border=
                                    cellspacing= rendered=true styleClass=orders
                                    headerClass=ordersHeader rowClasses=evenColumnoddColumn
                                    style=position:absolute; left: ; top: ; width: ; height: ; bordercollapse:collapse;
                                    id=dee>
                                </h:dataTable>
                            </Div>
                            <!Table end>
                        </td>

                        <td STYLE=verticalalign: top;>
                            <!Table start>
                            <Div ID=Table onScroll=f_scroll(TableTableTable);
                                STYLE=height: px; width: px; overflowy: scroll; overflowx: scroll;>
                                <h:dataTable value=#{myBeanmyList} var=myItem
                                    binding=#{myBeandynamicDataTable} bgcolor=white border=
                                    cellspacing= rendered=true styleClass=orders
                                    headerClass=ordersHeader rowClasses=evenColumnoddColumn
                                  style=position:absolute; left: ; top: ; width: ; height: ; bordercollapse:collapse;>
                                </h:dataTable>
                            </Div>
                            <!Table end>
                        </td>
                    </tr>
                </table>
            </h:form>
        </body>
    </f:view>
</html>

 
MyBeanjava


package mypackage;

import javautilArrayList;
import javautilList;

import javaxfacesapplicationApplication;
import ponentUIColumn;
import ponentUIOutput;
import lHtmlCommandLink;
import lHtmlDataTable;
import lHtmlOutputText;
import ntextFacesContext;
import javaxfaceselMethodBinding;
import javaxfaceselValueBinding;
import javaxfaceseventActionEvent;

public class MyBean {
    
    private List<List<String>> myList;
    
    private List<List<String>> myHeader;
    
    private List<List<Integer>> myNum;
    
    private HtmlDataTable dynamicDataTable;
    
    private HtmlDataTable headerDataTable;
    
    private HtmlDataTable numDataTable;
    
    private static int tempWidth = ;

    // Load Data from DataBase to Every DataTable 
    public void loadMyList() throws Exception {

        myList = new ArrayList<List<String>>();
        
        int row = ;
        
        while(row > ) {  
            
            int column = ;
            
            String str = o;
            
            List <String> tmpvector = new ArrayList <String> ();
            
            while(column > ) {
                
                tmpvectoradd(str);
                
                column;
            }
            row;
            
            myListadd(tmpvector);
        }
    }
    
    public void loadMyHeader() throws Exception {

        myHeader = new ArrayList<List<String>>();

        String str = *****;
        
        int column = ;
        
        List <String> tmpvector = new ArrayList <String> ();
        
        while(column > ) {

            tmpvectoradd(str);
            
            column;
        }
        myHeaderadd(tmpvector);
    }
    
    public void loadMyNum() throws Exception {

        myNum = new ArrayList<List<Integer>>();
        
        int row = ;
        
        while(row < ) {  
                
            List <Integer> tmpvector = new ArrayList <Integer> ();

            tmpvectoradd(row);
            
            myNumadd(tmpvector);
            
            row++;
        }
    }
    
    //initialize Every DataTable to the Front
    public void populateDynamicDataTable() {

        if (myList != null && myListsize() > ) {
            
            dynamicDataTable = new HtmlDataTable();
            
            int columns = ((List) myListget())size();

            Systemoutprintln(DisplayCloumn:    + columns);//
            
            for (int i = ; i < columns; i++) {             

                ValueBinding myItem = 
            FacesContextgetCurrentInstance()getApplication()createValueBinding(#{myItem[ + i + ]});              
                
                UIColumn column = new UIColumn();
                
                HtmlOutputText outputText = new HtmlOutputText();
                
                outputTextsetValueBinding(value myItem);

                outputTextsetStyle(width:  + tempWidth + );
                
                columngetChildren()add(outputText);
                
              columnsetId(col_ + i);  //You should set ID of every component which you create in the BackingBean 
                
                dynamicDataTablegetChildren()add(column);
            }
        }
    }
    
    public void populateHeaderDataTable() {
        
        if (myHeader != null && myHeadersize() > ) {
            
            headerDataTable = new HtmlDataTable();
            
            int columns = ((List) myHeaderget())size();    
            
            FacesContext facesContext = FacesContextgetCurrentInstance();
            
            Application application = facesContextgetApplication();
            
            for (int i = ; i < columns; i++) {
            
                HtmlCommandLink commandLink =
                 (HtmlCommandLink) applicationcreateComponent(HtmlCommandLinkCOMPONENT_TYPE);
                
                String valueBindingExpression = #{myHeader[ + i + ]};                    
                ValueBinding valueBinding = applicationcreateValueBinding(valueBindingExpression);                 
                commandLinksetValueBinding(value valueBinding);        
                
   commandLinksetId(head_comankLink_ + i);//The most important!!! If you dont put it nothing is gonna happen
 
                
                commandLinksetStyle(color: blue);
                
                commandLinksetStyle(width:  + tempWidth + );        
                
                commandLinkgetAttributes();

                Class[] params = { ActionEventclass };                
                MethodBinding actionListener = 
                  applicationcreateMethodBinding(#{myBeansetSelectedDocumentId} params);                
                commandLinksetActionListener(actionListener);

                commandLinksetImmediate(true); 

                UIColumn column = new UIColumn();                
                columngetChildren()add(commandLink);
                columnsetId(head_ + i);

                headerDataTablegetChildren()add(column);       
            }
        }
    }
    
    public void populateNumDataTable() {
        
        if (myNum != null && myNumsize() > ) {
            
            numDataTable = new HtmlDataTable();

            UIOutput output = new UIOutput();
            
            ValueBinding myItem =
              FacesContextgetCurrentInstance()getApplication()createValueBinding(#{myNum[]});
            
            outputsetValueBinding(value myItem);
            
            // Set column
            UIColumn column = new UIColumn();
            columngetChildren()add(output);
            columnsetId(num);
            
            numDataTablegetChildren()add(column);
        }
    }
   
    //Test Actions and ActionListeners

    public void setSelectedDocumentId(ActionEvent event) throws Exception 
    {

    }

    // Getters 
    public HtmlDataTable getDynamicDataTable() throws Exception {
        if (dynamicDataTable == null) {
            
            loadMyList(); // Reload to get most recent data
            
            populateDynamicDataTable();
        }
        return dynamicDataTable;
    }
    
    public HtmlDataTable getHeaderDataTable() throws Exception {
        if (headerDataTable == null) {
            
            loadMyHeader(); // Reload to get most recent data
            
            populateHeaderDataTable();
        }
        return headerDataTable;
    }

    public HtmlDataTable getNumDataTable() throws Exception {
        if (numDataTable == null) {
            
            loadMyNum(); // Reload to get most recent data
            
            populateNumDataTable();
        }
        return numDataTable;
    }
    
    public List<List<Integer>> getMyNum() {
        return myNum;
    }
    
    public List<List<String>> getMyHeader() {
        return myHeader;
    }
    
    public List<List<String>> getMyList() {
        return myList;
    }

    // Setters 
    public void setDynamicDataTable(HtmlDataTable dynamicDataTable) {
        thisdynamicDataTable = dynamicDataTable;
    }

    public void setHeaderDataTable(HtmlDataTable headerDataTable) {
        thisheaderDataTable = headerDataTable;
    }

    public void setMyHeader(List<List<String>> myHeader) {
        thismyHeader = myHeader;
    }
    
    public void setNumDataTable(HtmlDataTable numDataTable) {
        thisnumDataTable = numDataTable;
    }
    
    public void setMyNum(List<List<Integer>> myNum) {
        thismyNum = myNum;
    }

    public void setMyList(List<List<String>> myList) {
        thismyList = myList;
    }
}

 
facesconfigxml




<?xml version= encoding=UTF?>
<!DOCTYPE facesconfig PUBLIC //Sun Microsystems Inc//DTD JavaServer Faces Config //EN facesconfig__dtd>

<facesconfig>
    <managedbean>
        <managedbeanname>myBean</managedbeanname>
        <managedbeanclass>mypackageMyBean</managedbeanclass>
        <managedbeanscope>session</managedbeanscope>
    </managedbean>
</facesconfig>

stylescss


orders {
    border: thin solid black;
}

ordersHeader {
    textalign: center;
    fontstyle: ;
    color: Snow;
    fontsize: px;
    background: Teal;
}

evenColumn {
    textalign: center;
    fontsize: px;
    background: #FFFFF;
}

oddColumn {
    textalign: center;
    fontsize: px;
    background: white;

}


From:http://tw.wingwit.com/Article/program/Java/hx/201311/26840.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.