Using flags to remove repeated items in a table - velocity

I am currently working on an email project using Apache Velocity. In the email, I have a table where each row is a persons name followed by a value. Each person can have multiple rows, but the name should only display the first time it appears in the table. I am currently using flags to toggle whether the name has been shown or not. My question is if there is a better way to accomplish this than by using flags. Here is my current code, a bit watered down:
<table>
<tr>
<td>Name</td>
<td>Value</td>
</tr>
#foreach( $person in $view.people )
#set($nameDisplayed = false)
#foreach($personInfo in $person.infos)
#if($personInfo.value1 > 0)
<tr>
<td>#if(!$nameDisplayed) $person.name #end </td>
<td>1</td>
</tr>
#set($nameDisplayed = true)
#end
#end
#if($person.value2 > 0)
<tr>
<td>#if(!$nameDisplayed) $person.name #end </td>
<td>2</td>
</tr>
#set($nameDisplayed = true)
#end
#if($person.value3 > 0)
<tr>
<td>#if(!$nameDisplayed) $person.name #end </td>
<td>3</td>
</tr>
#set($nameDisplayed = true)
#end
#end
</table>
Here is a sample of possible outcomes:
Name Value
Rick 1
2
Morty 1
1
3
Filipe 2
3
Jose 3
Thanks for any suggestions!

I think $velocityCount will do the trick. Like:
#foreach( $person in $view.people )
#foreach($personInfo in $person.infos)
<tr>
<td>
#if($velocityCount == 1)
$person.name
#end
</td>
<td>[value]</td
</td>
#end
#end
This example most probably won't work as I can't test it and some fiddling has to be done, but it should illustrate the idea alright.
Every for-each-loop has its own $velocityCount. So if necessary you can place another one in the outer loop and do some calculation (I'm saying that, because I couldn't figure out how the values are made and you don't read'em from an object).

Related

how to apply filter on JQuery DataTable each columns? [duplicate]

I'm trying to filter table rows in an intelligent way (as opposed to just tons of code that get the job done eventually) but a rather dry of inspiration.
I have 5 columns in my table. At the top of each there is either a dropdown or a textbox with which the user may filter the table data (basically hide the rows that don't apply)
There are plenty of table filtering plugins for jQuery but none that work quite like this, and thats the complicated part :|
Here is a basic filter example http://jsfiddle.net/urf6P/3/
It uses the jquery selector :contains('some text') and :not(:contains('some text')) to decide if each row should be shown or hidden. This might get you going in a direction.
EDITED to include the HTML and javascript from the jsfiddle:
$(function() {
$('#filter1').change(function() {
$("#table td.col1:contains('" + $(this).val() + "')").parent().show();
$("#table td.col1:not(:contains('" + $(this).val() + "'))").parent().hide();
});
});
Slightly enhancing the accepted solution posted by Jeff Treuting, filtering capability can be extended to make it case insensitive. I take no credit for the original solution or even the enhancement. The idea of enhancement was lifted from a solution posted on a different SO post offered by Highway of Life.
Here it goes:
// Define a custom selector icontains instead of overriding the existing expression contains
// A global js asset file will be a good place to put this code
$.expr[':'].icontains = function(a, i, m) {
return $(a).text().toUpperCase()
.indexOf(m[3].toUpperCase()) >= 0;
};
// Now perform the filtering as suggested by #jeff
$(function() {
$('#filter1').on('keyup', function() { // changed 'change' event to 'keyup'. Add a delay if you prefer
$("#table td.col1:icontains('" + $(this).val() + "')").parent().show(); // Use our new selector icontains
$("#table td.col1:not(:icontains('" + $(this).val() + "'))").parent().hide(); // Use our new selector icontains
});
});
This may not be the best way to do it, and I'm not sure about the performance, but an option would be to tag each column (in each row) with an id starting with a column identifier and then a unique number like a record identifier.
For example, if you had a column Produce Name, and the record ID was 763, I would do something like the following:
​​<table id="table1">
<thead>
<tr>
<th>Artist</th>
<th>Album</th>
<th>Genre</th>
<th>Price</th>
</tr>
</thead>
<tbody>
<tr>
<td id="artist-127">Red Hot Chili Peppers</td>
<td id="album-195">Californication</td>
<td id="genre-1">Rock</td>
<td id="price-195">$8.99</td>
</tr>
<tr>
<td id="artist-59">Santana</td>
<td id="album-198">Santana Live</td>
<td id="genre-1">Rock</td>
<td id="price-198">$8.99</td>
</tr>
<tr>
<td id="artist-120">Pink Floyd</td>
<td id="album-183">Dark Side Of The Moon</td>
<td id="genre-1">Rock</td>
<td id="price-183">$8.99</td>
</tr>
</tbody>
</table>
You could then use jQuery to filter based on the start of the id.
For example, if you wanted to filter by the Artist column:
var regex = /Hot/;
$('#table1').find('tbody').find('[id^=artist]').each(function() {
if (!regex.test(this.innerHTML)) {
this.parentNode.style.backgroundColor = '#ff0000';
}
});
You can filter specific column by just adding children[column number] to JQuery filter. Normally, JQuery looks for the keyword from all the columns in every row. If we wanted to filter only ColumnB on below table, we need to add childern[1] to filter as in the script below. IndexOf value -1 means search couldn't match. Anything above -1 will make the whole row visible.
ColumnA | ColumnB | ColumnC
John Doe 1968
Jane Doe 1975
Mike Nike 1990
$("#myInput").on("change", function () {
var value = $(this).val().toLowerCase();
$("#myTable tbody tr").filter(function () {
$(this).toggle($(this.children[1]).text().toLowerCase().indexOf(value) > -1)
});
});
step:1 write the following in .html file
<input type="text" id="myInput" onkeyup="myFunction()" placeholder="Search for names..">
<table id="myTable">
<tr class="header">
<th style="width:60%;">Name</th>
<th style="width:40%;">Country</th>
</tr>
<tr>
<td>Alfreds Futterkiste</td>
<td>Germany</td>
</tr>
<tr>
<td>Berglunds snabbkop</td>
<td>Sweden</td>
</tr>
<tr>
<td>Island Trading</td>
<td>UK</td>
</tr>
<tr>
<td>Koniglich Essen</td>
<td>Germany</td>
</tr>
</table>
step:2 write the following in .js file
function myFunction() {
// Declare variables
var input, filter, table, tr, td, i;
input = document.getElementById("myInput");
filter = input.value.toUpperCase();
table = document.getElementById("myTable");
tr = table.getElementsByTagName("tr");
// Loop through all table rows, and hide those who don't match the search query
for (i = 0; i < tr.length; i++) {
td = tr[i].getElementsByTagName("td")[0];
if (td) {
if (td.innerHTML.toUpperCase().indexOf(filter) > -1) {
tr[i].style.display = "";
} else {
tr[i].style.display = "none";
}
}
}
}

identify the content of xml using header value in sql

DECLARE #xml xml=' <?xml version="1.0" encoding="UTF-8"?><text>
<table>
<thead>
<tr>
<th>a</th>
<th>b</th>
<th>c</th>
<th>d</th>
<th>e</th>
<th>f</th>
</tr>
</thead>
<tbody>
<tr>
<td>testa1</td>
<td>testb1</td>
<td>testc1</td>
<td>testd1</td>
<td>teste1</td>
<td>testf1</td>
</tr>
<tr>
<td>testa1</td>
<td>testb1</td>
<td>testc1</td>
<td>testd1</td>
<td>teste1</td>
<td>testf1</td>
</tr>
</tbody>
</table>
</text>'
SELECT
T.c.value('(tbody/tr/td)[1]','VARCHAR(100)') AS a
FROM
#xml.nodes('text/table[thead[tr[th="b"]]]') AS t(c)
This query selects only the first body contents from xml.
Here I want to select only the values under header b
Modifying the xpath as follows will do it:
SELECT T.c.value('/text/table/tbody/tr/td[position()=2]','VARCHAR(100)') AS a
Explanation:
Selects the second td element among a list of siblings.
Note:
Combine that with this SO answer to get:
SELECT T.c.value('/text/table/tbody/tr/td[position() = count(/text/table/tbody/tr/th[text() = "b"]/preceding-sibling::*)+1]','VARCHAR(100)') AS a

Selenium compare stored Text with NOT or != operator

i'm trying to compare the content of two variables in order to return if variable1 != variable2.
I found some different examples, how to compare variables, but not how to use this with an operator.
First of all, i store text, wait 5 minutes and store text again. If the text is different, so the testis passed.
<tr>
<td>storeText</td>
<td>id=newsdiv_1</td>
<td>newsBox1</td>
</tr>
<!--wait 6 min for ajax update-->
<tr>
<td>pause</td>
<td>360000</td>
<td></td>
</tr>
<tr>
<td>storeText</td>
<td>id=newsdiv_1</td>
<td>newsBox2</td>
</tr>
<tr>
<td>verifyExpression</td>
<td>${newsBox1}</td>
<td>regexp=!(${newsBox2})</td>
</tr>
verifyExpressionworks perfect in comparing if 2 variables are equal.
But the last test should work like this: if newsBox1!=newsBox2 than return true
You can do a verifyNotExpression, which is basically a not of the verifyExpression:
<tr>
<td>verifyNotExpression</td>
<td>${newsBox1}</td>
<td>${newsBox2}</td>
</tr>

PHPTAL and specific table

I have to create specific table in PHPTAL.
I have array like that:
$tab = array('item1', 'item2', 'item3', 'item4');
Final table should be look like that:
<table>
<tr>
<td>Item1</td>
<td>Item2</td>
</tr>
<tr>
<td>Item3</td>
<td>Item4</td>
</tr>
</table>
So I was trying use tal:condition width "repeat/item/odd" and "repeat/item/even" to fit < tr > tag in right place, but it not working that I want to.
Have you any ideas?
<tr tal:repeat="row php:array_chunk(tab, 2)">

How do I retrieve the text in a table column using Selenium RC?

I have a table that looks like the following:
<table class="theClass">
<tr>
<td class="anotherClass"><strong>Label1:</strong></td>
<td colspan="3">Value1a<br/>Value1b<br/>Value1c</td>
</tr>
<tr>
<td class="anotherClass"><strong>Label2:</strong></td>
<td colspan="3">Value2a<br/>Value2b<br/>Value2c</td>
</tr>
<tr>
<td class="anotherClass"><strong>Label3:</strong></td>
<td colspan="3">Value3a<br/>Value3b<br/>Value3c</td>
</tr>
</table>
How can I use Selenium RC to retrieve Value1a, Value1b, and Value1c ? Can I use selenium.getText(...) or storeText(...)? If so, what is the proper xpath I should use? Please assume that the table cannot be changed. Thanks!
it would be
string value1 = selenium.getTable("table.1.2");
string value2 = selenium.getTable("table.2.2");
and so on.
The help text for getTable is
getTable(tableCellAddress) Arguments:
tableCellAddress - a cell
address, e.g. "foo.1.4"
Returns:
the text from the specified cell
Gets the text from a cell of a table. The cellAddress syntax
tableLocator.row.column, where row and
column start at 0.
Check this,
int i =1;
while(selenium.isElementPresent("//table/tbody/tr["+i+"]/td[2]")){
System.out.println(selenium.getText("//table/tbody/tr["+i+"]/td[2]"));
i++;
}
Result will contain all the values listed.Hope it helps!!!
String value1 = selenium.getText("//tr[1]/td[1]/strong[contains(text(), Label1:')]/../../td[2]");
selenium.gettable(xpath)