You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@wicket.apache.org by Steve Tarlton <st...@gmail.com> on 2009/07/20 09:49:34 UTC
How do I set a selected item outside of an inmethod datagrid?
I have a table that contains history entries of an item table. Whenever I
change the selection in the item table, I repopulate the history in the
history table. What I would like to do is select the newest history entry in
the history table by default whenever I change the selection on the item
table.
Re: How do I set a selected item outside of an inmethod datagrid?
Posted by Marcin Palka <ma...@gmail.com>.
If I understand your problem correctly you want to do master and detail
grids. When you select an item in a master table (by means of a mouse click)
you want to repopulate the details(history) table and make a selection on
programmaticaly, right?
I would say you first need to override onItemSelection changed to intercept
selection events on your master table:
@Override
public void onItemSelectionChanged(IModel item, boolean
newValue) {
super.onItemSelectionChanged(item, newValue);
if (newValue) {
//selection has changed on the master table
//so you have to update
//the history table's datasource object
updateHistoryTableDatasource(item);//you implementation
goes here
//mark all items in a detail table for update
historyTable.markAllItemsDirty();
//refresh detail table
historyTable.update();
}
}
And for selection of a newest item in a history I would suggest that you
sort history by date that the newest item comes first and then simply select
the first item in the historyTable.
cheers
Marcin
--
View this message in context: http://www.nabble.com/How-do-I-set-a-selected-item-outside-of-an-inmethod-datagrid--tp24565364p24595335.html
Sent from the Wicket - User mailing list archive at Nabble.com.
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org
Re: How do I set a selected item outside of an inmethod datagrid?
Posted by satar <st...@gmail.com>.
Not sure if this was a tough question or simply no one on the user forum has
had such a need but I finally figured out a way to get my particular problem
solved and figured I would share. There likely may be a better way but I
tied the selection to an Override of the DataSource of the DataGrid:
...
/*
* ---------------------------------------------------------------------
* Create data source for the history grid
* ---------------------------------------------------------------------
*/
checkItemHistoryDataSource = new MatrixCheckItemHistoryDataSource(
NUMBER_OF_DAYS) {
@Override
public IModel<MatrixCheckItemModel> model(Object object) {
MatrixCheckItemModel m = (MatrixCheckItemModel) object;
IModel im = new MatrixDetachableCheckItemModel(m);
/*
* If we have selected a new item that doesn't match the history
item
* profile.
*/
if ((historyItemSelected.getId() == null)
|| !historyItemSelected.getMatrixCheckProfile().getId().equals(
listItemSelected.getMatrixCheckProfile().getId())) {
/*
* Reset the currently selected history item if it has the same
date
* as our new list item. This way it selects the current history
* instance by default, which is what we want.
*/
if (m.getMatrixShiftReport().getDay().equals(
listItemSelected.getMatrixShiftReport().getDay())) {
// historyItemSelected = m;
itemSelectedHistoryGrid.selectItem(im, true);
/*
* Reset our selected item panel.
*/
selectedItemPanel.setSelectedItem(historyItemSelected);
}
}
return im;
}
};
/*
* ---------------------------------------------------------------------
* Create and add the selected item history grid
* ---------------------------------------------------------------------
*/
itemSelectedHistoryGrid = new MatrixCheckItemHistoryGrid(
"selectedItemHistoryGrid", checkItemHistoryDataSource, colorblind) {
private static final long serialVersionUID = 1L;
@Override
public void resetSelectedItems() {
super.resetSelectedItems();
/*
* When selection changes the item panel needs to be refreshed.
*/
AjaxRequestTarget target = AjaxRequestTarget.get();
/*
* Refresh ajax target
*/
if (target != null) {
/*
* Set the selected item on the panel and refresh it
*/
selectedItemPanel.setSelectedItem(historyItemSelected);
target.addComponent(selectedItemPanel);
}
}
@Override
public void selectItem(IModel item, boolean isSelect) {
super.selectItem(item, isSelect);
/*
* There will only be one item in the collection as this grid
* has setAllowSelectMultiple() set to false.
*/
Collection<IModel> selected = getSelectedItems();
for (IModel model : selected) {
historyItemSelected = (MatrixCheckItemModel) model.getObject();
}
/*
* When selection changes the item panel needs to be refreshed.
*/
AjaxRequestTarget target = AjaxRequestTarget.get();
/*
* Refresh ajax target
*/
if (target != null) {
/*
* Set the selected item on the panel and refresh it
*/
selectedItemPanel.setSelectedItem(historyItemSelected);
target.addComponent(selectedItemPanel);
}
}
};
...
/*
* -------------------------------------------------------------------
* Create a grid of check items that are for the current shift and day
* -------------------------------------------------------------------
*/
currentGrid = new MatrixCheckItemGrid("current",
new MatrixCheckListDataSource(), colorblind) {
private static final long serialVersionUID = 1L;
@Override
public void selectItem(IModel item, boolean isSelect) {
super.selectItem(item, isSelect);
/*
* There will only be one item in the collection as this grid
* has setAllowSelectMultiple() set to false.
*/
Collection<IModel> selected = getSelectedItems();
for (IModel model : selected) {
listItemSelected = (MatrixCheckItemModel) model.getObject();
}
/*
* When selection changes the item panel needs to be refreshed.
*/
AjaxRequestTarget target = AjaxRequestTarget.get();
/*
* If we selected something then we can make the panel visible.
*/
itemSelectedHistoryGrid.setVisible(listItemSelected != null);
selectedItemPanel.setVisible(listItemSelected != null);
/*
* Refresh ajax target
*/
if (target != null) {
/*
* Reset the history data source to the item id and shift report
date
* for the item selected. In this case the shift report date will
* always be the same so we may want to optimize that later.
*/
checkItemHistoryDataSource.setCheckProfileId(listItemSelected
.getMatrixCheckProfile().getId());
checkItemHistoryDataSource.setShiftDate(listItemSelected
.getMatrixShiftReport().getDay());
/*
* We know that we want to change the history item if it currently
* doesn't match the listItemSelected and that the data model will
* choose the day of the one selected by the listItemSelected by
* default; however, unfortunately setting it in the
* checkItemHistoryDataSource doesn't refresh the selectedItem.
*/
if ((historyItemSelected.getId() == null)
||
!historyItemSelected.getMatrixCheckProfile().getId().equals(
listItemSelected.getMatrixCheckProfile().getId())) {
itemSelectedHistoryGrid.resetSelectedItems();
selectedItemPanel.setSelectedItem(listItemSelected);
target.addComponent(itemSelectedHistoryGrid);
target.addComponent(selectedItemPanel);
}
}
}
};
It took me quite a bit of time to figure this out so maybe it will save
someone else some time or stimulate better ideas on how to solve such a
problem.
--
View this message in context: http://www.nabble.com/How-do-I-set-a-selected-item-outside-of-an-inmethod-datagrid--tp24565364p24592148.html
Sent from the Wicket - User mailing list archive at Nabble.com.
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org