You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tapestry.apache.org by esper <ma...@yahoo.com> on 2013/03/19 14:05:27 UTC

HashMap as a Grid source - once again

Hi all,

There are a lot of ways to present a hashmap in a view page but I'm
interested in my own GridDateSource implementation. There has been some
discussion regarding that on this forum but I could never find an example.

It's easy to just pass the hashmap values back to tapestry grid but how does
tapestry figure out table headers, sorting, paging even since the hash map
is not ordered?

This is just an example of GridDataSource implementation for a CustomSource:
public class CustomSource implements GridDataSource {
	
	private HashMap<String, String> hashMap;
	
	public CustomSource(HashMap<String, String> myHashMap) {
		this.hashMap = myHashMap;
	}
	
	public int getAvailableRows() {
		return hashMap.size();
	}

	public void prepare(int startIndex, int endIndex, List<SortConstraint>
sortConstraints) {
		
	}

	public Object getRowValue(int index) {
		
	}

	public Class<> getRowType() {
		
	}
}


Could someone please enlighten me with the details?

Cheers!



--
View this message in context: http://tapestry.1045711.n5.nabble.com/HashMap-as-a-Grid-source-once-again-tp5720533.html
Sent from the Tapestry - User mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: HashMap as a Grid source - once again

Posted by Thiago H de Paula Figueiredo <th...@gmail.com>.
On Tue, 19 Mar 2013 11:45:57 -0300, Lance Java <la...@googlemail.com>  
wrote:

> You don't need to implement a GridDataSource, tapestry will happily  
> coerce a
> collection to a GridDataSource.
>
> I think the question you should be asking yourself is why do you have a  
> list
> of HashMaps in the first place? A list of beans is a much clearer API.

And way more organized, structured data . . .

-- 
Thiago H. de Paula Figueiredo

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: HashMap as a Grid source - once again

Posted by Lance Java <la...@googlemail.com>.
You don't need to implement a GridDataSource, tapestry will happily coerce a
collection to a GridDataSource.

I think the question you should be asking yourself is why do you have a list
of HashMaps in the first place? A list of beans is a much clearer API.




--
View this message in context: http://tapestry.1045711.n5.nabble.com/HashMap-as-a-Grid-source-once-again-tp5720533p5720545.html
Sent from the Tapestry - User mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: HashMap as a Grid source - once again

Posted by esper <ma...@yahoo.com>.
Please tell me you're kidding... To populate my grid with the list of
hashsets I need to:
- implement my own grid data source
- implement my own bean model

but why isn't there an example on how to do this? I would imagine this being
a normal day-to-day case in web development?!



--
View this message in context: http://tapestry.1045711.n5.nabble.com/HashMap-as-a-Grid-source-once-again-tp5720533p5720543.html
Sent from the Tapestry - User mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: HashMap as a Grid source - once again

Posted by esper <ma...@yahoo.com>.
I think it is just awesome that you, not only replied to my question but took
extra effort to implement it on your page. So I learned three things today.
How to setup my grid with the HashMap, that it's not a problem to integrate
tapestry with twitter bootstrap and that you are "the shit"! Thanks again,
I'm gonna give it a try.



--
View this message in context: http://tapestry.1045711.n5.nabble.com/HashMap-as-a-Grid-source-once-again-tp5720533p5720574.html
Sent from the Tapestry - User mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: HashMap as a Grid source - once again

Posted by Lance Java <la...@googlemail.com>.
> Actually, it would partially work: it would returns a working BeanModel
implementation so that you can add properties (PropertyModel instances) to
it. 

Yeah, I figured that out in my demo. I used
beanModelSource.createDisplayModel(Object.class, messages) and a
MapPropertyConduit



--
View this message in context: http://tapestry.1045711.n5.nabble.com/HashMap-as-a-Grid-source-once-again-tp5720533p5720551.html
Sent from the Tapestry - User mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: HashMap as a Grid source - once again

Posted by Thiago H de Paula Figueiredo <th...@gmail.com>.
On Tue, 19 Mar 2013 11:17:22 -0300, Lance Java <la...@googlemail.com>  
wrote:

>> So without the custom model, you can't use a HashMap with the grid
> Correct!
>
>> But then a logical question is how to set up this model without an  
>> actual
>> bean?
> BeanModel is an interface, so implement it ;)

There's no need for that.

>> model = beanModelSource.createDisplayModel(Set.class, null);
>> Would this work?
> No, createDisplayModel assumes that the class is a bean type. It uses the
> getters to create a BeanModel.

Actually, it would partially work: it would returns a working BeanModel  
implementation so that you can add properties (PropertyModel instances) to  
it.

-- 
Thiago H. de Paula Figueiredo

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: HashMap as a Grid source - once again

Posted by Lance Java <la...@googlemail.com>.
> So without the custom model, you can't use a HashMap with the grid
Correct!

> But then a logical question is how to set up this model without an actual
> bean? 
BeanModel is an interface, so implement it ;)

> model = beanModelSource.createDisplayModel(Set.class, null); 
> Would this work?
No, createDisplayModel assumes that the class is a bean type. It uses the
getters to create a BeanModel.



--
View this message in context: http://tapestry.1045711.n5.nabble.com/HashMap-as-a-Grid-source-once-again-tp5720533p5720542.html
Sent from the Tapestry - User mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: HashMap as a Grid source - once again

Posted by esper <ma...@yahoo.com>.
Fair enough. So without the custom model, you can't use a HashMap with the
grid.

But then a logical question is how to set up this model without an actual
bean?
model = beanModelSource.createDisplayModel(Set.class, null);
Would this work?



--
View this message in context: http://tapestry.1045711.n5.nabble.com/HashMap-as-a-Grid-source-once-again-tp5720533p5720541.html
Sent from the Tapestry - User mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: HashMap as a Grid source - once again

Posted by Lance Java <la...@googlemail.com>.
No, that won't even compile ;)

If you want to avoid all the fuss of a custom BeanModel, you will need to
use a List<SomeBean> instead of a List<Map&lt;String, String>>.



--
View this message in context: http://tapestry.1045711.n5.nabble.com/HashMap-as-a-Grid-source-once-again-tp5720533p5720539.html
Sent from the Tapestry - User mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: HashMap as a Grid source - once again

Posted by esper <ma...@yahoo.com>.
Ok. so let's say that I don't provide a model. Will this make my grid happy?

public Object getRowValue(int index) { 
  // This will provide my grid with the values
  hashMap.get(index).values();
} 

public Class<> getRowType() { 
  // This will act as a model and will feed table header
  hashMap.get(cashedIndex).keySet();
} 



--
View this message in context: http://tapestry.1045711.n5.nabble.com/HashMap-as-a-Grid-source-once-again-tp5720533p5720537.html
Sent from the Tapestry - User mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: HashMap as a Grid source - once again

Posted by Lance Java <la...@googlemail.com>.
The grid is ultimately driven by it's source (GridDataSource) and it's model
(BeanModel). If a model is not provided, the grid will auto-generate one
from GridDataSource.getRowType().

You will most likely create a custom BeanModel. Or, you could wrap your maps
in a bean with getters and setters.



--
View this message in context: http://tapestry.1045711.n5.nabble.com/HashMap-as-a-Grid-source-once-again-tp5720533p5720536.html
Sent from the Tapestry - User mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: HashMap as a Grid source - once again

Posted by Barry Books <tr...@gmail.com>.
I use a collection of maps all the time for both form input and grid
output. I have a report system that stores sql in the database and
uses a beaneditform for query parameters and a grid to display the
result set. It's turned out to be extremely useful. I use a
PropertyConduit just like you suggested but with one addition. Instead
of using Object.class I created MapData.class All it does is provide
get, put and getMap methods, but this allows Tapestry to auto
construct the object. For a grid this does not matter but it's very
useful when using it with beaneditform.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: HashMap as a Grid source - once again

Posted by Lance Java <la...@googlemail.com>.
Turns out you don't need to implement a BeanModel or a GridDataSource. I've
added a demo to tapestry-stitch which does exactly what you want. The demo
can be seen here:

http://tapestry-stitch.uklance.cloudbees.net/mapgriddemo

Now, the question remains... do you really want a collection of Maps?



--
View this message in context: http://tapestry.1045711.n5.nabble.com/HashMap-as-a-Grid-source-once-again-tp5720533p5720547.html
Sent from the Tapestry - User mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: HashMap as a Grid source - once again

Posted by esper <ma...@yahoo.com>.
You're right - sorry for that. It's List<Map&lt;String, String>>



--
View this message in context: http://tapestry.1045711.n5.nabble.com/HashMap-as-a-Grid-source-once-again-tp5720533p5720535.html
Sent from the Tapestry - User mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: HashMap as a Grid source - once again

Posted by Lance Java <la...@googlemail.com>.
Are you sure you want to generate a grid from a Map<String, String> and not a
List<Map&lt;String, String>>?







--
View this message in context: http://tapestry.1045711.n5.nabble.com/HashMap-as-a-Grid-source-once-again-tp5720533p5720534.html
Sent from the Tapestry - User mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org