How to create multi level menu using jade.pug - html

In header.pug
block link
-var selected='Home';
-var menu = [{'title':'Home','address':'/home/home.html','child':[]},{'title':'Shopping','address':'/shopping/shopping.html','child':[{'title':'TV','address':'/tv/tv.html'},{'title':'Smartphone','address':'/smartphone/smartphone.html'}]},{'title':'About','address':'/about/about.html','child':[]}]
#navbar
+navbar(selected,menu)
In mixin.pug
//- Navbar mixin
mixin navbar(selected, menus)
ul
each menu in menus
if selected === menu.title
li.active
a(href=menu.address, title=menu.address)= menu.title
if menu.child.length > 0
+navbar(selected, menu.child)
else
li
a(href=menu.address, title=menu.address)= menu.title
if menu.child.length > 0
+navbar(selected, menu.child)
Problem:
When compile it into html, there is a error, shown in the console:
Cannot read property of undefined in the "if menu.child.length > 0" row
Edit:
Added a picture:

The problem is with the data in menu.child when you call +navbar(selected, menu.child). Objects in menu.child don't have a propety child, so when Pug try to get the length of this undefined values the error appears.
Add child property inside objects in array menu.child.
Or modify a little bit your code to change if menu.child is not undefined (I assume you don't want to, because objects without child property will not be displayed:
if typeof menu.child == typeof []
if menu.child.length > 0
+navbar(selected, menu.child)

Related

List output from function on ipywidgets

I constructed a function linked to MySQL which returns a list of children for a given parent id. I would like to output this list of children using ipywidgets.
I am having trouble linking the function to ipywidgets. So far I have:
> from ipywidgets import widgets
>
> text1 = widgets.Text()
> text2 = widgets.Text()
> button = widgets.Button(description = 'Run')
> display(text1)
> display(button) display(text2)
>
> idnum = text1.value
> text2.value= list_children(idnum)
>
> button.on_click(list_children)
The function is the following:
> def list_children(parentid):
> value = parentid
> parent_80 = session.query(Parent).get(value)
> parent_80_children= parent_80.children
> childrenlist=[]
>
> for i in parent_80_children:
> childrenlist.append(i.UWI)
>
> return childrenlist
I keep getting the following error:
AttributeError: 'NoneType' object has no attribute 'children'
as it breaks in this line:
parent_80_children= parent_80.children
The function is correct if I run the python cell so I know it's working, but it breaks when I try to click the widget box "Run". Somehow there is no link between the function and the widget box.
I would like to have the output upon clicking the "Run" widget button as the following:
1771860100
1771860200
1771860300
minus the bullet points.
Any input is appreciated.
When you use the .on_click() method of a button, you need to specify the function you want to run, which you have done correctly. However, when you click the button, the button instance itself gets passed to the function. That explains why, when you click the button, you see that error. The function thinks it is getting a parentid (probably a string or int), but actually receives an instance of ipywidgets.Button instead!
Assuming you want to take the text in the text1 widget as you did in the upper section of your code, you need to pass the Text widget to the function. So, add this to your on_click() call using a partial to specify the text field as an argument. The text widget will be the first parameter, and the Button instance will follow as the 2nd parameter. If you forget that the Button instance gets passed when writing your function definition, you will get an error along the lines of 'takes exactly 1 argument but 2 given'.
Hopefully you can take the example below and modify to suit your needs.
import ipywidgets as widgets
from functools import partial
text1 = widgets.Text()
def list_children(text_field, button):
print(text_field.value)
button = widgets.Button()
button.on_click(partial(list_children, text1))
display(text1, button)

Razor - How to store html special character in variable

On my html page, I want to make a list from a collection of entries in my model. However, some of those entries may be null, and in that case I want to have an empty list item, like so:
Something
Something else
More stuff
I do that by inserting a non-breaking space character. (regular whitespace won't generate empty list items) Here's a code snippet:
<ul>
#{
foreach (var x in Model.Entries)
{
var rayon = x.Rayon ?? " ";
<li>#rayon</li>
}
}
</ul>
Sadly, this does not work because it instead pastes verbatim.
And removing the quotation marks, and/or adding #: at the start, won't compile.
What can I do?
You can use Html.Raw.
use like this
<li> #(Html.Raw(rayon)) </li>
Instead of " " just give a blank string:
var rayon = x.Rayon ?? string.Empty;
In fact, I think you can even just scrap that variable assignment together and just use x.Rayon as-is - a null value should have the same effect
<ul>
#{
foreach (var x in Model.Entries)
{
<li>x.Rayon</li>
}
}
</ul>
Update:
Both above work for me. So, if still not working, it looks to be down to CSS/styles on your ul.
e.g.
OK, I think this is down to CSS/styles on the ul that you have then (both above work fine for me). e.g. the following CSS would hide empty li elements:
ul li:empty {
display: none;
}
So, check out your CSS.
If you don't want to change the existing style, you could add an extra class to your CSS and apply that for just this instance.
i.e.
CSS:
ul.show-empty-li li:empty{
display:list-item;
}
HTML:
<ul class="show-empty-li">
...
</ul>

Python Selenium Click on all li in ul

Hi Ive used the following code and it works perfectly well. How can I make it click all the li items in a ul with an empty ie class="" >With an Xpath of
Xpath is ('//*[#id="Main_data"]/div[1]ul/li[1]/a/span')
html_list = driver.find_element_by_id("myId")
items = html_list.find_elements_by_tag_name("li")
for item in items:
text = item.text
print text
Ive tried item.click in the loop but obtained no results.
To locate elements having an empty class, use the "by XPath" locator:
items = driver.find_elements_by_xpath("//ul[#id = 'myId']//li[not(#class)]")
for item in items:
item.click()

Dynamic search/filter core-list (Polymer 0.5)

I need to implement a filter-type search which hides items in core list if they do not match the search. I created a .hidden class that is applied to an item if an expression returns false:
class = {{ {hidden: !match(model.host, hQuery)} | tokenList }}
The elements are hidden, but the list does not reflow elements unless I click on a visible row. Is there a way to force a reflow using a function call?
After a week of struggling, hiding list items is just not the right way to handle this. Iterate through the original array, push any matching objects to a temporary array, and then replace core-list's .data array with the temporary array: this.$.list_id.data = tmpArray. Performance is good for lists up to 10K records.
This is what I'm doing in my code, and it works:
<div style="{{hide_part1}}">
...content to show/hide...
</div>
....
Switching it based on route change(flatron-director):
routeChanged: function(oldValue, newValue) {
if ('some_route_1' == this.route) {
this.hide_part1 = ''
this.hide_part2 = 'display: none;'
} else if ('some_route_2' == this.route) {
this.hide_part1 = 'display: none;'
this.hide_part2 = ''
}
},
Also using core-list's updateSize() and especially scrollToItem(0), i.e. back to top, here and there helps as I also had problems with the 'reflow':
https://stackoverflow.com/questions/29432700/polymer-core-list-is-not-rendering-some-of-the-elements

libxml2 - remove child, but not grandchildren

I'm using libxml2 to parse HTML. I want to remove certain formatting tags like <center>, while keeping their content (for example, a link).
This means I'll have to remove certain child nodes from my xmlNodeSet, but keep that node's children.
Right now, I got this code:
xmlNodePtr parentNode = nodes->nodeTab[i];
if (parentNode != NULL) {
xmlNodePtr child = parentNode->children;
xmlNodePtr parentNextSibling = parentNode->next;
xmlNodePtr grandParent = NULL;
while (child) {
xmlUnlinkNode(child);
if (parentNextSibling != NULL) {
xmlAddPrevSibling(parentNextSibling, child);
}
else {
if (grandParent == NULL)
grandParent = parentNode->parent;
xmlAddChild(grandParent, child);
}
child = child->next;
}
xmlUnlinkNode(parentNode);
xmlFree(parentNode);
}
The code does add the child to the document, but it also deletes the node I was adding it as a sibling to. What am I doing wrong?
You're not saving off the child->next pointer before you cut it out of the tree. As soon as you unlink a node, it isn't part of the tree, so child->next becomes NULL. Then, after you reinsert it into the tree (before parentNode->next), the child->next pointer now points to what was previously parentNode->next, so the next time through the loop, you delete parentNode->next. Things can only go downhill from there. :-)