Classes having one or more students. I wanted to display multiple classes in rows and student names in that particular class as comma separated values. I have used following code but it doesn't work. It is giving runtime error as
CS0201: Only assignment, call, increment, decrement, await, and new object expressions can be used as a statement.
`columns.Bound(c => c.Students).Template
( m =>
#<text>
#foreach (var student in m.Students)
{
<li> #{ #student.Name; }</li>
}
</text>
);`
The right syntax is this:
columns.Bound(c => c.Students).Template(#<text>
<ul>
#foreach (var student in item.Students)
{
<li>#student.Name</li>
}
</ul>
</text>);
use this
<li> #{ #item.student.Name; }</li>
instead of
<li> #{ #student.Name; }</li>
To get the comma-separated values in the column, I did this:
columns.Bound(s => s.Students).Title("Students").Template(#<text>
#String.Join(",", (from g in item.Students select g.name))
</text>
);
Related
I have a complex nested list of models that looks like this:
hierarchy_tree = ['0000', 'hierarchy' [['0000-22', 'hierarchy2', [['0000-33', 'hiearchy3', [['0000-44-4444', 'hiearchy4', [['0000-55-5555-55', 'hiearchy5', []]]]]]]]]]
I am able to easily display this in a template using dot notation - example:
{% for hierarchy in hierarchy_tree %}
<tr class="item" data-id="{{system.0}}" data-parent="">
<td>
{{hierarchy.0}}
</td>
<td>
{{hierarchy.1.genericname}}
</td>
Now I am trying to output this to an .xlsx file but I cannot figure out how to pass all of the levels of this list? How can I do the same thing that I did in the template to pass this list to excel?
I have tried the following which will return the 1st list but throws an error (ValueError at /post/1/export/hierarchy/ - cannot covert(my passed in list)to excel) for the sublists because of the way that they are nested I believe.
for r in hierarchy_tree:
ws.append(r)
I have also tried and failed repeatedly to access the sublists using other methods.
So bottom line I need to figure out how to access and pass the values for the sublists - any ideas or help will be greatly appreciated?
Thank you
Comment: ... is just 1 row in the list ...
Python print a instance of Type list in one line, surounding with [ ... ].
Your hierarchy_tree is of Type list of n lists.
Every n't list in hierarchy_tree starts with [ and ends with ].
You have to break your hierarchy_tree into row Data.
For instance:
def treeWalk(tree, level=0):
rData = [ '' for i in range(level)]
for item in tree:
if isinstance(item, list):
if len(rData) > level:
ws.append(rData)
level += 1
treeWalk(item, level)
return
rData.append(item)
treeWalk(hierarchy_tree)
Tested with Python:3.4.2 - openpyxl:2.4.1 - LibreOffice: 4.3.3.2
I have one array of objects inside this objects I have some list of id's and using this id's I have to render multiple templates using #each loop for handlebars.
Simple this statement will return me the list of containing names of template.
{{#each data in sections1}}
{{data.id}}
{{/each}}
Output :
A B C D E F G H I J K L M N O P Q R S T U V W X
Consider this all A to Z are different template names respectively
Now I'm trying to render all these templates using #each loop in handlebar But I don't know the exact syntax.
For example can we do like this way ?
{{#each data in sections1}}
{{render data.id}} //Here I'm getting error Because of it is not exact syntax
{{/each}}
Maybe you should consider using partials, naming them with the proper name based on data.id value. Then use them like: {{partial data.id}}
DEMO
I need a semantic query result table template where I can have both newline-delimited cell results for some columns and comma-delimited columns in the same table.
If I use the standard format=broadtable, for example, the result is separated by newlines inside the table cells for all columns:
{{#ask:[[Category:Items]]
|?Description
|?Models
|?Addons
|format=broadtable
}}
If I create a template, all I am able to accomplish is a comma-separated result:
<includeonly>
{| class="wikitable sortable"
! style="width: 30%;" | Page
!! style="width: 30%;" | Description
!! style="width: 20%;" | Models
!! style="width: 20%;" | Addons
|-
</includeonly>
{{#ask:[[Category:Items]]
|?Description
|?Models
|?Addons
|format=template
|template=QResTemplate
}}
<includeonly>
|}
</includeonly>
Here is the QResTemplate:
<includeonly>
| {{{1}}} || {{{2}}} || {{{3}}} || {{{4}}}
|-
</includeonly>
Every Item has multiple Models and Addons, so it is column 3 and 4 in the table that I need one of them to be comma-separated while the other is newline-separated.
If I add something after {{{3}}}, like a newline, it is added after the last Model on the table row, not after each Model as I want it to.
Use Extension:Arrays to format your comma separated outputs as you want:
<includeonly><!--
store arrays
-->{{#arraydefine:models|{{{3}}}}}<!--
-->{{#arraydefine:addons|{{{4}}}}}<!--
print row
-->
| {{{1}}} || {{{2}}} || {{#arrayprint:models|<br/>}} || {{#arrayprint:addons|, }}
|-
</includeonly>
First you store both lists as arrays. arraydefine assumes that your list is comma separated, unless you specify somthing else. Then you print your arrays again with #arrayprint, but this time you can decide how you want those values to be separated.
If you can't use commas (e.g. because some value contains a comma, you can add e.g. sep=¤ to your ask query, and then do {{#arraydefine:models|{{{3}}}|¤}} to tell arraydefine that you are using a different separator.
I've stumbled on a bit of challenge here: how to get the contents of a table in HTML with the help of a regular expression. Let's say this is our table:
<table someprop=2 id="the_table" otherprop="val">
<tr>
<td>First row, first cell</td>
<td>Second cell</td>
</tr>
<tr>
<td>Second row</td>
<td>...</td>
</tr>
<tr>
<td>Another row, first cell</td>
<td>Last cell</td>
</tr>
</table>
I already found a method that works, but involves multiple regular expression to be executed in steps:
Get the right table and put it's rows in back-reference 1 (there may be more than one in the document):
<table[^>]*?id="the_table"[^>]*?>(.*?)</table>
Get the rows of the table and put the cells in back-reference 1:
<tr.*?>(.*?)</tr>
And lastly fetch the cell contents in back-reference 1:
<td.*?>(.*?)</td>
Now this is all good, but it would be infinitely more awesome to do this all using one fancy regular expression... Does someone know if this is possible?
There really isn’t a possible regex solution that works for an arbitrary number of table data and puts each cell into a separate back reference. That’s because with backreferences, you need to have a distinct open paren for each backref you want to create, and you don’t know how many cells you have.
There’s nothing wrong with using looping of one or another sort to pull out the data. For example, on the last one, in Perl it would be this, given that $tr already contains the row you need:
#td = ( $tr =~ m{<td.*?>(.*?)</td>}sg );
Now $td[0] will contain the first <td>, $td[1] will contain the second one, etc. If you wanted a two-dimensional array, you might wrap that in a loop like this to populate a new #cells variable:
our $table; # assume has full table in it
my #cells;
while(my($tr) =~ $table = m{<tr.*?>(.*?)</tr>}sg) {
push #cells, [ $tr =~ m{<td.*?>(.*?)</td>}sg ];
}
Now you can do two-dimensional addressing, allowing for $cells[0][0], etc. The outer explicit loop processes the table a row at a time, and the inner implicit loop pulls out all the cells.
That will work on the canned sample data you showed. If that’s good enough for you, then great. Use it and move on.
What Could Ever Be Wrong With That?
However, there are actually quite a few assumptions in your patterns about the contents of your data, ones I don’t know that you’re aware of. For one thing, notice how I’ve used /s so that it doesn’t get stuck on newlines.
But the main problem is that minimal matches aren’t always quite what you want here. At least, not in the general case. Sometimes they aren’t as minimal as you think, matching more than you want, and sometimes they just don’t match enough.
For example, a pattern like <i>(.*?)</i> will get more than you want if the string is:
<i>foo<i>bar</i>ness</i>
Because you will end up matching the string <i>foo<i>bar</i>.
The other common problem (and not counting the uncommon ones) is that a pattern like <tag.*?> may match too little, such as with
<img alt=">more" src="somewhere">
Now if you use a simplistic <img.*?> on that, you would only capture <img alt=">, which is of course wrong.
I think the last major remaining problem is that you have to altogether ignore certain things in parsing. The simplest demo of this embedded comments (also <script>, <style>, andCDATA`), since you could have something like
<i> some <!-- secret</i> --> stuff </i>
which will throw off something like <i>(.*?)</i>.
There are ways around all these, of course. Once you’ve done so, and it is really quite a bit of effort, you’ll find that you have built yourself a real parser, completely with a lot of auxiliary logic, not just one pattern.
Even then you are only processing well-formed input strings. Error recovery and failing softly is an entirely different art.
This answer was added before it was known the the OP needed a solution for c++...
Since using regex to parse html is technically wrong, I'll offer a better solution. You could use js to get the data and put it into a two dimensional array. I use jQuery in the example.
var data = [];
$('table tr').each(function(i, n){
var $tr = $(n);
data[i] = [];
$tr.find('td').text(function(j, text){
data[i].push(text);
});
});
jsfiddle of the example: http://jsfiddle.net/gislikonrad/twzM7/
EDIT
If you want a plain javascript way of doing this (not using jQuery), then this might be more for you:
var data = [];
var rows = document.getElementById('the_table').getElementsByTagName('tr');
for(var i = 0; i < rows.length; i++){
var d = rows[i].getElementsByTagName('td');
data[i] = [];
for(var j = 0; j < d.length; j++){
data[i].push(d[j].innerText);
}
}
Both these functions return the data the same way.
I have a sort of mini parsing syntax I made up to help me streamline my view code in cakephp. Basically I have created a table helper which, when given a dataset and (optionally) a set of options for how to format the data will render out a table, as opposed to me looping though the data and editing it manually.
It allows the user to be as complex or as simple as they like, it can get pretty powerful. However, In order to achieve this I had to make a simple parsing syntax. As a quick example the user would do something like so:
$this->Table->data = $userData;
$this->Table->elements['td']['data'] = array(
'{:User.username:}',
'{:User.created:}' => array('Time::nice')
);
echo $this->Table->render();
And when rendering the table would then generate:
<table>
<tbody>
<tr><td>rich97</td><td>Sun 21st 02:30pm</td></tr>
</tbody>
</table>
The problem occurs then I try to nest the braces like so:
{:User.levels.iconClasses.{:User.access:}:}
Is there anyway I can only get the inner most brackets on the first time round and loop though until there are no matches? Or even do it in one go? Or even better use strpos?
Here is my regex as it stands:
'/\{\:([^}]+)\:\}/'
Just add the opening brace to your negated character class:
'/\{:([^{}]+):\}/'
var $validate= array(
'name'=>array(
'notEmpty' =>array(
'rule'=>'notEmpty',
'message'=>'Please Enter The Name'
),
'isUnique' =>array(
'rule'=>'isUnique',
'message'=>'Name Already Exist'
)
),
'address'=>array(
'rule'=>'notEmpty',
'message'=>'Please Enter The Address')
);