]> source.dussan.org Git - vaadin-framework.git/commitdiff
scrolling table: ordering implemented
authorMatti Tahvonen <matti.tahvonen@itmill.com>
Thu, 28 Jun 2007 10:55:16 +0000 (10:55 +0000)
committerMatti Tahvonen <matti.tahvonen@itmill.com>
Thu, 28 Jun 2007 10:55:16 +0000 (10:55 +0000)
svn changeset:1797/svn branch:trunk

src/com/itmill/toolkit/terminal/gwt/client/ui/scrolltable/IScrollTable.java
src/com/itmill/toolkit/terminal/gwt/client/ui/scrolltable/IScrollTableBody.java
src/com/itmill/toolkit/terminal/gwt/public/component-themes/common/css/common.css
src/com/itmill/toolkit/ui/Table.java

index 4bb7b08193283678e4c54f929359eaecfd1a3fb4..cc9a9296141fa2bc17bf6ab6a183032c04a1859a 100644 (file)
@@ -10,6 +10,7 @@ import com.google.gwt.user.client.DeferredCommand;
 import com.google.gwt.user.client.Element;
 import com.google.gwt.user.client.Event;
 import com.google.gwt.user.client.Timer;
+import com.google.gwt.user.client.ui.ClickListener;
 import com.google.gwt.user.client.ui.Composite;
 import com.google.gwt.user.client.ui.FlowPanel;
 import com.google.gwt.user.client.ui.Grid;
@@ -64,7 +65,9 @@ public class IScrollTable extends Composite implements Paintable, ITable, Scroll
        private IScrollTableBody tBody;
        private int width = -1;
        private int height = -1;
-       private int firstvisible;
+       private int firstvisible = 0;
+       private boolean sortAscending;
+       private String sortColumn;
        
        public IScrollTable() {
                headerContainer.setStyleName("iscrolltable-header");
@@ -91,7 +94,9 @@ public class IScrollTable extends Composite implements Paintable, ITable, Scroll
                this.immediate = uidl.getBooleanAttribute("immediate");
                this.totalRows = uidl.getIntAttribute("totalrows");
                this.pageLength = uidl.getIntAttribute("pagelength");
-               this.firstvisible = uidl.getIntVariable("firstvisible");
+               if(pageLength == 0)
+                       pageLength = totalRows;
+               this.firstvisible = uidl.hasVariable("firstvisible") ? uidl.getIntVariable("firstvisible") : 0;
                if(uidl.hasAttribute("rowheaders"))
                        rowHeaders = true;
                if(uidl.hasAttribute("width"))
@@ -99,6 +104,11 @@ public class IScrollTable extends Composite implements Paintable, ITable, Scroll
                if(uidl.hasAttribute("height"))
                        width = uidl.getIntAttribute("height");
                
+               if(uidl.hasVariable("sortascending")) {
+                       this.sortAscending = uidl.getBooleanVariable("sortascending");
+                       this.sortColumn = uidl.getStringVariable("sortcolumn");
+               }
+               
                UIDL columnInfo = null;
                UIDL rowData = null;
                for(Iterator it = uidl.getChildIterator(); it.hasNext();) {
@@ -157,8 +167,15 @@ public class IScrollTable extends Composite implements Paintable, ITable, Scroll
                                if(c != null && c.getColKey().equals(cid)) {
                                        c.setText(col.getStringAttribute("caption"));
                                } else {
-                                       tHead.setWidget(0, colIndex, 
-                                                       new HeaderCell(cid, col.getStringAttribute("caption")));
+                                       c = new HeaderCell(cid, col.getStringAttribute("caption"));
+                                       tHead.setWidget(0, colIndex, c );
+                               }
+                               if(col.hasAttribute("sortable")) {
+                                       c.setSortable(true);
+                                       if(cid.equals(sortColumn))
+                                               c.setSorted(true);
+                                       else
+                                               c.setSorted(false);
                                }
                        }
                }
@@ -231,11 +248,9 @@ public class IScrollTable extends Composite implements Paintable, ITable, Scroll
                rowRequestHandler.cancel();
                
                // fix headers horizontal scrolling
-               System.out.println("scrolling header to " + scrollLeft);
                headerContainer.setHorizontalScrollPosition(scrollLeft);
 
-               
-               firstRowInViewPort = (int) Math.ceil( scrollTop / tBody.getRowHeight() );
+               firstRowInViewPort = (int) Math.ceil( scrollTop / (double) tBody.getRowHeight() );
                client.console.log("At scrolltop: " + scrollTop + " At row " + firstRowInViewPort);
                
                int postLimit = (int) (firstRowInViewPort + pageLength + pageLength*CACHE_REACT_RATE);
@@ -448,19 +463,28 @@ public class IScrollTable extends Composite implements Paintable, ITable, Scroll
                }
        }
        
-       public class HeaderCell extends Composite {
+       public class HeaderCell extends Composite implements ClickListener {
                
                private DragWidget dragWidget;
                private Label text;
+               
+               private boolean sortable = false;
+               private String cid;
 
                private HeaderCell(){};
-               public void setText(String stringAttribute) {
-                       text.setText(stringAttribute);
-               }
-               public String getColKey() {
-                       return dragWidget.getColKey();
+               
+               public void setSortable(boolean b) {
+                       if(b == sortable)
+                               return;
+                       sortable = b;
+                       if(sortable)
+                               text.addClickListener(this);
+                       else
+                               text.removeClickListener(this);
                }
+               
                public HeaderCell(String colId, String headerText) {
+                       this.cid = colId;
                        FlowPanel fp = new FlowPanel();
                        DOM.setStyleAttribute(fp.getElement(), "white-space", "nowrap");
 
@@ -483,6 +507,44 @@ public class IScrollTable extends Composite implements Paintable, ITable, Scroll
                        text.setWidth((w - DragWidget.DRAG_WIDGET_WIDTH - 4) + "px");
                        setWidth(w + "px");
                }
+               public void setText(String stringAttribute) {
+                       text.setText(stringAttribute);
+               }
+               public String getColKey() {
+                       return dragWidget.getColKey();
+               }
+               
+               /**
+                * Listens clicks to headers text. This changes sorting.
+                */
+               public void onClick(Widget sender) {
+                       if(sortColumn.equals(cid)) {
+                               // just toggle order
+                               client.updateVariable(id, "sortascending", !sortAscending, false);
+                       } else {
+                               // set table scrolled by this column
+                               client.updateVariable(id, "sortcolumn", cid, false);
+                       }
+                       // get also cache columns at the same request
+                       bodyContainer.setScrollPosition(0);
+                       firstvisible = 0;
+                       rowRequestHandler.setReqFirstRow(0);
+                       rowRequestHandler.setReqRows((int) (2*pageLength*CACHE_RATE + pageLength));
+                       rowRequestHandler.deferRowFetch();
+               }
+               
+               private void setSorted(boolean sorted) {
+                       if(sorted) {
+                               if(sortAscending)
+                                       this.setStyleName("headerCellAsc");
+                               else
+                                       this.setStyleName("headerCellDesc");
+                       } else {
+                               this.setStyleName("headerCell");
+                       }
+               }
+
+
        }
        
 }
index 2eb762aaaa22d8e4bfd36f805acf4033c984d1ed..f0249f9648102e0fae2450b909db8a4d0a4d79c2 100644 (file)
@@ -103,13 +103,14 @@ public class IScrollTableBody extends Panel {
                                addRowBeforeFirstRendered(rowArray[i]);
                                firstRendered--;
                        }
-               } else if (firstIndex > lastRendered || firstIndex + rows < firstRendered) {
+//             } else if (firstIndex > lastRendered || firstIndex + rows < firstRendered) {
+               } else if (true) {
                        // complitely new set of rows
                        // create one row before truncating row
                        IScrollTableRow row = createRow((UIDL) it.next());
                        while(lastRendered + 1 > firstRendered)
                                unlinkRow(false);
-
+                       fixSpacers();
                        addRow(row);
                        firstRendered = firstIndex;
                        this.lastRendered = firstIndex + rows - 1 ;
@@ -117,6 +118,7 @@ public class IScrollTableBody extends Panel {
                                addRow(createRow((UIDL) it.next()));
                        fixSpacers();
                } else {
+                       // sorted or column reordering changed
                        client.console.log("Bad update" + firstIndex + "/"+ rows);
                }
        }
index e0ae7ce99ee01de9e916cb49e21472a4ba8f0682..96918358eca0cb9fe42eb887b1658d3b129b311a 100644 (file)
@@ -23,15 +23,25 @@ select {
        margin:0;
        padding:0;
        border:0;
-       background: yellow;
 }
 
 .iscrolltable-header table td {
        margin:0;
        padding:0;
        border:0;
+       background:yellow;
 }
 
+.iscrolltable-header .headerCell {
+}
+.iscrolltable-header .headerCellAsc {
+       height:2px;
+       background: blue;
+}
+.iscrolltable-header .headerCellDesc {
+       height:2px;
+       background: cyan;
+}
 
 
 .iscrolltable-table {
index aefe4336c32f3d9eb61e9251a18110af72a0eb58..c1fdd0586ee51bcc1ee84a96d1708e3850a0d160 100644 (file)
@@ -1550,7 +1550,7 @@ public class Table extends Select implements Action.Container,
 
                // The cursors are only shown on pageable table
                if (first != 0 || getPageLength() > 0)
-                       target.addVariable(this, "firstvisible", first + 1);
+                       target.addVariable(this, "firstvisible", first);
 
                // Sorting
                if (getContainerDataSource() instanceof Container.Sortable) {