In GXT 3 how to access another field in same row during handling of onCompleteEdit event - gwt

I have a GridInlineEditing object for GXT 3.0 grid. It all works as expected - I can capture user editing events and find out the row, column and model/change-record for this event.
What I have not figured out is how to best access another control in that row.
Specifically, I have this Column model:
private void initializeColumnModel() {
// Create the configurations for each column in the grid
List<ColumnConfig<Reminder, ?>> ccs = new LinkedList<ColumnConfig<Reminder, ?>>();
typeColumnConfig = new ColumnConfig<Reminder,String>( properties.name(), 120, "Type" );
completedColumnConfig = getDateCellColumn( properties.completed_(), 200, "Completed" );
dueColumnConfig = getDateCellColumn( properties.due_(), 200, "Due" );
applicableColumnConfig = new ColumnConfig<Reminder,Boolean>( properties.applicable(), 140, "Applicable");
// Add column configurations to ColumnModel.
ccs.add(typeColumnConfig);
ccs.add(completedColumnConfig);
ccs.add(dueColumnConfig);
ccs.add(applicableColumnConfig);
applicableColumnConfig.setAlignment(HasHorizontalAlignment.ALIGN_CENTER);
typeColumnConfig.setAlignment(HasHorizontalAlignment.ALIGN_CENTER);
reminderColumnModel = new ColumnModel<Reminder>(ccs);
}
and am attempting to intercept a change to the 'completed date' value and then programmatically operate the 'due date' control (adding a fixed number of years to that as if the user just did it).
Here's how I capture the event:
#Override
public Widget asWidget() {
if(!gridInitialized){
editing = new GridInlineEditing<Reminder>(grid);
DateField dueDateField = getDateField();
DateField completedDateField = getDateField();
editing.addEditor(dueColumnConfig, dueDateField);
editing.addEditor(completedColumnConfig, completedDateField);
editing.addEditor(applicableColumnConfig, new CheckBox());
editing.addCompleteEditHandler(new CompleteEditHandler<Reminder>(){
#Override
public void onCompleteEdit(CompleteEditEvent<Reminder> event) {
GridCell cell = event.getEditCell();
int row = cell.getRow();
int col = cell.getCol();
Reminder rem = reminderStore.get(row);
Store<Reminder>.Record rec = reminderStore.getRecord(rem);
//System.out.println("row:"+row+", col:"+col+", applic:"+rem.getApplicable());
//System.out.println("rec:"+rec.toString());
Change<Reminder, Boolean> applicChange = rec.getChange(properties.applicable());
Change<Reminder, Date> dueChange = rec.getChange(properties.due_());
Change<Reminder, Date> comChange = rec.getChange(properties.completed_());
System.err.print("Row "+(row+1)+" changed: ");
if(applicChange!=null){
boolean applicValue = applicChange.getValue();
System.out.println("applicable changed to "+applicValue);
}
if(dueChange!=null){
Date dueValue = dueChange.getValue();
System.out.println("due changed to: "+SimpleDate.convertFromDate(dueValue));
}
if(comChange!=null){
Date comValue = comChange.getValue();
System.out.println("com changed to: "+SimpleDate.convertFromDate(comValue));
try{
fixDueDate(row,comValue, rem);
}
catch(Exception e){
//boo.
System.err.println("Could not update the due date for this completion date change.");
}
}
}
});
gridInitialized=true;
}
// Initialize the Revert Changes button.
revert.addSelectHandler(new SelectHandler(){
#Override
public void onSelect(SelectEvent event) {
reminderStore.rejectChanges();
}
});
return widget;
}
The method 'fixDueDate' is where I hope to place my logic that adjusts the other column control (another DateField):
private void fixDueDate(int row, Date completedDate, Reminder rem) throws InvalidDateFormatException, InvalidDateException{
SimpleDate newCompDate = new SimpleDate(completedDate);
SimpleDate dueDate = newCompDate.addYears(rem.getRenewalYears());
//rem.setDue(dueDate.getFormattedDate());
//reminderStore.update(rem);
}
thanks.

This works:
private void fixDueDate(int row, Date completedDate, Reminder rem, Store<Reminder>.Record rec )
throws InvalidDateFormatException, InvalidDateException{
SimpleDate newCompDate = new SimpleDate(completedDate);
SimpleDate dueDate = newCompDate.addYears(rem.getRenewalYears());
rec.addChange(dueColumnConfig.getValueProvider(), dueDate.toDate());
}
and the modified field marks appear on both the user modified field and the field modified by this rec.addChange call above.

Related

Wicket 7 - AutoCompleted Text field - to have onSelect method

We would like to implement AutoCompleteTextField field, once user has selected the field from AutoComplete result, then system would auto populate on other text field, i have used the component AjaxFormComponentUpdatingBehavior (blur), however this will take effect on every text input from AutoCompleteTextField field, but if i change to AjaxFormComponentUpdatingBehavior (change), it doesnt work.
Below is the sample code:
AutoCompleteTextField<String> field_postcode = new AutoCompleteTextField<String>("field_postcode",
new PropertyModel<String>(getModelObject(), "wAdditionalInfo.postal"), autoCompleteRenderer) {
private static final long serialVersionUID = 1L;
#Override
protected Iterator<String> getChoices(String input) {
if (Strings.isEmpty(input)) {
List<String> emptyList = Collections.emptyList();
return emptyList.iterator();
}
List<String> choices = new ArrayList<String>();
List<Postcode> postcodeList = getProfileManager().findAllPostcodeByPostcode(input);
for (Postcode p : postcodeList) {
String postcode = p.getPostcode();
if (postcode.startsWith(input)) {
choices.add(p.getPostcode());
if (choices.size() == 10) {
break;
}
}
}
return choices.iterator();
}
};
field_postcode.setRequired(true);
field_postcode.add(new AjaxFormComponentUpdatingBehavior("blur"){
private static final long serialVersionUID=-1107858522700306810L;
#Override protected void onUpdate( AjaxRequestTarget target){
Postcode postcode = getProfileManager().findPostcodeByPostcode(field_postcode.getInput());
if (postcode != null) {
City city = postcode.getCity();
State state = city.getState();
field_city.setModelObject(city.getCity());
ddl_state.setModelObject(state);
if (isDisplayTip) {
//isDisplayTip true mean is from widrawal webform
isReadonly = true;
} else {
field_city.setEnabled(false);
}
ddl_state.setEnabled(false);
} else {
if (isDisplayTip) {
isReadonly = false;
} else {
field_city.setEnabled(true);
}
ddl_state.setEnabled(true);
}
target.add(field_city, ddl_state);
}
}
);
Is there any api from wicket to achieve this? We need to have something when user select the option from Auto complete, then it only onUpdate method of AjaxFormComponentUpdatingBehavior
According to https://github.com/apache/wicket/blob/cbc237159c4c6632b4f7db893c28ab39d1b40ed4/wicket-extensions/src/main/java/org/apache/wicket/extensions/ajax/markup/html/autocomplete/wicket-autocomplete.js#L620 it should trigger change event on the HTMLInputElement and thus notify you on the server side.
Use the browser debugger to see whether https://github.com/apache/wicket/blob/cbc237159c4c6632b4f7db893c28ab39d1b40ed4/wicket-extensions/src/main/java/org/apache/wicket/extensions/ajax/markup/html/autocomplete/wicket-autocomplete.js#L453 is executed and whether it leads to an Ajax call with the value in the parameters.

Select Children when selecting Parent checkbox

I am using a Grid where the first column is checkbox. Every row is a folder which can have many other elements to be selected. There could be another folder inside a folder.
Now, when I have to select a element I have to select it one by one. I am not able to understand that how could I make it possible that if I check a folder checkbox, It checks the all selectable elements inside this folder.
Please let me know if more info required.
RemoteSortTreeLoader<BasicModel> loader =
new BaseRemoteSortTreeLoader<BasicModel>(proxy, reader) {
public boolean hasChildren(BasicModel parent) {
//code;
}
};
TreeStore store = new TreeStore(loader);
List<ColumnConfig> columnList = new ArrayList<ColumnConfig>();
CheckBoxSelectionModel checkBoxSelectionModel =
new CheckBoxSelectionModel();
columnList.add(checkBoxSelectionModel.getColumn());
ColumnModel columns = new ColumnModel(columnList);
EditorTreeGrid grid = new EditorTreeGrid<BasicModel>(store,columns);
grid.getSelectionModel().setSelectionMode(SelectionMode.SIMPLE);
grid.getSelectionModel().addListener(Events.BeforeSelect,
new Listener<SelectionEvent<BasicModel>>() {
#Override
public void handleEvent(SelectionEvent<BasicModel> event) {
if (event.getModel() instanceof SDPTimelineCatalogModel) {
event.setCancelled(false);
}
} // handleEvent
}
);
grid.getSelectionModel().addSelectionChangedListener(
new SelectionChangedListener<BasicModel>() {
#Override
public void selectionChanged(SelectionChangedEvent<BasicModel> event) {
logger.info(" Inside addSelectionChangedListener ");
if (event.getSelection().size() == 0) {
disableNext();
} else {
enableNext();
}
} // selectionChanged
}
);
thanks

How to apply like search on GWT cell table?

I am using GWT 2.3.I which I am using GWT cell table.
Here below is the code for my cell table:
public class FormGrid extends SuperGrid {
List<Form> formList;
#Override
public void setColumns(CellTable table) {
TextColumn<Form> nameColumn = new TextColumn<Form>() {
#Override
public String getValue(Form object) {
return object.getName();
}
};
table.addColumn(nameColumn, "Name");
}
#Override
public void setData() {
if (formList != null && formList.size() > 0) {
AsyncDataProvider<Form> provider = new AsyncDataProvider<Form>() {
#Override
protected void onRangeChanged(HasData<Form> display) {
int start = display.getVisibleRange().getStart();
int end = start + display.getVisibleRange().getLength();
end = end >= formList.size() ? formList.size() : end;
List<Form> sub = formList.subList(start, end);
updateRowData(start, sub);
}
};
provider.addDataDisplay(getTable());
provider.updateRowCount(formList.size(), true);
}
}
public List<Form> getFormList() {
return formList;
}
public void setFormList(List<Form> formList) {
this.formList = formList;
}
}
In this my set column and set data will be called fro super class flow.This cell table is working fine.
Now I want to put a filter type facility (like search) in this cell table.It should be like, there is a texbox above the cell table and what ever written in that text box, it should fire a like query to all form name for that text box value.
for example I have 1000 form in the grid.Now if user writes 'app' in some filter textbox above the cell table the all the form which have 'app' in there name will be filtered and grid has only those forms only.
This is the first case:
Another case is I am only render one column in grid name.I have two more properties in form (description,tag).But I am not rendering them.now for filter if user writes 'app' in filter box then it should make a query to all three (name, description, and tag) and should return if 'app' matched to any of three.
I am not getting how to apply filter in cell table.
Please help me out.Thanks in advance.
You can find an implementation in the expenses sample.
Here is a short summary of the steps
1.) Create a Textbox and a SearchButton.
2.) add a clickHandler to the SearchButton (You can also add KeyUpHandler to the Textbox alternatively)
searchButton.addClickHandler(new ClickHandler() {
public void onClick(ClickEvent event) {
search();
}
});
3.) In the search function retrieve the searchString and store it.
private void search() {
searchString = searchBox.getText();
setData();
}
4.) modify your setdata() function to take searchString into account
#Override
public void setData() {
if (formList != null && formList.size() > 0) {
AsyncDataProvider<Form> provider = new AsyncDataProvider<Form>() {
#Override
protected void onRangeChanged(HasData<Form> display) {
int start = display.getVisibleRange().getStart();
int end = start + display.getVisibleRange().getLength();
//new function if searchString is specified take into account
List<Form> sub = getSubList(start,end);
end = end >= sub.size() ? sub.size() : end;
updateRowData(sub.subList(start, end);, sub);
}
};
provider.addDataDisplay(getTable());
provider.updateRowCount(formList.size(), true);
}
}
private List<Form> getSubList(int start, int end) {
List<Form> filtered_list = null;
if (searchString != null) {
filtered_list= new ArrayList<Form>();
for (Form form : formList) {
if (form.getName().equals(searchString) || form.getTag().equals(searchString) || form.getDescription().equals(searchString))
filtered_list.add(form);
}
}
else
filtered_list = formList;
return filtered_list;
}
can propose another solution what can be used quite easy multiple times.
Idea is to create custom provider for your celltable.
GWT celltable filtering
Video in this post shows it in action.
Here is the part of code of custom list data provider which u have to implement.
#Override
protected void updateRowData(HasData display, int start, List values) {
if (!hasFilter() || filter == null) { // we don't need to filter, so call base class
super.updateRowData(display, start, values);
} else {
int end = start + values.size();
Range range = display.getVisibleRange();
int curStart = range.getStart();
int curLength = range.getLength();
int curEnd = curStart + curLength;
if (start == curStart || (curStart < end && curEnd > start)) {
int realStart = curStart < start ? start : curStart;
int realEnd = curEnd > end ? end : curEnd;
int realLength = realEnd - realStart;
List<t> resulted = new ArrayList<t>(realLength);
for (int i = realStart - start; i < realStart - start + realLength; i++) {
if (filter.isValid((T) values.get(i), getFilter())) {
resulted.add((T) values.get(i));
}
}
display.setRowData(realStart, resulted);
display.setRowCount(resulted.size());
}
}
}

How to hide column in Cell table GWT?

I am using Cell Table in GWT.In that cell table I am adding these columns.
TextColumn<Document> idColumn = new TextColumn<Document>() {
#Override
public String getValue(Document object) {
return Long.toString(object.getId());
}
};
TextColumn<Document> refColumn = new TextColumn<Document>() {
#Override
public String getValue(Document object) {
return object.getReferenceNumber();
}
};
/*
* DateCell dateCell = new DateCell(); Column<Contact, Date> dateColumn
* = new Column<Contact, Date>(dateCell) {
*
* #Override public Date getValue(Contact object) { return
* object.birthday; } };
*/
TextColumn<Document> nameColumn = new TextColumn<Document>() {
#Override
public String getValue(Document object) {
return object.getDocumentName();
}
};
table = new CellTable<T>();
table.addColumn(idColumn, "Id");
table.addColumn(refColumn, "Reference Number");
table.addColumn(nameColumn, "Name");
}
Now I have some queries:
How to hide the id column?
On click of row how can i get the from selected row?
Please help me out.
Thanks in advance.
Well you could try to use fixed layout for the CellTable and set the width of the specific column you want to hide to 0px.
I did use another approach.
In my case I have a cellTable which should display a checkbox column as soon as I press a button (which puts the celltable in edit mode).
I do this by creating a CheckBoxColumn and inserting and removing it when I press on the button. It looks seomething like that:
#Override
public void insertCheckBoxColumn(Column<Object,Boolean> column) {
if (cellTable.getColumnIndex(column) == -1) {
cellTable.addColumn(column,"");
cellTable.setColumnWidth(column,50, Unit.PX);
}
}
#Override
public void removeCheckBoxColumn(Column<Object, Boolean> column) {
int index = cellTable.getColumnIndex(column);
if (index != -1)
cellTable.removeColumn(index);
}
However note that you might run into this issue on google chrome.

Display Field for select item in the list grid

Hello all
I am using smart gwt 2.2 on windows with mozilla browser.
I am using a list grid with two fields.
I set the editor type of second field to SelectItem.
In that select item I am rendering a grid.
For select item I set the value field and the display field.
But after the select any item from select item it is display value field.
I am also attaching the code for it.
code for grid
public void initializeGrid() {
ListGrid grid = new ListGrid();
setGridProperty(grid);
grid.setFields(getGridFields());
grid.setData(getGridData());
getSmartContainer().addChild(grid);
}
private static ListGridRecord[] getGridData() {
ListGridRecord record = new ListGridRecord();
record.setAttribute("id", "");
record.setAttribute("name", "");
ListGridRecord record1 = new ListGridRecord();
record1.setAttribute("id", "");
record1.setAttribute("name", "");
return new ListGridRecord[] { record, record1 };
}
private static void setGridProperty(ListGrid grid) {
grid.setWidth("90%");
grid.setHeight(125);
grid.setCanEdit(true);
grid.setEditEvent(ListGridEditEvent.CLICK);
grid.setEditByCell(true);
grid.setAlternateRecordStyles(true);
grid.setShowAllRecords(true);
}
private static ListGridField[] getGridFields() {
ListGridField field = new ListGridField("id");
FormItem item = new TextItem();
field.setEditorType(item);
ListGridField field1 = new ListGridField("name");
SelectItem item1 = new SelectItem("name");
setPropertyForSelectitem(item1);
DataSource dataSource = new DataSource();
dataSource.setClientOnly(true);
item1.setOptionDataSource(dataSource);
setDataForSelectItem(dataSource);
field1.setEditorType(item1);
return new ListGridField[] { field, field1 };
}
Code for select item
public static void setDataForSelectItem(DataSource dataSource) {
for (int i = 0; i < 2; i++) {
ListGridRecord record = new ListGridRecord();
record.setAttribute("id", "1");
record.setAttribute("name", "name" + i);
record.setAttribute("address", "address" + i);
dataSource.addData(record);
}
}
private static void setPropertyForSelectitem(SelectItem item) {
item.setPickListFields(getFieldsForSelectItem());
item.setWidth(150);
item.setValueField("id");
item.setDisplayField("name");
item.setPickListWidth(250);
}
private static ListGridField[] getFieldsForSelectItem() {
ListGridField idField = new ListGridField("id");
ListGridField nameField = new ListGridField("name");
ListGridField addField = new ListGridField("address");
return new ListGridField[] {idField,nameField,addField };
}
[/CODE]
After drawing this grid it is rendering value field for the select item, but i want to render the name (as it is the display field for the select item).
Same select item I used in the dynamic form but it is working well at that place, but for the grid it is not working well.
Please Help.
Thanks