In the example here : http://wiki.apache.org/myfaces-data/attachments/WorkingWithLargeTables/attachments/myfaces-cars.zip

there is a method wich fill and create a datamodel:

 protected PageableData createPageableData() {
   List<SimpleCar> cars = new ArrayList<SimpleCar>();

   for(int i = 0; i < getRows(); ++i) {
     int index = 1 + i + (getPageIndex() * getRows()) - getRows();

     if (!isSortAscending()) {
       index = CARS_DATASET_SIZE - index;
     }

     cars.add(
         new SimpleCar(
             index,
             "Car Type " + index,
             (index % 2 == 0) ? "Blue" : "Green"));
   }

   sort(cars, getSortColumn(), isSortAscending());

   return new PageableData(CARS_DATASET_SIZE, new ListDataModel(cars));
 }

is there in this method I could access my database like this way ? :
myList = currencyService.getCurrencies(index, MAX_ELEMENT);

Regards

Cosma Colanicchia wrote:
What do you mean with "load only 5 rows by time"? If it stands for
"load ony 5 rows by time *from the database*", then extending
DataModel is the right way to go.

If you simply want to display 5 rows for page, and you don't mind
loading the whole table just to see the first page, then you don't
need this at all.

When writing your DataModel, it doesn't matter at all how many rows
are currently displayed, DataModel is always asked for ONE row at a
time. You don't know how will be displayed, or even what type of
component is asking for that data.

Having cleared this, the "a single row at a time" doesn't mean that
you have to issue an SQL query for each request. Your DataModel, when
asked for the row X, can for example loads the set of rows from X to
X+4 (5 rows) and save it in a member variable. On the next row
request, it will be issuing another query only if the requested row
isn't in the cached set. Again, note that the chosen size of  5 is
completely unrelated to the "page size" that the dataTable is using,
that could freely be 3 or 140.

To obtain this logic you need to implement your DataModel subclass,
defining all the abstract methods:

- setWrappedData(java.lang.Object data)
- getWrappedData()
- getRowCount()
- setRowIndex(int rowIndex)
- getRowIndex()
- isRowAvailable()
- getRowData()

See the javadoc on the sun javaserver faces website for more info
about the class:
http://java.sun.com/javaee/javaserverfaces/1.1_01/docs/api/javax/faces/model/DataModel.html


Cosma


2006/7/4, Alexandre Jaquet <[EMAIL PROTECTED]>:
Well I'm been working with a datatable and a datascroller,
what I want is to load only 5 rows by time.



Cosma Colanicchia wrote:
> Alexandre, can you specify some more details about what you're trying
> to do?
>
>
> Cosma
>
> 2006/7/4, Alexandre Jaquet <[EMAIL PROTECTED]>:
>> I only need to work with :
>>
>> datatable.setRowIndex (5);
>> datatable.getRowData();
>>
>> when I want to extend the component ?
>>
>> Thanks for your time passed ro responding :)
>>
>> Cagatay Civici wrote:
>> > Hi,
>> >
>> > As Cosma explained before, all the control is done using a datamodel, >> > even if you bind a list object as the value of a uidata, it is wrapped
>> > as a ListDataModel object. For example when you call
>> >
>> > datatable.setRowIndex (5);
>> > datatable.getRowData();
>> >
>> > At UIData, it leads to
>> >
>> > getDataModel().setRowIndex(5);
>> > getDataModel().getRowData();
>> >
>> > At DataModel it finally refers to;
>> >
>> > setRowIndex(5);  // sets row index
>> > getRowData();   //finally returns list.get(rowIndex);
>> >
>> > Cagatay
>> >
>> >
>> >
>> > On 7/4/06, *Alexandre Jaquet* <[EMAIL PROTECTED]
>> > <mailto:[EMAIL PROTECTED]>> wrote:
>> >
>> >     Well Cosma first thanks for your explanation, if I understand
>> >     well, all
>> >     components have to extend UIData and we can set to every
>> components a
>> >     dataModel?
>> >
>> >
>> >     Cosma Colanicchia wrote:
>> >     > Hi Alexandre,
>> >     >
>> >     > it's simple, JSF defines a DataModel class that is used by
>> UIData
>> > > componentes (like the data table). Note that it is allowed the
>> >     usage
>> >     > of some other types (for example java.util.List) because
>> DataModel
>> >     > implementations that wraps it are available automatically
>> picked.
>> >     >
>> > > Basically, the datatable will initially ask your DataModel size
>> >     using
>> >     > getDatasetSize(), then ask rows as need one by one calling:
>> >     >
>> > > - isRowAvailable(index) to see if a particular index maps to an
>> >     actual
>> >     > row
>> >     > - setRowIndex(index) to make that index current
>> >     > - getRowData() to get the object with the data of the current
>> >     row index
>> >     >
>> > > So, if you need more control, you can implement your DataModel
>> >     object
>> > > (extending the JSF base class) and specify it as the datatable's
>> >     value
>> >     > attribute.
>> >     >
>> >     > This way you can, for example, implement the getDatasetSize()
>> >     doing a
>> >     > SELECT COUNT on the database and query the db only for a
>> limited set
>> > > of rows in getRowData(), instead of loading an entire table in
>> >     memory
>> >     > when creating the view.
>> >     >
>> >     >
>> >     > Hope this helps
>> >     > Cosma
>> >     >
>> >     > 2006/7/4, Alexandre Jaquet <[EMAIL PROTECTED]
>> >     <mailto:[EMAIL PROTECTED]>>:
>> >     >> Hi,
>> >     >>
>> > >> I want to understand how work DataTable any good documentation
>> >     ? I want
>> >     >> to understand the cars-demo example :
>> >     >> http://wiki.apache.org/myfaces/WorkingWithLargeTables
>> >     <http://wiki.apache.org/myfaces/WorkingWithLargeTables>
>> >     >>
>> >     >> regards
>> >     >>
>> >     >
>> >
>> >
>>
>>
>




Reply via email to