Accessing a for loop variable inside and if statement - thymeleaf

I have the following code but it gives me an error. Can anyone help?
<span th:each="p,iter : ${pp}">
<span th:text="${p.title}">[default]</span>
<span
th:if="${#lists.size(hh) >= iter.index
th:text="${hh[iter.index].name}">
</span>
...
I get this error message:
Error during execution of processor org.thymeleaf.standard.processor.attr.StandardIfAttrProcessor'
It is because of the list.size(hh) >= iterStat.index. If I don't have the iterStat.index then it is ok. But as this is dynamic I want to use the index of the iteration.
Is this possible?

You have a missing } in th:if="${#lists.size(hh) >= iter.index

Related

thymeleaf - how to test a string with "<p></p>"

I want to test if a variable is not equal to
< p>< /p>
(I put spaces before p and / here otherwise it wont show, in my case there is no space)
I did : <span th:if="${myString!='<p></p>'} ">
But it doesn't work.
What is the right syntax?
Thanks.
You are comparing Strings thus != is not the right condition. User .equals() and just negate it. Replace your code with this:
<span th:if="${!myString.equals('<p></p>')} ">

Nokogirl::HTML.parse trying to find a <li> element by its class

I'm trying to get the contents of elements
The last line fails for invalid syntax
browser=Watir::Browser.new(:chrome)
doc=Nokogiri::HTML.parse(browser.html)
list=doc.css("ul").sort { |x,y| y.css("li").count <=> x.css("li").count }.first
rows=list.css("class=brand-content")
Try rows=list.css(".brand-content").
'css' means it uses CSS selector syntax. li.brand-content would also work.

Parse FacebookPage Using BeautifullSoup

i'm searching for a name in an html page of facebook.
if I take the file html.txt like this:
html = open('html.txt','r').read()
soup = BeautifulSoup(html)
if I search for the name with find it seems to be ok, but if i Try searching with BS i cant find anything..
>>>html.find("Joseph Tan")
98939
>>>html[98700:99000]
'<div class="fwn fcg"><span class="fcg"><span class="fwb"><a class="profileLink" href="https://www.facebook.com/ASD.391" data-ft="{"tn":"l"}" data-hovercard="/ajax/hovercard/user.php?id=123456">Alex Tan</a></span> condivided the photo <a class="profileLink" '
>>> soup.findAll('div',{'class':'fwn fcg'})
[]
>>> soup.findAll('span',{'class':'fwb'})
[]
>>> soup.findAll('a',{'class':'profileLink'})
[]
>>>
Someone can help me? thanks a lot
EDIT: RE-CREATED HTML PAGE
html page
It is working as below:
print soup.find_all('div', class_=['fwn','fcg'])
OUTPUT:
[<div class="uiHeaderActions rfloat _ohf fsm fwn fcg"><a class="_1c1m" href="#" role="button">Segna tutti come già letti</a> · <a accesskey="m" ajaxify="/ajax/messaging/composer.php" href="/messages/new/" id="u_0_8" rel="dialog" role="button">Invia un nuovo messaggio</a></div>, <div class="uiHeaderActions fsm fwn fcg">Segna come già letto · Impostazioni</div>, <div class="fsm fwn fcg"><a ajaxify="/settings/language/language/?uri=https%3A%2F%2Fwww.facebook.com%2Fshares%2Fview%3Fid%3D10152555113196961&source=TOP_LOCALES_DIALOG" href="#" rel="dialog" role="button" title="Usa Facebook in un'altra lingua.">Italiano</a></div>]
According to ==>this link, this is the style of how to search classes and other HTML elements using BS. Please check.
There were two problems.
1. The way you wrote is not matched with the link above I provided. May be you are not using updated version of BS.
2. There are two classes 'fwn' and 'fcg'. So you have to give their names in a list and this is how I got the output.
Same is is applicable for 'span' and 'a' as below:
print soup.find_all('span', class_='jewelCount')
print soup.find_all('a', class_='_awj')
Your given 'span' with class 'fwb' and given 'a' with class 'profileLink' was not found.Because, they are not present in the HTML.
You can check by printing all spans and a's.
Write print soup.find_all('a') and print soup.find_all('span')* to check on your own.
Hope this will help, if not, write again! :)

How to change my view file to get a appropriate output?

I have a controller like this :
def mytask = {
def user = User.findByLogin(params.id)
def mywork = user.schedules.daySchedules
[ mywork : mywork ]
}
Where I'm trying to find all the tasks assigned to a particular user. I have a corresponding view file :
<g:each in="${mywork}" var="tasks">
<div id = "todayswork">
${tasks.task}
</div>
<div id ="Dates">
${tasks.startTime}-
${tasks.endTime}
</div>
<hr/>
</g:each>
Logic works fine, I'm getting the output as I wanted. For example, if I go to http://localhost:8080/scheduleTest/daySchedule/mytask/anto my browser showing all the task for the user anto. But there is a problem in rendering it.
I'm getting the output as :
But I need the output something like this one:
How change my view file to get the appropriate output.
Thanks in advance.
It's hard to tell from your examples, but my guess is you need to be looping over the tasks item, which appears to be a List in a List.
This means change this:
<g:each in="${mywork}" var="tasks">
to this
<g:each in="${mywork[0]}" var="tasks">
// or
<g:each in="${mywork.tasks}" var="tasks">
Again, I'm not exactly sure where the problem is occurring, but one of those will fix it.
The reason you are getting the output is that Groovy will automatically perform a property expansion on a list if the property is not defined on that list. Example:
use(org.codehaus.groovy.runtime.TimeCategory) {
def d1 = 5.minutes.ago
def d2 = 1.week.from.now
assert [d1, d2].time == [d1.time, d2.time]
}
It's the same thing as writing list*.property, and returns a new list containing each property on the original items.

Invalid Variant Operation error

I have an error in my button click, and I can't figure out how to resolve it.
This is my code:
if (ovElements.item(i).name = 'add') and
(ovElements.item(i).type = 'button') and
(ovElements.item(i).Value = ' + ') then
ovElements.item(i).Click;
This is the markup:
<td width="20" align="left"><input class="button" style="width: 30px;"
name="add" value=" + " onclick="addLvl();" type="button"></td>
And it gives this error:
Invalid Variant Operation Error
What did I do wrong?
Just a guess:
ovElements.item(i).Value is probably a Variant. If a variant contains a null value you will get that error when you compare it to a string.
Make sure ovElements.item(i) doesn't contain a null value before comparing it.
You can save "ovElements.item(i)" to a local variable and then split your code into multiple line.
obj = ovElements.item(i);
if obj <> nil then
try
if obj.name = 'add' then
if obj.type = 'button' then
if obj.value = ' + ' then
obj.click;
except
end;
In this way you can see which line causes this problem.
It means an operation on a variant which is executed is invalid. This happens, for example, when a variant containing some text is divided by an integer. Clearly this cannot work, but since the compiler can't check this, it is a runtime error.
Use a temporary variable for the 3 parts in your if statement to see better on which line the error is raised. Then inspect what the values are and what the invalid operation is.

Resources