W3Cschool
恭喜您成為首批注冊(cè)用戶
獲得88經(jīng)驗(yàn)值獎(jiǎng)勵(lì)
以下代碼顯示如何對(duì)DataTable進(jìn)行排序。
以下代碼來(lái)自table-style.css。
.book-table-header{ bbook-bottom:1px solid #BBB; padding:16px; } .book-table-odd-row{ bbook-top:1px solid #BBB; } .book-table-even-row{ bbook-top:1px solid #BBB; }
以下代碼來(lái)自demo.xhtml。
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://java.sun.com/jsf/html" xmlns:f="http://java.sun.com/jsf/core" xmlns:c="http://java.sun.com/jsp/jstl/core" > <h:head> <h:outputStylesheet library="css" name="table-style.css" /> </h:head> <h:body> <h1>JSF 2 dataTable sorting example</h1> <h:form> <h:dataTable value="#{book.bookList}" var="o" styleClass="book-table" headerClass="book-table-header" rowClasses="book-table-odd-row,book-table-even-row" > <h:column> <f:facet name="header"> <h:commandLink action="#{book.sortByBookNo}"> Book No </h:commandLink> </f:facet> #{o.bookNo} </h:column> <h:column> <f:facet name="header"> Product Name </f:facet> #{o.productName} </h:column> <h:column> <f:facet name="header">Price</f:facet> #{o.price} </h:column> <h:column> <f:facet name="header">Quantity</f:facet> #{o.qty} </h:column> </h:dataTable> </h:form> </h:body> </html>
下面的代碼來(lái)自UserBean.java。
package cn.w3cschool.common; import java.io.Serializable; import java.math.BigDecimal; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Comparator; import java.util.List; import javax.faces.bean.ManagedBean; import javax.faces.bean.SessionScoped; @ManagedBean(name="book") @SessionScoped public class UserBean implements Serializable{ private static final long serialVersionUID = 1L; private List<Book> bookArrayList; private boolean sortAscending = true; private static final Book[] bookList = { new Book("1234", "Java", new BigDecimal("2303.0"), 3), new Book("3211", "Web", new BigDecimal("4232.00"), 6), new Book("2344", "SQL", new BigDecimal("5230.00"), 10), new Book("5643", "CSS", new BigDecimal("11320.00"), 9), new Book("4565", "HTML",new BigDecimal("232.00"), 20) }; public UserBean(){ bookArrayList = new ArrayList<Book>(Arrays.asList(bookList)); } public List<Book> getBookList() { return bookArrayList; } //sort by book no public String sortByBookNo() { if(sortAscending){ //ascending book Collections.sort(bookArrayList, new Comparator<Book>() { @Override public int compare(Book o1, Book o2) { return o1.getBookNo().compareTo(o2.getBookNo()); } }); sortAscending = false; }else{ //descending book Collections.sort(bookArrayList, new Comparator<Book>() { @Override public int compare(Book o1, Book o2) { return o2.getBookNo().compareTo(o1.getBookNo()); } }); sortAscending = true; } return null; } public static class Book{ String bookNo; String productName; BigDecimal price; int qty; public Book(String bookNo, String productName, BigDecimal price, int qty) { this.bookNo = bookNo; this.productName = productName; this.price = price; this.qty = qty; } public String getBookNo() { return bookNo; } public void setBookNo(String bookNo) { this.bookNo = bookNo; } public String getProductName() { return productName; } public void setProductName(String productName) { this.productName = productName; } public BigDecimal getPrice() { return price; } public void setPrice(BigDecimal price) { this.price = price; } public int getQty() { return qty; } public void setQty(int qty) { this.qty = qty; } } }下載 DataTable_Sort.zip
將生成的WAR文件從目標(biāo)文件夾復(fù)制到Tomcat部署文件夾,并運(yùn)行Tomcat-Install-folder/bin/startup.bat。
Tomcat完成啟動(dòng)后,在瀏覽器地址欄中鍵入以下URL。
http://localhost:8080/simple-webapp/demo.xhtml
Copyright©2021 w3cschool編程獅|閩ICP備15016281號(hào)-3|閩公網(wǎng)安備35020302033924號(hào)
違法和不良信息舉報(bào)電話:173-0602-2364|舉報(bào)郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號(hào)
聯(lián)系方式:
更多建議: