Note that there are some explanatory texts on larger screens.

plurals
  1. POCellTable with custom Header containing SearchBox and Focus Problem
    primarykey
    data
    text
    <p>I am trying to implement a CellTable with a custom Column Header which displays a SearchBox (simple Textbox) below the normal Column text.<br>The SearchBox should allow the user to filter the CellTable. It should look something like this:</p> <pre><code> |Header 1|Header 2 | |SEARCHBOX|SEARCHBOX| ------------------------------------------------------- | ROW 1 ------------------------------------------------------ | ROW 2 </code></pre> <p>As soon as the user types in a character into the SearchBox a <strong>RangeChangeEvent</strong> is fired which leads to a server requests and the CellTable is updated with the new filtered list.<br><br> Basically everything works fine. However as soon as the CellTable is refreshed the SearchBox <strong>loses its focus</strong> and the user has to click with the mouse into the SearchBox again to type in a new character.<br> <br>This is probably related to the fact that the render method of the custom header and its cell is called after the CellTable refresh.<br>Is there any way how to set the focus back to the SearchBox? I tried to set <strong>tabindex=0</strong> but it didn't help.<br><br></p> <h2>Custom Header Class</h2> <pre><code>public static class SearchHeader extends Header&lt;SearchTerm&gt; { @Override public void render(Context context, SafeHtmlBuilder sb) { super.render(context, sb); } private SearchTerm searchTerm; public SearchHeader(SearchTerm searchTerm,ValueUpdater&lt;SearchTerm&gt; valueUpdater) { super(new SearchCell()); setUpdater(valueUpdater); this.searchTerm = searchTerm; } @Override public SearchTerm getValue() { return searchTerm; } } </code></pre> <h2>Custom Search Cell (used in the custom Header)</h2> <p>The <strong>isChanged</strong> boolean flag is set to <strong>true</strong> when the user types something into the SearchBox and is set back to <strong>false</strong> if the SearchBox loses its focus. I added this flag in order to distinguish which SearchBox gets the focus (in case I use multiple SearchBoxes)</p> <pre><code>public static class SearchCell extends AbstractCell&lt;SearchTerm&gt; { interface Template extends SafeHtmlTemplates { @Template("&lt;div style=\"\"&gt;{0}&lt;/div&gt;") SafeHtml header(String columnName); @Template("&lt;div style=\"\"&gt;&lt;input type=\"text\" value=\"{0}\"/&gt;&lt;/div&gt;") SafeHtml input(String value); } private static Template template; private boolean isChanged = false; public SearchCell() { super("keydown","keyup","change","blur"); if (template == null) { template = GWT.create(Template.class); } } @Override public void render(com.google.gwt.cell.client.Cell.Context context, SearchTerm value, SafeHtmlBuilder sb) { sb.append(template.header(value.getCriteria().toString())); sb.append(template.input(value.getValue())); } @Override public void onBrowserEvent(Context context,Element parent, SearchTerm value,NativeEvent event,ValueUpdater&lt;SearchTerm&gt; valueUpdater) { if (value == null) return; super.onBrowserEvent(context, parent, value, event, valueUpdater); if ("keyup".equals(event.getType())) { isChanged = true; InputElement elem = getInputElement(parent); value.setValue(elem.getValue()); if (valueUpdater != null) valueUpdater.update(value); } else if ("blur".equals(event.getType())) { isChanged =false; } } protected InputElement getInputElement(Element parent) { Element elem = parent.getElementsByTagName("input").getItem(0); assert(elem.getClass() == InputElement.class); return elem.cast(); } } </code></pre> <h2>Init Code for the CellTable</h2> <p><strong>NameColumn</strong> is the implementation of the abstract <strong>Column</strong> class with the appropriate types. It uses a <strong>TextCell</strong> internally. </p> <pre><code>ValueUpdater&lt;SearchTerm&gt; searchUpdater = new ValueUpdater&lt;SearchTerm&gt;() { @Override public void update(AccessionCellTableColumns.SearchTerm value) { // fires a server request to return the new filtered list RangeChangeEvent.fire(table, new Range(table.getPageStart(), table.getPageSize())); } }; table.addColumn(new NameColumn(searchTerm),new SearchHeader(searchTerm,searchUpdater)); </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload