Re: Highlight some words in the markup content
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 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 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(" > class=\"search\">$0"); > > } > > } > > 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 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 > >>> wrote: > >>> > >>> Thanks. But how the filter should know about the request which > contains > the > information about what to highlight? > > -- > Cheers, > >>>
Re: Highlight some words in the markup content
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 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(" class=\"search\">$0"); > } > } > 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 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 >>> 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 > 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, "" + searchword + >> >>> ""); >>> >>> label.setEscapeModelStrings(false); >>> >>> Martijn >>> >>> On Wed, Oct 15, 2008 at 3:56 PM,
Re: Highlight some words in the markup content
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("$0"); } } 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, "" + searchword + > >> ""); >> >> 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-mai
Re: Highlight some words in the markup content
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, "" + searchword + ""); 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]
Re: Highlight some words in the markup content
If you are displaying search results dynamically, and it's not part of your static markup, you can also do something like this: class HighlightingModel extends LoadableDetachableModel { private transient IModel text; private transient IModel searchWord; HighlightingModel(IModel text, IModel searchWord) { this.text = text; this.searchWord = searchWord; } @Override protected String load() { return text.getObject().replaceAll(searchWord.getObject(), "" + searchWord.getObject() + ""); } @Override protected void onDetach() { super.onDetach(); text = null; searchWord = null; } } IModel resultDescription = new HighlightingModel(new PropertyModel(someObject, "someProperty"), new PropertyModel(searchQuery, "searchTerm")); -- Jeremy Thomerson http://www.wickettraining.com On Wed, Oct 15, 2008 at 12:07 PM, 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, "" + searchword + ""); 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] > >
Re: Highlight some words in the markup content
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, "" + searchword + ""); 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] > >
Re: Highlight some words in the markup content
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, "" + searchword + ""); 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]
Re: Highlight some words in the markup content
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, "" + searchword + >> ""); >> >> 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] > >
Re: Highlight some words in the markup content
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, "" + searchword + ""); 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]
Re: Highlight some words in the markup content
tekst.replaceAll(searchword, "" + searchword + ""); 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] > > -- Become a Wicket expert, learn from the best: http://wicketinaction.com Apache Wicket 1.3.4 is released Get it now: http://www.apache.org/dyn/closer.cgi/wicket/1.3. - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Highlight some words in the markup content
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]