i creating variable highlightedsearchkey , replacing searchkey string highlightedsearchkey in name string
this works if search string continuous . doesn't highlight discontinuos string.
eg: string1- boy
string2- boy good. (both , boy should highlighted)
searchkey (1st string) => search string . want highlight words of string in 2nd one.
name (2nd string) => result displayed .
<c:set var="highlightedsearchkey" value=" <label style='background-color:yellow'> ${searchkey} </label> "/> <td>${fn:replace(name, searchkey, highlightedsearchkey)}</td>
please copy , paste example.
<%@ taglib prefix = "c" uri = "http://java.sun.com/jsp/jstl/core" %> <%@ taglib prefix = "fn" uri = "http://java.sun.com/jsp/jstl/functions" %> <c:set var="string1" value="good boy"/> <c:set var="string2" value="this boy good"/> <c:set var="array2" value='${fn:split(string2, " ")}'/> <html> <body> <c:foreach items="${array2}" var="current"> <c:choose> <c:when test="${fn:contains(string1, current)}"> <label style='background-color:yellow'> ${current} </label> </c:when> <c:otherwise> ${current} </c:otherwise> </c:choose> </c:foreach> <br/>another way<br/> <c:set var="string1" value="goods boy"/> <c:set var="array1" value='${fn:split(string1, " ")}'/> <c:set var="string2" value="this boy goodly aboy goods"/> <c:set var="array2" value='${fn:split(string2, " ")}'/> <c:foreach items="${array2}" var="current"> <c:set var="found" value="false"/> <c:foreach items="${array1}" var="curr"> <c:if test="${curr == current}"> <c:set var="found" value="true"/> </c:if> </c:foreach> <c:choose> <c:when test="${found}"> <label style='background-color:yellow'> ${current} </label> </c:when> <c:otherwise> ${current} </c:otherwise> </c:choose> </c:foreach> </body> </html>
Comments
Post a Comment