-2

I am not too good with JSF, i have searched but couldn't find similar answer on the net please i need help. I have a set of a sample table below which can grow, it is not certain of how many rows each car has.

...................................................
| ID         | Car        | Year       | Colour   |
|.................................................|
| 37efcc3e   | Audi       | 1994       | Green    |
| 2aacdbd4   | Audi       | 2009       | Yellow   |
| 5dd57389   | BMW        | 1989       | Green    |
| 94f32d8a   | BMW        | 1978       | Orange   |
| d03b3af1   | BMW        | 2001       | Green    |
| 7ca88a0b   | Ford       | 1964       | Silver   |

which i want to display like this below using div to achieve this

Audi
1994   Green     37efcc3e
2009   Yellow    2aacdbd4

BMW    
1989   Green     5dd57389
1978   Orange    94f32d8a
2001   Green     d03b3af1

Ford
1964   Silver    7ca88a0b

How can i use JSF or Primefaces repeat not dataTable to achieve this.
With plain java

List<Product> productList;
for (Product product1 : productList) {
  for (Product product2 : productList) {
    if(product1.getCar().equals(product2.getCar())) {//check if car changes

    }
  }
}
Or can use set
Set<Product> set = new HashSet<Product>(productList);
if(set.size() < list.size()){//check if car changes

}

Primefaces have row grouping or group row with table, I am just wondering how possible how to do it with repeat

  • try doing it in plain java with two loops printing to the console. Then you have your repeat 'solution' as well – Kukeltje Apr 15 '18 at 05:45
  • @Kukeltje in a plain java how can i do it using two loops when the list is coming from one table and the car column value is unknown? Like i said am new to JSF, Please i need to solve this issue. – Christian Ibanibo Apr 16 '18 at 02:46
  • As stated before, your basic issue has nothing to do with jsf if you cannot achieve this in plain java. You can even do it in one loop. Basic programming... SO generalize you problem and search on Stackoverflow for answers to your nearly formulated question... – Kukeltje Apr 16 '18 at 06:44
  • @Kukeltje be easy on me, am not as good as u am still learning. Looping it with plain java will repeatedly show the car brand on every row before showing the records with the year, colour and id values. My case is this if a car brand (e.g Audi) has 100 records, how can i show the name of the car only on the first row then show the 100 records of the car brand with other values. – Christian Ibanibo Apr 16 '18 at 07:22
  • Yes, I fully 1000% percent understand your question and I could provide an working solution (help for **now**) but this way I'm helping you to learn thinking in a different way (help for now and the future) You can (should) try to achieve this in plain java as well. Think about conditional printing things to the console..... And then think about the condition (the 1st and each time the 'car' changes). – Kukeltje Apr 16 '18 at 07:32
  • @Kukeltje Please i have tried different condition with the car name inside nested loop yet i still get repeated car name. – Christian Ibanibo Apr 16 '18 at 15:40
  • Then read [ask] and [mcve]... But a plain java one... no jsf... – Kukeltje Apr 16 '18 at 16:09
  • @Kukeltje my bad i felt with jsf repeat varStatus would be the solution. Yet i haven't gotten the solution, would be easy if am removing duplicate from the list but am not because of the other fields of the record. I understand you are trying to help me but am not still getting the desired answer. I have tried several ways yet am still get duplicate cars – Christian Ibanibo Apr 16 '18 at 16:53
  • @Kukeltje I need your help i can't figure out what am missing, it might be a basic programming, for me kinda confuse here. Using two loops to check if the car equals to each other yet am still getting repeated car values. Please i need your help – Christian Ibanibo Apr 17 '18 at 07:44
  • There still is no [mcve] with a (simple) model and a simple java loop (or loops, depending on your (sorted?) model in java) that we can comment on. Sorry, but I'm not writing a complete solution for you... – Kukeltje Apr 17 '18 at 07:48
  • The double loop is only required if if you have e.g. a model where you have a hashset with 'car' as the key and in it a list of all cars. But you do not need to compare anything then. If you have a sorted model of all entries then you can do it in one loop and check if the current car is different then the previous one and only then print the 'car'. See it is all plain java initially, nothing JSF related. – Kukeltje Apr 17 '18 at 09:55
  • I felt it was jsf since primefaces have row grouping using simple list. Thanks i appreciate. Should i wrap everything inside a map? like Map> map1 = new HashMap<>>(); Or wrap the map's entry set in a collection which the can iterate over, such as an ArrayList. example Map> map = new TreeMap<>(); List>> entries; entries = new ArrayList<>(map.entrySet()); then i can pass that to the repeat . Is this right? – Christian Ibanibo Apr 18 '18 at 08:01

1 Answers1

0

Doing this in JSF is not really different than doing it in plain Java

Pseudocode java

Assuming a sorted list (like in your table but then sorted) so no additional rewriting of the backing model

for(int i=0; i<cars.size();i++) {
    if (i==0 || !cars[i].getBrand().equals(car[i-1].getBrand())) {
       System.out.println(cars[i].getBrand());
    }
    System.out.println(cars[i].type + "\t" +cars[i].color + "\t" + cars[i].year);
}

And in JSF (also 'pseudocode', not tested)

<table>
  <ui:repeat var="car" value="#{bean.cars}" varStatus="myVarStatus">

    <ui:fragment  rendered="#{myVarStatus.index == 0 || !bean.cars.get(myVarStatus.index).brand.equals(bean.cars.get(myVarStatus.index -1).brand}">
        <tr><td colspan="3"><h:outputText value="#{car.brand}"></td></tr>
    </ui:fragment>

    <tr>
       <td>#{car.type}</td>
       <td>#{car.color}</td>
       <td>#{car.year}</td>
    </tr>

  </ui:repeat>
</table>
Kukeltje
  • 12,223
  • 4
  • 24
  • 47
  • am using collection not array so returning an element at the specified position will be cars.get(i).getBrand(). I have tested the plain java and i get syntax error when i remove int i = 0 from your if condition i get java.lang.ArrayIndexOutOfBoundsException: -1. And i don't understand your if condition where you declare int i = 0 again and car[i-1] ? I also tested the jsf code there was no error but brand was repeated. – Christian Ibanibo Apr 18 '18 at 14:29
  • `int i=0` in the condition was a typo corrected it. Can you see why it needs to be what is? And regarding the JSF code, you might need to improve the condition, I just HOPE you get the idea. If you don't... well... – Kukeltje Apr 18 '18 at 15:04
  • Thank you i really appreciate, you made me think, it works. Can i use it in production? or hashset and hash table is preferable ? – Christian Ibanibo Apr 19 '18 at 08:16
  • You are welcome... No this works fine for production. No need to first create a different model (unless that is easier for other reasons for you). And you can and may accept the answer (and even 'upvote' it ;-)) – Kukeltje Apr 19 '18 at 08:17