2009-09-04 6 views
1

J'ai implémenté mon trieur de colonnes personnalisé qui est utilisé pour trier les éléments dans ma table.Comment implémenter un ColumnSorter personnalisé avec ScrollTable (GWT-incubator)

 
    class FileColumnSorter extends SortableGrid.ColumnSorter 
    { 
     @Override 
     public void onSortColumn(SortableGrid sortableGrid, TableModelHelper.ColumnSortList columnSortList, 
           SortableGrid.ColumnSorterCallback columnSorterCallback) 
     .... 
    } 

Quand j'initialiser le FixedWidthGrid que je fais ce qui suit:

 
FixedWidthGrid dataTable = new FixedWidthGrid(rows, cols); 
dataTable.setSelectionPolicy(SelectionGrid.SelectionPolicy.ONE_ROW); 
dataTable.setColumnSorter(new FileColumnSorter()); 

Le scrolltable est initialisé de la façon suivante:

 
FixedWidthFlexTable headerTable = createHeaderTable(); 

// Calling the lines described above 
FixedWidthGrid fileListGrid = createDataTable 
(currentDescriptorList.size(), 6); 

// Combine the components into a ScrollTable 
scrollTable = new ScrollTable(fileListGrid, headerTable); 
scrollTable.setSortPolicy(AbstractScrollTable.SortPolicy.SINGLE_CELL); 
scrollTable.setColumnSortable(0, false); 
scrollTable.setColumnSortable(1, true); 
scrollTable.setColumnSortable(2, true); 
scrollTable.setColumnSortable(3, true); 
scrollTable.setColumnSortable(4, true); 
scrollTable.setColumnSortable(5, false); 

Quand je lance l'application, je reçois le construit en tri au lieu de mon tri personnalisé . J'ai aussi essayé de faire ce qui suit:

 
ColumnSorter sorter = new FileColumnSorter(); 
FixedWidthGrid dataTable = new FixedWidthGrid(rows, cols) { 
    @Override 
    public ColumnSorter getColumnSorter() 
    { 
     return sorter; 
    } 
}; 

Pour vous assurer que mon trieuse se habituer, mais je reçois toujours la même expérience .

Mise à jour: Ajout de la FileColumnSorter

class FileColumnSorter extends SortableGrid.ColumnSorter 
{ 
    @Override 
    public void onSortColumn(SortableGrid sortableGrid, 
     TableModelHelper.ColumnSortList columnSortList, 
     SortableGrid.ColumnSorterCallback columnSorterCallback) 
    { 
     final int column = columnSortList.getPrimaryColumn(); 

     final Integer[] originalOrder = new Integer[sortableGrid.getRowCount()]; 
     for (int i = 0; i < originalOrder.length; i++) 
     { 
      originalOrder[i] = i; 
     } 

     Arrays.sort(originalOrder, new Comparator<Integer>() { 
      public int compare(Integer first, Integer second) 
      { 
       Descriptor firstDesc = share.getCurrentDescriptors().get(first); 
       Descriptor secondDesc = share.getCurrentDescriptors().get(second); 

       if (firstDesc.getType().equals(secondDesc.getType())) 
       { 
        switch (column) 
        { 
         case 0: 
          return firstDesc.compareTo(secondDesc); 
         case 1: 
          return firstDesc.getName().compareTo(secondDesc.getName()); 
         case 2: 
          return ((Long) firstDesc.getSize()).compareTo(secondDesc.getSize()); 
         case 3: 
          return firstDesc.getCreated().compareTo(secondDesc.getCreated()); 
         case 4: 
          return firstDesc.getModified().compareTo(secondDesc.getModified()); 
         default: 
          return firstDesc.compareTo(secondDesc); 
        } 
       } 
       else 
       { 
        return firstDesc.getType() == Descriptor.FileItemType.FOLDER ? 1 : -1; 
       } 
      } 
     }); 

     int[] resultOrder = new int[originalOrder.length]; 
     for (int i = 0; i < originalOrder.length; i++) 
     { 
      if (columnSortList.isPrimaryAscending()) 
      { 
       resultOrder[i] = originalOrder[i]; 
      } 
      else 
      { 
       resultOrder[resultOrder.length - i - 1] = originalOrder[i]; 
      } 
     } 
     columnSorterCallback.onSortingComplete(resultOrder); 
    } 
} 
+0

Ce modèle fonctionne pour moi. Pouvez-vous ajouter le code pour 'FileColumnSorter'? –

+0

Ont mis à jour le avec l'implémentation du trieur. Cela inclut un descripteur qui est un DTO avec quelques champs de base. – tronda

+0

Question idiote - vous avez FileColumnSort comme nom de classe dans le code affiché, mais instanciez FileColumnSorter. Juste au cas où vous auriez deux cours différents par accident et instancié le mauvais ... – Joe

Répondre

Questions connexes