-->

How to fix the Column Ordering issue when Insert a

2019-06-14 01:48发布

问题:

I have a cellTable that has many columns like the followings:

OrderID - OrderDate - OrderTime - Name .....
1 - 2012-01-05 - 11:12:12 - Tom...... 

Each column will have a checkBox for Hide/Unhide column:

[x] orderID (CheckBox)
[x] orderDate (CheckBox)
[x] orderTime (CheckBox)
......

Requirement: I want that when users uncheck the CheckBox the corresponding column will disappear & when they check the CheckBox, the column will appear in the CORRECT ORDER as at the beginning. This is the code:

public void insertColumn(int beforeIndex, TextColumn<String[]> textColumn, String columnName){
    if(getView().getOrderListCellTable().getColumnIndex(textColumn)==-1){
        getView().getOrderListCellTable().insertColumn(beforeIndex,textColumn, columnName);
        textColumn.setSortable(true);
    }
}
public void removeColumn(TextColumn<String[]> textColumn){
    int index= getView().getOrderListCellTable().getColumnIndex(textColumn);

    if(index!=-1){
        getView().getOrderListCellTable().removeColumn(index);
    }
}

However, I got this issue, when I hide / unhinde just 1 column then the order is OK, ex, hide orderDate column then the table will be like this:

OrderID - OrderTime - Name .....
1 - 11:12:12 - Tom...... 

And ofcourse, when unhiding orderDate then it will go back to the beginning correctly.

Code to insert/remove orderDate

    getView().getOrderDateCheckBox().addClickHandler(new ClickHandler(){

        @Override
        public void onClick(ClickEvent event)       {   
            if(getView().getOrderDateCheckBox().getValue()){    

                insertColumn(1, orderDateColumn,"Order Date");
            }
            else{
                removeColumn(orderDateColumn);
            }
        }

    });

But if I hide both orderID & OrderDate & then unhide orderDate then it will be like this:

OrderTime - OrderDate - Name .....
11:12:12 - 2012-01-05 - Tom...... 

This is not right cos he orderDate should be stand before orderTime. This cos I insertColumn(1, orderDateColumn,"Order Date"); at position 1 (before index).

Can you figure out a logic to fix this issue?

回答1:

  1. Create columns.
  2. Assign an order number to each column: 0, 1, 2, etc.
  3. When inserting a column with order number X, find the first column with an order number higher than X, which is present in the table. Insert this column right before it.