排序和DataModel - CollectionDataModel,setRowIndex()有什么用?

时间:2016-06-24 16:07:29

标签: jsf datatable jsf-2.2 datamodel

每个集合(ListHashMap等)由JSF包装在DataModel类中(或者,在子类中,作为ArrayDataModel,{{1 },CollectionDataModelListDataModelResultDataModelResultSetDataModel)。当JSF呈现/解码表数据时,它将调用表ScalarDataModel类'方法。

我正在尝试构建一个在Anghel Leonard的书中给出的示例 - 掌握Java Server Faces 2.2

bean就像这个一样简单:

DataModel

@Named @ViewScoped public class PlayersBean implements Serializable { private HashSet<Players> dataHashSet = new HashSet<>(); private SortDataModel<Players> sortDataModel; final SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy"); private String sortType = "asc"; public PlayersBean() { try { dataHashSet.add(new Players(2, "NOVAK DJOKOVIC", (byte) 26, "Belgrade, Serbia", "Monte Carlo, Monaco", (short) 188, (byte) 80, "Boris Becker, Marian Vajda", sdf.parse("22.05.1987"))); dataHashSet.add(new Players(1, "RAFAEL NADAL", (byte) 27, "Manacor, Mallorca, Spain", "Manacor, Mallorca, Spain", (short) 185, (byte) 85, "Toni Nadal", sdf.parse("03.06.1986"))); dataHashSet.add(new Players(7, "TOMAS BERDYCH", (byte) 28, "Valasske Mezirici, Czech", "Monte Carlo, Monaco", (short) 196, (byte) 91, "Tomas Krupa", sdf.parse("17.09.1985"))); dataHashSet.add(new Players(8, "STANISLAS WAWRINKA", (byte) 28, "Lausanne, Switzerland", "St. Barthelemy, Switzerland", (short) 183, (byte) 81, "Magnus Norman", sdf.parse("28.03.1985"))); dataHashSet.add(new Players(4, "ANDY MURRAY", (byte) 26, "Dunblane, Scotland", "London, England", (short) 190, (byte) 84, "Ivan Lendl", sdf.parse("15.05.1987"))); dataHashSet.add(new Players(5, "JUAN MARTIN DEL POTRO", (byte) 25, "Tandil, Argentina", "Tandil, Argentina", (short) 198, (byte) 97, "Franco Davin", sdf.parse("23.09.1988"))); dataHashSet.add(new Players(10, "JO-WILFRIED TSONGA", (byte) 28, "Le Mans, France", "Gingins, Switzerland", (short) 188, (byte) 91, "Nicolas Escude & Thierry Ascione", sdf.parse("17.04.1985"))); dataHashSet.add(new Players(6, "ROGER FEDERER", (byte) 32, "Basel, Switzerland", "Bottmingen, Switzerland", (short) 185, (byte) 85, "Stefan Edberg, Severin Luthi", sdf.parse("08.08.1981"))); dataHashSet.add(new Players(9, "RICHARD GASQUET", (byte) 27, "Beziers, France", "Neuchatel, Switzerland", (short) 185, (byte) 75, "Sergi Bruguera and Sebastien", sdf.parse("18.06.1986"))); dataHashSet.add(new Players(3, "DAVID FERRER", (byte) 31, "Javea, Spain", "Valencia, Spain", (short) 175, (byte) 73, "Jose Francisco Altur", sdf.parse("02.04.1982"))); sortDataModel = new SortDataModel<Players>(new CollectionDataModel<Players>(dataHashSet)); } catch (ParseException ex) { Logger.getLogger(PlayersBean.class.getName()).log(Level.SEVERE, null, ex); } } public SortDataModel<Players> getSortDataModel() { return sortDataModel; } public String sortDataByRanking() { sortDataModel.sortThis(new Comparator<Players>() { @Override public int compare(Players key_1, Players key_2) { if (sortType.equals("asc")) { return key_1.getRanking() - key_2.getRanking(); } else { return (-1) * (key_1.getRanking() - key_2.getRanking()); } } }); sortType = (sortType.equals("asc")) ? "dsc" : "asc"; return null; } } 为:

SortDataModel.java

经过调试后,我发现行整数数组将填充从0开始到你要收集的集合大小(本例中的行)减去1的整数。

enter image description here

接下来我看到他已覆盖public class SortDataModel<T> extends CollectionDataModel<T> { CollectionDataModel<T> model; private Integer[] rows; public SortDataModel(CollectionDataModel<T> model) { this.model = model; initRows(); } private void initRows() { int rowCount = model.getRowCount(); if (rowCount != -1) { this.rows = new Integer[rowCount]; for (int i = 0; i < rowCount; ++i) { rows[i] = i; } } } public void sortThis(final Comparator<T> comparator) { Comparator<Integer> rowc = new Comparator<Integer>() { @Override public int compare(Integer key_1, Integer key_2) { T key_1_data = getData(key_1); T key_2_data = getData(key_2); return comparator.compare(key_1_data, key_2_data); } }; Arrays.sort(rows, rowc); } private T getData(int row) { int baseRowIndex = model.getRowIndex(); model.setRowIndex(row); T newRowData = model.getRowData(); model.setRowIndex(baseRowIndex); return newRowData; } @Override public void setRowIndex(int rowIndex) { if ((0 <= rowIndex) && (rowIndex < rows.length)) { model.setRowIndex(rows[rowIndex]); } else { model.setRowIndex(rowIndex); } } @Override public boolean isRowAvailable() { return model.isRowAvailable(); } @Override public int getRowCount() { return model.getRowCount(); } @Override public T getRowData() { return model.getRowData(); } @Override public int getRowIndex() { return model.getRowIndex(); } @Override public Object getWrappedData() { return model.getWrappedData(); } @Override public void setWrappedData(Object data) { } } 方法来替换默认行索引。

发出GET请求会导致从

多次调用此重写方法

enter image description here

到0,1,2,3 .......最后再次-1到最后, enter image description here

  1. 这种方法到底有什么用?难以理解作者的观点?

  2. 正如您所看到的那样,通过添加10个项目来初始化hashset。在解雇GET请求后,我只看到5个项目。为什么呢?

  3. 获取:

    enter image description here

    点击“排名”链接对其进行排序:

    发表:

    enter image description here

    为什么只有5条记录?

    我正在使用Glassfish Application Server 4.1.1,内部使用Mojarra 2.2.12。

    查看

    setRowIndex

0 个答案:

没有答案