jQuery Mobile: alternative of .trigger('create') or .page() when updating navbar? - javascript

I have the following jQuery Mobile HTML code, the navbar's content is set using javascript. jQuery mobile styles the navbar when it's set statically, but when you set the content of it (html) later using javascript, you have to do some extra work to make it work:
<div data-role="header">
<h1 id="title">App</h1>
</div><!-- /header -->
<div data-role="content" id="content">
<p>Loading...</p>
</div><!-- /content -->
<div data-role="footer" data-position="fixed">
<div data-role="navbar" id="navbar">
<ul id="menu">
</ul>
</div>
</div><!-- /footer -->
</div><!-- /page -->
trigger('create'); is generally used to solve the problem of unstyled markup when set using javascript/ajax. However, it seems only to work within data-role="content" and not for #navbar. The script below should work but leaves the menu unstyled...
$(function(){
$("#menu").html("<li><a href='#'>Test Styling</a></li>").trigger('create');
});
Any ideas how to solve this? I have tried page(); and .listview('refresh'); with no results.

Try calling the navbar method after appending the list item:
$(function(){
$("#menu").html("<li><a href='#'>Test Styling</a></li>");
$("#navbar").navbar();
});
Edit:
You could also try creating the navbar dynamically :
var footer = $("#footer-id");
var navBar = $("div", {
"data-role":"navbar",
"html":"<ul><li><a href='#'>Test Styling</a></li></ul>"
}).appendTo(footer).navbar();

Related

JQuery Mobile Multipage & Appended footer has no styling (Intel XDK)

The multipage is working, but I want to inject the footer code in every page container.
Page Code:
<div data-role="content">
<div class="upage" id="mainpage" data-role="page">
<div class="upage-outer">
<!-- some content -->
</div>
</div>
<div class="upage" id="uberpage" data-role="page">
<div class="upage-outer">
<!-- some content -->
</div>
</div>
</div>
And the I just inject the footer code with some buttons (When device is ready):
$(".upage-outer").append('<div data-role="footer">Copyright + Buttons</div>');
This is the full footer code:
<div data-role="footer" data-position="fixed" class="container-group inner-element uib_w_4" data-uib="jquery_mobile/footer" data-ver="0">
<div data-role="controlgroup" data-type="horizontal" class="brideButtonGroup">
<a class="widget uib_w_6" data-role="button" href="#mainpage" rel="external">Wetter</a>
<a class="widget uib_w_7" data-role="button" onclick="intel.xdk.device.launchExternal('http://wetter2.mt-labor.it.hs-worms.de/plot');">Mehr</a>
<a class="widget uib_w_8" data-role="button" href="#uberpage" rel="external">Über</a>
</div>
</div>
The code gets injected, but on the mainpage, the Buttons are not displayed; instead, there are plain text links. On the second page, everything is displayed very well. I can switch between the pages.
What did I do wrong?
edit: I used enhanceWhithin(), and its working. But when I click somewhere on the document, strange colors appear, which are caused by the class ".ui-fixed-hidden". Where does this come from?
edit2: https://jsfiddle.net/564f1fkf/ It's not exactly the same, but if you go to site 2 and then back to site 1 and click somewhere on the page, you should see the problem.
PS: See pictures
You have to call the enhanceWithin() method on data-role="content" element. This will enhance the newly added HTML into jQuery mobile format.
$("[data-role='content']").enhanceWithin();

Jquery mobile not rendering all 'data-role="pages"' present in an html file

I have an html file in my WEB-INF folder which can only be accessed by a servlet. Here is how my servlet forwards the request to that html page
ServletContext context= getServletContext();
RequestDispatcher rd= context.getRequestDispatcher("/WEB-INF/file.html");
rd.forward(req, resp);
My file.html has 2 data-role="page" but only the 1st data-role="page" is being rendered. Do you guys have any idea as to why only 1 of the 2 pages is being rendered by jquery-mobile. Here is a template of my file.html.
<div data-role="page" id="profile-page" data-dialog="true">
...
</div>
<div data-role="page" id="main-page">
...
</div>
So basically my servlet forwards the user to the file.html, could that be one of the reasons why the page is not being created.
In jQuery Mobile we can show only one page at a time, as each page occupies full visible height.
There is multipage template option available, but even there we can show only one page at a time, but we can link to other pages with help of navigation.
The sample code for multipage template from jQuery Docs
<body>
<!-- Start of first page -->
<div data-role="page" id="foo">
<div data-role="header">
<h1>Foo</h1>
</div><!-- /header -->
<div role="main" class="ui-content">
<p>I'm first in the source order so I'm shown as the page.</p>
<p>View internal page called bar</p>
</div><!-- /content -->
<div data-role="footer">
<h4>Page Footer</h4>
</div><!-- /footer -->
</div><!-- /page -->
<!-- Start of second page -->
<div data-role="page" id="bar">
<div data-role="header">
<h1>Bar</h1>
</div><!-- /header -->
<div role="main" class="ui-content">
<p>I'm the second in the source order so I'm hidden when the page loads. I'm just shown if a link that references my id is beeing clicked.</p>
<p>Back to foo</p>
</div><!-- /content -->
<div data-role="footer">
<h4>Page Footer</h4>
</div><!-- /footer -->
</div><!-- /page -->
</body>
multipage demo
NOTE:
What you are trying to do is not possible with multiple pages. You
have to use single page and show/hide the specific divs as per your
requirement. You have to write your own logic, jQuery Mobile can't help in this scenario.
Reference:
jquery mobile official reference

jQuery Mobile Panel does not close on click

I have a PhoneGap application wich uses a jQuery Mobile multipage layout. On every page (page A and page B) I have a navbar for switching between pages. No custom code involved. I also have a panel on every page which opens by clicking the 'new' button in the header.
Sometimes it happens that I hit the button (maybe to soft? or to fast?) to open to panel but the buttons just changes it state to "active" and the panel does not open. I cannot click the button anymore. When I change the page and go back (all by using the navbar) page A appears again and now the panel is opened. This is a strange behaviour but could be plausible. But trying to close it now is just impossible. Clicking one of the buttons in the panel is as inactive as clicking on the page itself. The whole thing seems to be froozen.
<div data-role="page" id="one">
<div data-role="header" data-transition="fixed">
New
<h1 class="header">myApp</h1>
Options
</div>
<!-- Header -->
<div data-role="panel" id="panel_one">
<div data-role="collapsible-set" class="menu"></div>
</div>
<!-- Panel -->
<div data-role="content">
<div data-role="navbar">
<ul>
<li><a href="#one" data-transition="none" class="ui-btn-active" >A</a></li>
<li><a href="#two" data-transition="none" >B</a></li>
</ul>
</div>
</div>
<!-- Content -->
</div>
<!-- Page One-->
<div data-role="page" id="two">
<div data-role="header" data-transition="fixed">
New
<h1 class="header">myApp</h1>
Options
</div>
<!-- Header -->
<div data-role="panel" id="panel_two">
<div data-role="collapsible-set" class="menu"></div>
</div>
<!-- Panel -->
<div data-role="content">
<div data-role="navbar">
<ul>
<li>A</li>
<li><a href="#two" data-transition="none" class="ui-btn-active" >B</a></li>
</ul>
</div>
</div>
<!-- Content -->
</div>
<!-- Page Two-->
menu() creates the content of the Menu which is not shown in the example. Another method (loadPanel()) is boud to the pageshow-event and will populate the sidebar with the buttons. every button will execute
var pageId = $.mobile.activePage.attr("id");
$("#panel_" + pageId).panel('close');
before anything else is executed. However, all of this works most of the time and I would just like to know if some observed a similar behaviour.
As per jQuery mobile documentation
A panel must be a sibling to the header, content and footer elements inside a jQuery Mobile page. You can add the panel markup either before or after these elements, but not in between.
Maybe you should move up your panel because it may be the cause of the strange behaviour

Design a single page website with jQuery

Below is my psuedo-code for a Single page website I am doing. I need to be able to make a single page website that is not scroll-based (which is the typical). The idea here is that my site have a nav where user selects a menu and depending on the selected menu the correct page div is rendered on the content div.
The page divs are also in the HTML and should not be displayed unless the correct menu is selected (or if a better approach is to place the pages in Javascript file):
<nav>
<ul>
<li>menu1</li>
<li>menu2</li>
</ul>
</nav>
<div id="content">
<!-- content will be from page divs below -->
</content>
<div id="page1"> <!-- for menu1 -->
<div>the content</div>
</div>
<div id="page2"> <!-- for menu2 -->
<div>the content</div>
</div>
How can achieve this with jQuery?
Here is what you looking for
Html :-
<nav>
<ul>
<li class="nav active" data-id="1">menu1</li>
<li class="nav" data-id="2">menu2</li>
</ul>
</nav>
<div id="content">
<!-- content will be from page divs below -->
<div id="page1" class="page">
<!-- for menu1 -->
<div>the content from page 1</div>
</div>
<div id="page2" class="hide page">
<!-- for menu2 -->
<div>the content from page2</div>
</div>
</div>
Jquery :-
$(document).ready(function () {
$('.nav').click(function () {
$(this).addClass('active').siblings().removeClass('active');
$('.page').hide();
var clickedId = $(this).data('id');
$('#page' + clickedId).show();
});
});
Working Demo
To display preloaded contents in different containers you can use anchor tag with some CSS
HTML
a
b
c
<div id="page1" class="page"> this is page1 id</div>
<div id="page2" class="page"> this is page2 id</div>
<div id="page3" class="page"> this is page3 id</div>
CSS
#page1, #page2, #page3{
display:none;
}
#page1:target{
display:block;
}
#page2:target{
display:block;
}
#page3:target{
display:block;
}
As another answer stated, you may want to take a look at AngularJS. Here is an excellent tutorial to get you started if you choose: http://www.youtube.com/watch?v=i9MHigUZKEM
However, since your question stated jQuery, the best way will probably be to have all the pages separate html pages that are loaded in via AJAX and replace your current html.
The other approach is to have everything on the page at once and hide and show the content based on the menu item the user clicked. However, this may make the page REALLY sluggish if you have a lot of content all on the page at once.
If you want the content to be embedded in the page but hidden, use jQuery's .hide() and .show()
If you want to keep the content in separate pages, use jQuery's .load()
use this code , make sure your #page1 and #page2 divs hidden initially
jQuery(document).ready(function(){
jQuery('li').click(function(){
var new_index = parseInt(jQuery(this).index())+1;
jQuery("div[id*='page']").hide();
jQuery('#page'+new_index).show();
});
});
<nav>
<ul>
<li>menu1</li>
<li>menu2</li>
</ul>
</nav>
<div id="content">
<!-- content will be from page divs below -->
</content>
<div id="page1" style="display:none";> <!-- for menu1 -->
<div>the content 1</div>
</div>
<div id="page2" style="display:none";> <!-- for menu2 -->
<div>the content 2</div>
</div>
Try this link, it will help you. you are having navigation bar on your right hand site from where you can navigate through out your website.
Single page website

footer hides when moves on next page

I have html structure like this
<div data-role="page" data-theme="b">
<div data-role="header" data-theme="b">
<h1>header</h1>
</div><!-- /header -->
<div data-role="content" data-theme="b">
next pagey
</div><!-- /content -->
<div data-role="footer" data-theme="b" data-position="fixed">
<h1>Footer</h1>
</div><!-- /footer -->
</div><!-- /page -->
<div id="p2" data-role="page" data-theme="b">
<div data-role="header" data-theme="b">
<h1>header</h1>
</div><!-- /header -->
<div data-role="content" data-theme="b">
<h1>Content</h1>
</div><!-- /content -->
<div data-role="footer" data-theme="b" data-position="fixed">
<h1>Content</h1>
</div><!-- /footer -->
</div><!-- /page -->
But when i click on next page and next page comes in with auto-added back btn in header, when I click on back button previous page appears but footer dissappears..
in FF working ok, but not in ipad simulator
Getting fixed footers to work is a pain for the JQM team. I think it's just another little bug that is caused by unforseen circumstances. Well, nevermind...
A bit of insight:
Those darn mobile browsers have no support of fixed CSS positioning, so this is done with javascript and absolute position (and a ton of hacks - I bet)
This should already give you a clue...
If the behaviour isn't specific just to the emulator (quite possible!) and the problem remains after you fill the page with contents (I mean - finish writing the application) then all you need to do is to kickstart the code that repositions the footer. I'd do it like that:
$('div').live('pageshow',function(){ $(window).trigger('orientationchange') });
The orientationchange event should work. You can also try resize if orientationchange causes any new problems.

Categories

Resources