Thursday, May 6, 2010

Groovy wrapping of long table td text with no blanks

Subtitle: Groovy's a Wrap

Just today I had a client -- who had just tested a fairly complex GSP -- ask to have text in a table data element wrap. He had tested it with a long string of characters with no spaces. He wanted:

asldfjas;fjasodfsaodjfsaodfdsadfa;ljlj

To show as:

asldfjas;fj
asodfsaod
jfsaodfdsa
dfa;ljlj

And not just the first 12 characters that fit in the td.


Quick solution: Hey buddy, use spaces and the browser will wrap for you.
Groovy solution: Insert thespaces for them.

I took the Groovy option. What I did was extend the String class in BootStrap to have a wrapAt method:


class BootStrap {

def init = { servletContext ->
String.metaClass.wrapAt = {width ->
matcher = (delegate =~ /\w{1,$width}/);
def that = ''
matcher.each {
that += "${it.trim()} "
}
return that
}
}
def destroy = {
}
}


Then, in my GSP (where the em width was 12) I added:

<td>${option.value.wrapAt(12)}</td>


I tested the code with the following:


String.metaClass.wrapAt = {width ->
println delegate
matcher = (delegate =~ /\w{1,$width}/);
def that = ''
matcher.each {
that += "${it.trim()} "
}
return that
}

def str = 'one two thetimehascomeforalgoodmentocometotheaidoftheircountry'
assert "one two thetimehas comeforalg oodmentoco metotheaid oftheircou ntry " == str.wrapAt(10)


Sure, not a perfect solution, but all the text the user keyed is viewable on the page (and the optional PDF that is generated from that page.) It looks like this:

one two
thetimehasco
meforalgoodm
entocometoth
eaidoftheirc
ountry

Also, it gave me a use for Groovy meta programming

4 comments:

Anja021netteC_021Restrepo1 said...

凡是遇到困擾的問題,不要把它當作可怕的,討厭的,無奈的遭遇,而要把它當作歷練、訓練和幫助。.............................................

雅伯 said...

北台灣視訊aio34c正妹 69成人網 080視訊聊天室iww 線上免費看成人片 百分百成人圖片 辣妹妹影音視訊聊天室 視訊交友551281 kk 視訊聊天室 免費a片 sex-情色視訊 夜未眠成人影城 視訊聊天網 aaa片免費看短片 正妹交友go uouo 視訊聊天交友 hi5 tv免費影片 av女優王國 546視訊聊天室 免費影音視訊聊天室 ut 視訊聊天室 sex story,辣妹影片直播 hilive成人情色網 線上a長片 美眉脫內衣影片 sex888入口aio甜心寶貝直播貼片 彩虹免費av影片 亞洲東洋視訊辣妹影片 視訊交友網fm358 love104 影音城 qk999成人論壇 免費視訊gogo 拓網學生族視訊777美女 免費成人影片sex,av女優 a 免費影片觀賞 免費視訊topchurch 免費線上 aa 片試看85CC 高雄援交本土自拍天堂 成人視訊elove 臺灣情色網hilive tv 546 視訊聊天室 線上免費av影片 視訊ggoo xvediox松島楓免費影片 免費聊天室 aa影音視訊聊天室 一對多視訊免費視訊辣妹 j 我愛 78 論壇 免費 a 片 兼職援交 免費a自拍偷拍g世代論壇

健豪 said...

留言支持好作品~加油!期待進步和更新 ....................................................

SeymourZ_R翊芷 said...

很棒的分享~如有打擾之處,敬請原諒!........................................