Variable within scala template in play framework - scala

I need to be able to declare variables and after some markup later I
need to reference them. In order to accomplish this, this is
simplified version of my scala template:
#(map1:
java.util.LinkedHashMap[String,java.util.LinkedHashMap[String,Object]])
#import scala.collection.JavaConversions._
#import play.Logger
#for( (key,value) <- map1) {
<div>
#{
val rmap = Foo.someMethod(value)
val baz = rmap.getOrElse("baz", null)
<table border="0" cellpadding="0" cellspacing="0" >
<tbody>
<tr>
<td rowspan="3">
<div class="bar">
#baz
</div>
</td>
</tr>
</tbody>
</table>
}
</div>
}
Is above valid scala template and if not how can I declare baz and
reference it later in the markup?
I am using 1.2.2RC2 and scala 0.9.1

I was curious so did some digging. See https://groups.google.com/forum/#!topic/play-framework/Mo8hl5I0tBQ - there is no way at the moment, but an interesting work-around is shown. Define utils/Let.scala:
package utils
Object Let {
def let[A,B](a:A)(f:A=>B):B = f(a)
}
and then
#import utils.Let._
#let(2+3){ answer =>
#answer <hr> #answer
}
It's a very functional way of handling it, but then, what'd you expect in Scala :)

You can just use a for comprehension:
#for( (key,value) <- map1;
rmap = Foo.someMethod(value);
baz = rmap.getOrElse("baz", null)
) {
<div>
<table border="0" cellpadding="0" cellspacing="0" >
<tbody>
<tr>
<td rowspan="3">
<div class="bar">
#baz
</div>
</td>
</tr>
</tbody>
</table>
</div>
}
... and if you don't have anything you need to loop over, you can just say #for(i <- List(1); <declare variables>){<html here>}

Related

Coffeescript - React callback: sending child header click back to parent

Just starting to learn Coffeescript, and I'm working with React.js. I'm trying to determine which was clicked, and I've been advised not to use data-attributes on each header. I have some ideas how to handle this under the handleHeaderClick function, but I'm not exactly sure how they should be implemented. I'm also thinking about splitting up the ContactsTable component into a ContactsTableHeader component and a ContactsTableRow component, but I should still have the same issue in ContactsTableHeader - determining which header was clicked.
#Application.cjsx
handleHeaderClick: ->
# childComponent.props
# childComponent.refs
# React.findDOMNode(childComponent.refs.firstName)
# React.findDOMNode(childComponent.refs.lastName)
# React.findDOMNode(childComponent.refs.age)
render: ->
<div>
<ContactsTable contactList={#state.contacts} onClick={#handleHeaderClick} />
</div>
#ContactsTable.cjsx
render: ->
if #props.contactList
contactsList = #props.contactList.map (contact) ->
<tr><td>{"#{contact.firstName}"}</td><td>{"#{contact.lastName}"}</td><td>{contact.age}</td></tr>
<table style={tableStyle}>
<thead style={headerStyle} onClick=#props.onClick>
<tr>
<th>FirstName</th>
<th>Last Name</th>
<th>Age</th>
</tr>
</thead>
<tbody>
{contactsList}
</tbody>
</table>
You can do something like this.
#Application.cjsx
handleHeaderClick: (header) ->
#setState({clickedHeader: header})
#do something else
render: ->
<div>
<ContactsTable contactList={#state.contacts} onClick={#handleHeaderClick} />
</div>
#ContactsTable.cjsx
render: ->
if #props.contactList
contactsList = #props.contactList.map (contact) ->
<tr><td>{"#{contact.firstName}"}</td><td>{"#{contact.lastName}"}</td><td>{contact.age}</td></tr>
<table style={tableStyle}>
<thead style={headerStyle}>
<tr>
<th onClick={#props.onClick('FirstName')}>FirstName</th>
<th onClick={#props.onClick('LastName')}>Last Name</th>
<th onClick={#props.onClick('Age')}>Age</th>
</tr>
</thead>
<tbody>
{contactsList}
</tbody>
</table>
Here the clickedHeader is preserved inside the component which is present in Application.cjsx. You can also preserve it inside ContactsTableHeader which should look something like similar.

Selenium Webdriver_Not able to click on link given into column

I'm a beginner and I'm trying to write a Selenium web driver. I'm using eclipse and trying to locate a link available in next column. In each row I have different link. For example in my employee data table I have 2 columns and 10 rows in one page. The first column contains the name of person and second column contains its employee ID (ID is a hyperlink). I'm trying to select the hyperlink of any employee but I'm not able to do it.
I have below the HTML code and sample script.
<span id="37">
<div class="clear"></div>
<div id="FC_Schema.1021.WIP" type="DataCheckingList" name="FC_Schema.1021.WIP">
<input id="baseurl" type="hidden" value="/Web/" name="baseurl">
<input id="hdnDcCnt" type="hidden" value="13" name="hdnDcCnt">
<input id="PageNo" type="hidden" value="1" name="PageNo">
<meta content="width=device-width" name="viewport">
<style type="text/css">
<div id="DataCheckingLoad">
<div id="SupplyChainTabs">
<div id="gbo" class="InnerAlertsTabs">
<table id="one" class="draggable" width="100%">
<thead>
<tbody>
<tr>
<td valign="top" align="center">
<td valign="top">
<td valign="top">
<a onclick="return ButtonClick(this,'TxnyD.Communities.2.1','Org.2000476_Product.THQS|SMS|Questionnaire.WIP_Questionnaire_TxnyD.Communities.2.1_THQS_TxnyD.Operationaloffice.1.2');" href="#f">Axiom
Process LLC</a>
<div class="country-name">
<div class="CompanyInfoContactLinks">
</td>
<td valign="top">
THQS
<div class="clear"> </div>
<a id="Org.2000476_Product.THQS|SMS|Questionnaire.WIP_Questionnaire_TxnyD.Communities.2.1_THQS_TxnyD.Operationaloffice.1.2" onclick="RedirectToQuestionnairePage(id)" href="#">THQS</a>
<div class="clear"> </div>
</td>
In the above code first column contains company name i.e "Axiom
Process LLC" and second column contains company subcription name i.e."THQS". I have to select the THQS link of company "Axiom Process LLC"
Selenium script designed for it as below
public static void main(String[] args) throws Exception {
WebDriver driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.get("http://XXX.XXX.XXX.XXX/Web");
Thread.sleep(1000);
driver.findElement(By.xpath("//input[#id='UserName']")).sendKeys("Gbouser.1");
driver.findElement(By.xpath("//input[#id='Password']")).sendKeys("****");
driver.findElement(By.xpath("//input[#name='Login']")).click();
driver.findElement(By.xpath("//a[contains(text(),'Task')]")).click();
driver.findElement(By.xpath("//a[contains(text(),'Data Checking')]")).click();
driver.findElement(By.partialLinkText("Axiom")).findElement(By.xpath("//a[contains(text(),'THQS')]")).click();
}
So basically these are dynamic tables and each time it will have different records. How can select the link available in column?
I also tried below script as well:
driver.findElement(By.name("*[id^='CLS'][id$='Demolition Limited']")).findElement(By.xpath("//a[contains(text(),'THQS')])[3]")).click();
welcome to Stackoverflow. Before answering your question, just a few pointers. Think that you had a downvote 'cause you didn't format your question well. Otherwise a perfectly legal question, and you would receive help much earlier.
Secondly, don't ever put the real Web site address, username and password :). I was able to login, and understand exactly what you need, but a malicious guy could do bad wonders there.
To answer your question, instead of the line that is causing you problems, try this
Thread.sleep(3000);
TablePageObject tablePageObject = PageFactory.initElements(driver, TablePageObject.class);
tablePageObject.clickLink("Axiom");
Where TablePageObject is
public class TablePageObject {
private WebDriver driver;
#FindBy(css = "table tr")
private List<WebElement> allTableRows; // find all the rows of the table
public TablePageObject(WebDriver driver) {
this.driver = driver;
}
public void clickLink(String companyName) {
for(WebElement row : allTableRows) {
List<WebElement> links = row.findElements(By.cssSelector("a"));
// the first link by row is the company name, the second is link to be clicked
if (links.get(0).getText().contains(companyName)) {
links.get(3).click();
}
}
}
}
An explanation, TablePageObject class will be your programmatic handle for the dynamic table you're trying to control. It follows a page factory pattern of selenium, which I heartly recommend, and you can learn more here https://code.google.com/p/selenium/wiki/PageFactory
It basically keeps a collection of rows of your table, which more or less looks like:
<tr>
<td valign="top" align="center">
// irelevant
</td>
<td valign="top">
// irelevant
</td>
<td valign="top">
<a>Axiom Process LLC</a>
<a>company details</a>
<a>web details</a>
</td>
<td valign="top">
<a>THQS</a>
</td>
<td valign="top" align="center" style="display: none">
// irelevant
</td>
<td valign="top">
//irelevant
</td>
<td valign="top">
// irelevant
</td>
<td valign="top">
</td>
<td>
</td>
</tr>
You collect all the links from the table, check the company name against the first link, and if matched, click the fourth link,
Hope it helps, best,
by the way, I've masked your password in the question

Lift - Binding list of values to template

Here is my template -
<div class="lift:Admin.showInvitees">
<tr class="success"> <!-- "success" "error" -->
<td><invitee:name></invitee:name></td>
<td><invitee:description></invitee:description></td>
<td><invitee:status></invitee:status></td>
<td></td>
</tr>
</div>
and here is the snippet -
class Admin {
def showInvitees(in: NodeSeq): NodeSeq = {
val invitees = Invitation.allInvitations
invitees.flatMap{invitee => bind("invitee", in, "name" -> invitee.name.is, "status" -> invitee.status.is, "description" -> invitee.description.is)}
}
}
I checked this question and used the solution. But it's not working for me. Values are not getting bound at all. This is what I get as output -
<tr class="success">
<td><invitee:name></invitee:name></td>
<td><invitee:description></invitee:description></td>
<td><invitee:status></invitee:status></td>
</tr>
Need help binding a list of values to template.
After the first answer I tried this as well -
def showInvitees(in: NodeSeq): NodeSeq = {
val invitees = Invitation.allInvitations
(".invitees" #> invitees.map{ inv =>
".invitee-name" #> inv.name.is &
".invitee-description" #> inv.description.is &
".invitee-status" #> inv.status.is
})(in)
}
Even this is not working-
Template -
<div class="lift:Admin.showInvitees">
<tr class="invitees success"> <!-- "success" "error" -->
<td><span class="invitee-name"></span></td>
<td><span class="invitee-description"></span></td>
<td><span class="invitee-status"></span> </td>
<td></td>
</tr>
</div>
This is the old syntax for binding values. Try the new one:
// snippet
class Admin {
def showInvitees = {
case class Invitee(name: String, descr: String, status: String)
val invitees = List(Invitee("Alex", "for cookies", "ok"), Invitee("Bob", "for beer", "kicked out"))
".invitee" #> invitees.map { i =>
".invitee-name" #> i.name.is &
".invitee-descr" #> i.description.is &
".invitee-status" #> i.status.is
}
}
}
// html
<div class="Admin.showInvitees">
<table>
<tr class="invitee success">
<td><span class="invitee-name"></span></td>
<td><span class="invitee-descr"></span></td>
<td><span class="invitee-status"></span></td>
</tr>
</table>
</div>
// result
<table>
<tbody><tr class="invitee success">
<td>Alex</td>
<td>for cookies</td>
<td>ok</td>
</tr><tr class="invitee success">
<td>Bob</td>
<td>for beer</td>
<td>kicked out</td>
</tr>
</tbody></table>

Scala Lift: how to get ajaxRadio parameters

I have ajaxRadio ineach row of a table.
when this Radio is selected, I want scala function to get two parameters:
1st one is the selected radio, 2nd one is the data key of the row.
my source is as follows:
val radioList = List("wideArea", "oldArea","newArea")
def liftForm(xhtml: NodeSeq): NodeSeq = {
var areaList = newOrderList.map(values =>{
<li id={values(1)} >
<table>
<thead></thead>
<tbody>
<tr class = "real">
<td class="listImage">
<img class="areaImage" src={values(6)}/>
</td>
<td style="vertical-align:top;">
<tr><p class="areaName">{values(4)}</p></tr>
<tr><p class="areaComment">{values(5)}</p></tr>
</td>
<td class="listCheck" >
{ajaxButton(S.?("delete"), () => doDelete(values(1)), "class" -> "button delete")}
</td>
</tr>
<tr>
<td></td>
<td>
{
val it = ajaxRadio[String](radioList,Box.legacyNullTest(values(2)),doRadioChange _).toForm.grouped(4)
for(i <- it)yield(<tr>{i.flatMap(y => <td> {y} </td>)}</tr>)
}
</td>
</tr>
</tbody>
</table>
</li>})
bind("list",xhtml,"areaList" -> <ul>{areaList}</ul>)
By doRadioChange _, I can only get selected radio. How can I get the the 2nd parameter: data key of the row. ie values(1)?
Your functions can close over all of the local state at that point in the function. You should be able to replace doRadioChange _ with:
doRadioChange(_, values(1))
Assuming, of course, that doRadioChange accepts the proper parameters.

HTML::TableExtract: applying the right attribs to specify the attributes of interest

I tried to run the following Perl script on the HTML further below. My problem is how to define the correct hash reference, with attribs that specify attributes of interest within my HTML <table> tag itself.
#!/usr/bin/perl
use strict; use warnings;
use HTML::TableExtract;
use YAML;
my $table = HTML::TableExtract->new(keep_html=>0, depth => 1, count => 1, br_translate => 0 );
$table->parse($html);
foreach my $row ($table->rows)
sub cleanup {
for ( #_ ) {
s/\s+//;
s/[\xa0 ]+\z//;
s/\s+/ /g;
}
}
{ print join("\t", #$row), "\n"; }
I want to apply this code on the HTML-document you see further below.
My first approach is to do this with the columns method. But i am not able to figure out how to use the columns method on the below HTML-file: My intuition makes me think it should be something like the following (but my intuition is wrong):
foreach my $column ($table->columns) {
print join("\t", #$column), "\n";
}
The HTML::TableExtract documentation doesn't shed much light (for me anyway).
I can see in the code of the module that the columns method belongs to HTML::TableExtract::Table, but I can't figure out how to use it. I appreciate any help.
Background:
I try to get the table extracted and I have a very very small document of tables that i want to parse with the HTML::TableExtract module I am trying to search for keywords in the HTML - so that i can take them for the attribs I have to print only the necessary data.
I tried going CPAN but could not really find how to search through it for particular keywords. One way to do it would be HTML::TableExtract - the other way would be to parse with HTML::TokeParser I have very little experience with HTML::TokeParser.
Well - one or the other way i need to do this parsing: I want to output the result of the parsed tables into some .text - or even better store it into a database. The problem here is I cant find anyway to search through the resulting parsed table and get necessary data.
The HTML
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<meta name="GENERATOR" content="Microsoft FrontPage 3.0">
<link rel="stylesheet" href="jspsrc/css/bp_style.css" type="text/css">
<title>Weitere Schulinformationen</title>
</head>
<body class="bodyclass">
<div style="text-align:center;"><center>
<!-- <fieldset><legend> general information </legend>
-->
<br/>
<table border="1" cellspacing="0" bordercolordark="white" bordercolorlight="black" width="80%" class='bp_result_tab_info'>
<!-- <table border="0" cellspacing="0" bordercolordark="white" bordercolorlight="black" width="80%" class='bp_search_info'>
-->
<tr>
<td width="100%" colspan="2" class="ldstabTitel"><strong>data_one </strong></td>
</tr>
<tr>
<td width="27%"><strong>data_two</strong></td>
<td width="73%"> 116439
</td>
</tr>
<tr>
<td width="27%"><strong>official_description</strong></td>
<td width="73%">the name </td>
</tr>
<tr>
<td width="27%"><strong>name of the street</strong></td>
<td width="73%">champs elysee</td>
</tr>
<tr>
<td width="27%"><strong>number and town</strong></td>
<td width="73%"> 75000 paris </td>
</tr>
<tr>
<td width="27%"><strong>telefon</strong></td>
<td width="73%"> 000241 49321
</td>
</tr>
<tr>
<td width="27%"><strong>fax</strong></td>
<td width="73%"> 000241 4093287
</td>
</tr>
<tr>
<td width="27%"><strong>e-mail-adresse</strong></td>
<td width="73%"> <a href=mailto:1111116439#my_domain.org>1222216439#site.org</a>
</td>
</tr>
<tr>
<td width="27%"><strong>internet-site</strong></td>
<td width="73%"> <a href=http://www.thesite.org>http://www.thesite.org</td>
</tr>
<!--
<tr>
<td width="27%"> </td>
<td width="73%" align="right"><a href="schule_aeinfo.php?SNR=<? print $SCHULNR ?>" target="_blank">
[Schuldaten ändern] </a>
</tr>
</td> -->
<tr>
<td width="27%"> </td>
<td width="73%">the department</td>
</tr>
<tr>
<td width="100%" colspan=2><strong> </strong></td>
</tr>
<tr>
<td width="27%"><strong>number of indidviduals</strong></td>
<td width="73%"> 192</td>
<tr>
<td width="100%" colspan=2><strong> </strong></td>
</tr>
<!-- if (!fsp.isEmpty()){
ztext = " ";
int i = 0;
Iterator it = fsp.iterator();
while (it.hasNext()){
String[] zwert = new String[2];
zwert = (String[])it.next();
if (i==0){
if (zwert[1].equals("0")){
ztext = ztext+zwert[0];
}else{
ztext = ztext+zwert[0]+" mit "+zwert[1];
if (zwert[1].equals("1")){
ztext = ztext+" Schüler";
}else{
ztext = ztext+" Schülern";
}
}
i++;
}else{
if (zwert[1].equals("0")){
ztext = ztext+"<br> "+zwert[0];
}else{
ztext = ztext+"<br> "+zwert[0]+" mit "+zwert[1];
if (zwert[1].equals("1")){
ztext = ztext+" Schüler";
}else{
ztext = ztext+" Schülern";
}
}
}
}
-->
</table>
<!-- </fieldset> -->
<br>
</body>
</html>
Thanks for any and all help.
You need to provide something that uniquely identifies the table in question. This can be the content of its headers or the HTML attributes. In this case, there is only one table in the document, so you don't even need to do that. But, if I were to provide anything to the constructor, I would provide the class of the table.
Also, I do not think you want the columns of the table. The first column of this table consists of labels and the second column consists of values. To get the labels and values at the same time, you should process the table row-by-row.
#!/usr/bin/perl
use strict; use warnings;
use HTML::TableExtract;
use YAML;
my $te = HTML::TableExtract->new(
attribs => { class => 'bp_result_tab_info' },
);
$te->parse_file('t.html');
for my $table ( $te->tables ) {
print Dump $table->columns;
}
Output:
---
- 'data_one '
- data_two
- official_description
- name of the street
- number and town
- telefon
- fax
- e-mail-adresse
- internet-site
- á
- á
- number of indidviduals
- á
---
- ~
- "á116439\r\n "
- 'the name '
- champs elysee
- ' 75000 paris '
- "á000241 49321\r\n"
- "á000241 4093287\r\n"
- "á1222216439#site.org\r\n"
- áhttp://www.thesite.org
- the department
- ~
- á192
- ~
Finally, a word of advice: It is clear that you do not have much of an understanding of Perl (or HTML for that matter). It would be better for you to try to learn some of the basics first. This way, all you are doing is incorrectly copying and pasting code from one answer into another and not learning anything.