Perhaps you could use a jQuery plugin to do this? Something like [1] - although this is just the first Google result that came up for me. I have not tested this.
[1] - http://johannburkard.de/blog/programming/javascript/highlight-javascript-text-higlighting-jquery-plugin.html -- Jeremy Thomerson http://www.wickettraining.com On Mon, Oct 19, 2009 at 3:12 PM, Jim Pinkham <[email protected]> wrote: > I'd be interested to know if anyone else is using this technique for search > highlighting with a DataTable. > > http://pastebin.com/m5446797d > > The data in my grid may contain HTML, so that makes it considerably more > complicated (my first attempt didn't address this problem). I'd love to > know if someone else has a better/simpler way, although this does appear to > work fine, it took a day to come up with. > > In general, I kind of like the idea of separating this concern from the > model, whose purpose is just to get the data, and think of this search > highlighting as more of a decorator that applies to the entire grid rather > than any particular column, since I've got several PropertyColumn > subclasses > (LinkPropertyColumn, ImagePropertyColumn, NonEscapedHTMLPropertyColumn, > ...etc). > > Hope this helps someone else. > > Thanks, > -- Jim. > > On Fri, Oct 24, 2008 at 1:06 PM, Jim Pinkham <[email protected]> wrote: > > > I've got a few embellishments to that idea that others might enjoy. > These > > include the ability to use on non-string properties, and caching the last > > compiled pattern (maybe uses a bit less CPU than replaceAll each time). > > > > protected static class HighlightingModel extends > > LoadableDetachableModel { > > private static final long serialVersionUID = 1L; > > > > private transient IModel text; > > private transient IModel searchWord; > > private transient Pattern lastp; > > private transient String lastSearch; > > > > HighlightingModel(IModel text, IModel searchWord) { > > this.text = text; > > this.searchWord = searchWord; > > } > > > > @Override > > protected String load() { > > String srch = (String)searchWord.getObject(); > > Object object = text.getObject(); > > String txt = ""; > > if (object != null) { > > IConverter converter = > > Application.get().getConverterLocator().getConverter(object.getClass()); > > txt = converter.convertToString(object, > > Session.get().getLocale()); > > } > > if (srch != null) { > > Pattern p = null; > > if (lastp != null && srch.equals(lastSearch) ) > > p = lastp; > > else { > > try { > > p = Pattern.compile(srch, > > Pattern.CASE_INSENSITIVE); > > } catch (PatternSyntaxException e) { > > if (e.getIndex()>-1) { > > srch = srch.substring(0, e.getIndex()); > > try { > > p = Pattern.compile(srch, > > Pattern.CASE_INSENSITIVE); > > } catch (Exception e2) { > > srch = null; > > } > > } else > > srch = null; > > } > > } > > if (p != null) { > > Matcher m = p.matcher(txt); > > return m.replaceFirst("<span > > class=\"search\">$0</span>"); > > } > > } > > return txt; > > } > > > > @Override > > protected void onDetach() { > > super.onDetach(); > > text = null; > > searchWord = null; > > } > > } > > > > -- Jim Pinkham > > > > > > On Thu, Oct 16, 2008 at 3:24 PM, Thomas Singer <[email protected]> wrote: > > > >> Thanks all. The first tests work fine now, still need to handle the > >> ignore-case searching... > >> > >> Is there anybody else interested in this feature? Then I will try to > >> prepare the IResponseFilter implementation for public > viewing/integration in > >> Wicket. > >> > >> as far as i know the response filter runs within the request cycle and > >>> request cycle has metadata facility, so.... > >>> > >> > >> We already make use of RequestCycle.get(), but if you don't work on > Wicket > >> the full day like you, then one forgets such secrets. > >> > >> > >> -- > >> Cheers, > >> Tom > >> > >> > >> Igor Vaynberg wrote: > >> > >>> as far as i know the response filter runs within the request cycle and > >>> request cycle has metadata facility, so.... > >>> > >>> -igor > >>> > >>> On Wed, Oct 15, 2008 at 10:07 AM, Thomas Singer <[email protected]> > >>> wrote: > >>> > >>> Thanks. But how the filter should know about the request which > contains > >>>> the > >>>> information about what to highlight? > >>>> > >>>> -- > >>>> Cheers, > >>>> Tom > >>>> > >>>> > >>>> > >>>> Igor Vaynberg wrote: > >>>> > >>>> iresponsefilter > >>>>> > >>>>> -igor > >>>>> > >>>>> On Wed, Oct 15, 2008 at 8:22 AM, Thomas Singer <[email protected]> > >>>>> wrote: > >>>>> > >>>>> OK, this looks trivial, but were should I place this code to replace > >>>>> all > >>>>> > >>>>>> content, no matter whether it comes from a page template, border or > >>>>>> fragment? > >>>>>> > >>>>>> -- > >>>>>> Cheers, > >>>>>> Tom > >>>>>> > >>>>>> > >>>>>> > >>>>>> Martijn Dashorst wrote: > >>>>>> > >>>>>> tekst.replaceAll(searchword, "<span class="search">" + searchword + > >>>>>> > >>>>>>> "</span>"); > >>>>>>> > >>>>>>> label.setEscapeModelStrings(false); > >>>>>>> > >>>>>>> Martijn > >>>>>>> > >>>>>>> On Wed, Oct 15, 2008 at 3:56 PM, Thomas Singer <[email protected]> > >>>>>>> wrote: > >>>>>>> > >>>>>>> We are using Wicket as base for our website (www.syntevo.com). We > >>>>>>> have > >>>>>>> > >>>>>>>> a > >>>>>>>> simple search feature on the website, but want to extend it like > it > >>>>>>>> is > >>>>>>>> known > >>>>>>>> to work in forums: the searched words should be highlighted. > >>>>>>>> > >>>>>>>> Does someone already has implemented such a feature to surround > >>>>>>>> plain > >>>>>>>> words > >>>>>>>> in the markup by a special tag? > >>>>>>>> > >>>>>>>> -- > >>>>>>>> Cheers, > >>>>>>>> Tom > >>>>>>>> > >>>>>>>> > >>>>>>>> > --------------------------------------------------------------------- > >>>>>>>> To unsubscribe, e-mail: [email protected] > >>>>>>>> For additional commands, e-mail: [email protected] > >>>>>>>> > >>>>>>>> > >>>>>>>> > >>>>>>>> > >>>>>>>> > --------------------------------------------------------------------- > >>>>>>> > >>>>>> To unsubscribe, e-mail: [email protected] > >>>>>> For additional commands, e-mail: [email protected] > >>>>>> > >>>>>> > >>>>>> > >>>>>> > --------------------------------------------------------------------- > >>>> To unsubscribe, e-mail: [email protected] > >>>> For additional commands, e-mail: [email protected] > >>>> > >>>> > >>>> > >>> > >> --------------------------------------------------------------------- > >> To unsubscribe, e-mail: [email protected] > >> For additional commands, e-mail: [email protected] > >> > >> > > >
