Smart GWT List Grid - Setting a hilite to a list grid on record click - gwt

I'm trying to set a hilite inside the record click handler of the list grid. I have tired the following code,
My hilites are as follows,
public static Hilite[] getWayBillSetHilites() {
return new Hilite[]{
new Hilite() {
{
setFieldNames("RECORD_VIEWED_STATUS");
setCriteria(new Criterion("RECORD_VIEWED_STATUS", OperatorId.EQUALS, "TRUE"));
setCssText(Constant.Css.TEXT_ITALIC_GRAY_32);
setTextColor("font-style:italic;color:#525252;");
setId("0");
}
}
};
}
record click handler of the grid appears as follows,
grid.addRecordClickHandler(new RecordClickHandler() {
#Override
public void onRecordClick(RecordClickEvent recordClickEvent) {
//gridWayBillSetGrid.getHiliteState()
//make RECORD_VIEWED_STATUS value "true"
recordClickEvent.getRecord().setAttribute("RECORD_VIEWED_STATUS", true);
gridWayBillSetGrid.enableHilite("0", true);
}
});
But when I click on the record, the styles are not showing up.
Please be kind to advise on this.

I think it's the wrong use case for hilites. Use getCellCSSText instead.

Try this one (override getCellCSSText method of ListGrid class):
ListGrid grid = new ListGrid(...){
#Override
protected String getCellCSSText(ListGridRecord record, int rowNum, int colNum) {
if("true".equalsIgnoreCase(record.getAttribute("RECORD_VIEWED_STATUS"))){
return "font-style:italic;color:#525252;";
}
return super.getCellCSSText(record, rowNum, colNum);
}
};

Related

GWT DataGrid header click event for sorting

I want to know if user clicked on header for Ascending or Descending sorting.
How can I find that?
Regards
Try this:
1) create SortHandler and attach to your grid
ColumnSortEvent.AsyncHandler sortHandler= new ColumnSortEvent.AsyncHandler(grid);
grid.addColumnSortHandler(sortHandler);
ColumnSortList columnSortList = grid.getColumnSortList();
2) create your Column (TextColumn etc) such as:
TextColumn<Dog> yourTextColumn = new TextColumn<Dog>() {
#Override
public String getValue(String object) {
return object;
}
}
3) create your Header
Header yourHeader = new Header() {
#Override
public Object getValue() {
return value; // here return header value
}
}
3) add column to grid and push a ColumnSortInfo onto the list
grid.addColumn(yourTextColumn, yourHeader);
columnSortList.push(new ColumnSortList.ColumnSortInfo(yourColumn, true));
You can always look here for more useful examples.
in order to get the direction of the ordering, all you have to do is to get the ColumnSortList and call method #get(0) with zero index. as documentation of ColumnSortList says the "The 0th item is the {#link ColumnSortInfo} of the most recently sorted * column.". so the returned object (which is ColumnSortList.ColumnSortInfo) contains information about last clicked Column and it has a method called #isAscending() which is a flag to identify the direction.
the sample code:
ColumnSortList sortList = dataGrid.getColumnSortList();
ColumnSortList.ColumnSortInfo info = sortList.get(0);
also want to note that this code block should be in AsyncDataProvider#onRangeChanged.
there is also an alternative to this, consider the following:
columnSortHandler = new ColumnSortEvent.AsyncHandler(table) {
#Override
public void onColumnSort(ColumnSortEvent event) {
List<T> newData = new ArrayList(table.getVisibleItems());
if (event.isSortAscending()) {
Collections.sort(newData, (Comparator)event.getColumn());
} else {
Collections.sort(newData, (Comparator)event.getColumn());
Collections.reverse(newData);
}
table.setRowData(newData);
}
};
table.addColumnSortHandler(columnSortHandler);

Double Click event for DataGrid

I added double click event for DataGrid, but it doesn't work correctly. The code handles a single click, but it does not handle double click.
Please help.
private DataGrid<Contract> table = new DataGrid<Contract>();
table.addCellPreviewHandler(new Handler<Contract>() {
#Override
public void onCellPreview(final CellPreviewEvent<Contract> event) {
if (BrowserEvents.DBLCLICK.equals(event.getNativeEvent().getType())) {
//it doesn't handle
Window.alert("Tro-lo-lo");
}
if (BrowserEvents.CLICK.equals(event.getNativeEvent().getType())) {
//it handles
Window.alert("Tru-la-la");
}
}
});
DataGrid has many things in common with CellTable. So solutions from this question must work for you too:
Using CellPreviewHandler count time between two clicks
Or you can add DoubleClickHandler using addDomHandler method
dataGrid.addDomHandler(new DoubleClickHandler() {
#SuppressWarnings("unchecked")
#Override
public void onDoubleClick(DoubleClickEvent event) {
DataGrid<YourDataProviderType> grid = (DataGrid<YourDataProviderType>) event.getSource();
int row = grid.getKeyboardSelectedRow();
YourDataProviderType item = grid.getVisibleItem(row);
Window.alert("Do Something Here");
}
}, DoubleClickEvent.getType());

GWT CellTable SelectionModel can not deselect item after editing

Hello I have a Contact class with informations which i show in a CellTable.
The CellTable has a DataListProvider, MultiSelectionModel and KeyProvider
which checks the id of the Contact.
DataListProvider and CellTable have the same KeyProvider.
if i only select/deselect the items in the CellTable and show them in a TextBox ists working fine. But the when i change the value of the Contact item in the TextBox(Contact instance) and try to deselect the item the selectionmodel says its still selected?
I tried with clear() but its still selected!
GWT 2.5 / FireFox
ProvidesKey<Contact> keyProvider = new ProvidesKey<Contact>(){
#Override
public Object getKey(Contact item) {
return item.getIdContact();
}
};
public MyCellTable(boolean canLoad, Integer pagesize, ProvidesKey<T> keyProvider) {
super(-1, resource, keyProvider);
selectionModel = new MultiSelectionModel<T>();
selectionModel .addSelectionChangeHandler(new SelectionChangeEvent.Handler() {
#Override
public void onSelectionChange(SelectionChangeEvent event) {
selectionChange();
}
});
dataProvider = new ListDataProvider<T>(keyProvider);
dataProvider.addDataDisplay(this);
}
in the selection event i call
protected void selectionChange(){
Contact c = grid.getGrid().getSelectedItem();
if(c != null){
cpForm.enable();
cpForm.clear();
form.bind(c); // Formular which updates the selected instance
cpForm.add(form);
}else{
cpForm.disable(noseletionText);
}
}
i have no ValueUpdater
when i select an item i generate a formular and if i change something i call:
#Override
public void save() {
super.save();
ContactServiceStore.get().updateContact(manager.getBean(),
new MyAsyncCallback<Void>() {
#Override
public void onSuccess(Void result) {
onchange();
}
});
}
i if call the method without changes on the contact its still working and i can deselect but when i change the name or something else i cant select other items or deselect the current item!
You're not actually using your ProvidesKeys in your MultiSelectionModel. You need to create your MultiSelectionModel like so:
MultiSelectionModel<T> selectionModel = new MultiSelectionModel<T>(keyProvider);
If you don't supply the MultiSelectionModel with a ProvidesKey it will use the actual object as a key.
Make sure you also add the MultiSelectionModel to the table:
cellTable.setSelectionModel(selectionModel);
The reason selectionModel.clear() wasn't working was because selectionModel was not set to the table.

GWT CellTable Custom Selection Model

I need a 'custom selection model' for GWT CellTable. One of the columns in CellTable is a Checkbox column.
Basic rquirements (both work in solution below):
- Row click (not on checkbox), selects that row and un-selects all other rows.
- Checkbox selection should select/un-select that row only.
Following is the code I am using, but its very very slow. Any guidance would be appreciated.
final SelectionModel<T> selectionModel = new MultiSelectionModel<T>();
dataTable.setSelectionModel(selectionModel,
DefaultSelectionEventManager.createCustomManager(
new DefaultSelectionEventManager.CheckboxEventTranslator<T>() {
#Override
public SelectAction translateSelectionEvent(CellPreviewEvent<T> event) {
SelectAction action = super.translateSelectionEvent(event);
if (action.equals(SelectAction.IGNORE)) {
selectionModel.clear();
return SelectAction.TOGGLE;
}
return action;
}
}
)
);
Following is the code snipped for CheckColumn callback.
Column<T, Boolean> checkColumn = new Column<T, Boolean>(
new CheckboxCell(true, false))
{
#Override
public Boolean getValue(T t)
{
// Get the value from the selection model.
return selectionModel.isSelected(t);
}
};
I have put in a KeyProvider for the CellTable and its not slow anymore. :)
ProvidesKey<T> keyProvider = new ProvidesKey<T>() {
public Object getKey(T t) {
return tip == null? null : tip.getId();
}
};
dataTable = new CellTable<T>(PAGE_SIZE, keyProvider);
You could just whitelist your checkbox
int checkboxColumn = 0;
DefaultSelectionEventManager.createCustomManager(new DefaultSelectionEventManager
.WhitelistEventTranslator(checkboxColumn));

trying to add some link cell in my GWT cellTable

I am trying to add a Link in my cell table (I just want the item to be underlined and mouse symbol change on hover)
and on click I just want to give a window Alert .
for that i have tried these Options : ( but no luck )
1)
final Hyperlink hyp = new Hyperlink("test", "test");
Column<EmployerJobs, Hyperlink> test = new Column<EmployerJobs, Hyperlink>(new HyperLinkCell())
{
#Override
public Hyperlink getValue(EmployerJobs object)
{
return hyp;
}
};
Problem with option 1 is , it takes me to navigation page "test", whereas I dont want to go any other page i just want a window alert.
2)
Column<EmployerJobs, SafeHtml> test = new Column<EmployerJobs, SafeHtml>(new SafeHtmlCell())
{
#Override
public SafeHtml getValue(EmployerJobs object)
{
SafeHtmlBuilder sb = new SafeHtmlBuilder();
sb.appendEscaped("test");
return sb.toSafeHtml();
}
};
problem with option 2 is I dont know what exactly to return here and its not getting underlined.
3) at last i am trying to add anchor in my celltable with a compositecell(as ideally i want three different anchors in my ONE cell)
final Anchor anc = new Anchor();
ArrayList list = new ArrayList();
list.add(anc);
CompositeCell ancCell = new CompositeCell(list);
Column testColumn1 = new Column<EmployerJobs, Anchor>(ancCell) {
#Override
public Anchor getValue(EmployerJobs object) {
return anc;
}
};
Option 3 is giving some exception .
If you can help me get working any of the above option, I'll be grateful
Thanks
You are doing it totally wrong. You need to use ActionCell for stuff like this or create your own cell. Example code:
ActionCell.Delegate<String> delegate = new ActionCell.Delegate<String>(){
public void execute(String value) { //this method will be executed as soon as someone clicks the cell
Window.alert(value);
}
};
ActionCell<String> cell = new ActionCell<String>(safeHtmlTitle,delegate){
#Override
public void render(com.google.gwt.cell.client.Cell.Context context, //we need to render link instead of default button
String value, SafeHtmlBuilder sb) {
sb.appendHtmlConstant("<a href='#'>");
sb.appendEscaped(value);
sb.appendHtmlConstant("</a>");
}
};
Column testColumn1 = new Column<EmployerJobs, String>(cell) {
#Override
public String getValue(EmployerJobs object) {
//we have to return a value which will be passed into the actioncell
return object.name;
}
};
I recommend to read official documentation for Cell Widgets, since it is pretty much everything what you need to know about cell widgets.