add items in navbar <ul>.. mode=dropdown

348 views
Skip to first unread message

ctrlSoft

unread,
Apr 30, 2013, 11:20:52 AM4/30/13
to web...@googlegroups.com
hi, how to add items in 
<ul class="dropdown-menu" style="display: block;">
...
...
...
</ul>
generated by auth.navbar(mode="dropdown"),

ctrlSoft

unread,
Apr 30, 2013, 11:22:56 AM4/30/13
to web...@googlegroups.com
by default i have, login, logout .... register, i wish i could append some links only if auth.user

Anthony

unread,
Apr 30, 2013, 11:37:33 AM4/30/13
to web...@googlegroups.com
The auth.navbar() produces an HTML helper DOM, so you can manipulate it as usual:

navbar = auth.navbar(mode='dropdown')
if auth.user:
    navbar
.element('ul.dropdown-menu').components.extend([LI('Item 1'), LI('Item 2'), ...])

Anthony

Neeraj Shukla

unread,
May 23, 2015, 6:01:34 AM5/23/15
to web...@googlegroups.com

navbar = auth.navbar(mode='dropdown')
if auth.user:
    navbar
.element('ul.dropdown-menu').components.extend([LI('Item 1'), LI('Item 2'), ...])

Above method is not working on web2py 2.10.4

however jquery works.
For example
{{block page_js}}
{{if auth.user:}}
<script>
    jQuery("ul.dropdown-menu").append('<li>item 1</li>');
</script>
{{pass}}
{{end page_js}} 

However, use of javascript may slowdown page loading. Can anyone please suggest better working approach for mentioned web2py version ?

Anthony

unread,
May 23, 2015, 9:46:44 AM5/23/15
to web...@googlegroups.com, neeraj...@gmail.com
Hmm, seems to work for me. Can you show your exact code as well as the HTML markup that ends up in the browser?

Neeraj Shukla

unread,
May 23, 2015, 12:00:23 PM5/23/15
to web...@googlegroups.com, neeraj...@gmail.com

Following code I used and it produces None in HTML markup
{{=auth.navbar(mode='dropdown').element('ul.dropdown-menu').components.extend([LI('Item 1'), LI('Item 2')])}}


Couldnt figure it out.

Anthony

unread,
May 23, 2015, 5:46:00 PM5/23/15
to web...@googlegroups.com, neeraj...@gmail.com
Your code is incorrect. Instead of inserting the full navbar object, you apply a string of methods to it and insert the return value of the final method (which is None). As in the original code example, you must assign the navbar to a variable, then apply the manipulation, and finally insert the edited navbar:

{{navbar = auth.navbar(mode='dropdown')

  if auth.user:
      navbar
.element('ul.dropdown-menu').components.extend([LI('Item 1'), LI('Item 2'),
...])}}
{{=navbar}}

Anthony

Neeraj Shukla

unread,
May 24, 2015, 9:55:05 AM5/24/15
to web...@googlegroups.com, neeraj...@gmail.com
Thanks Anthony...

Richard Vézina

unread,
Apr 10, 2017, 4:07:25 PM4/10/17
to web2py-users
Hello Anthony,

Is there a way to manipulate auth.navbar content from model file somehow avoiding repeat this in every layout file I have?

Thanks

Richard

--
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
---
You received this message because you are subscribed to the Google Groups "web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to web2py+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Richard Vézina

unread,
Apr 10, 2017, 4:28:46 PM4/10/17
to web2py-users
Nevermind, I have extract header of every layout I have anyway into header.html that I include back in each layout so, my app stay DRY.

I am glad I had issue with selecting navbar jquery injected element that I can't select with selenium/splinter as it allow me to figure an new way to include my tag by user membership... Doing it like you propose prevent me from making a select to check if user is member of the proper group, reducing page load time... One less select!!

:)

Richard
Reply all
Reply to author
Forward
0 new messages