I need to change to a different page on start up and I want it to be a dialog.
This works in 1.2 and works with IE and FireFox in 1.3.
Chrome displays the dialog then immediately reverts to the first page.
If I remove the "role: dialog" it works with everyone. I've tried a hidden hyperlink that I force a click on ... same result. Chrome (latest version) reverts to page1.
Any ideas on how to fix this?
Here is a simplified fiddle http://jsfiddle.net/stocksp/P4ZWj/3/
div data-role="page" id="page1">
<div data-role="content">
<h6>page one content</h6>
</div>
</div>
<div data-role="page" id="page2">
<div data-role="header">
<h1 class="title">My DIALOG </h1>
</div><!-- /header -->
<div data-role="content">
Dialog Content
</div>
</div>
$(document).delegate("#page1", "pageinit", function () {
$.mobile.changePage('#page2', { transition: 'pop', role: 'dialog' });
});
Quick workaround til this is fixed: wrap the changePage method in a setTimeout...
$(document).delegate("#page1", "pageinit", function () {
setTimeout(function () {
$.mobile.changePage('#page2', { transition: 'pop', role: 'dialog' });
}, 100);
});
Related
I am new to JQuery Mobile. Currently trying to use JQM in an ASP.NET MVC project. I am following this tutorial found on JQM official site. But it uses multiple html pages.
<div data-role="page" id="page2" data-prev="page1" data-next="page3">
<div role="main" class="ui-content">
.....
</div>
</div>
Here data-next="page3" is used to show page3.html. My question- is page3.html being loaded by ajax while viewing page2.html ?. If I would like to use all the pages in a single html file by using JQM Page, how should i proceed?
data-next and data-prev are just arbitrary HTML5 data attributes used in the example to store the url of the next and previous pages. For internal pages you could set them to "#page2", "#page3", etc. So for 3 pages you might set it up like this:
<div id="page1" data-role="page" data-prev="" data-next="#page2">
<div data-role="header" data-position='fixed'>
<h1>Page 1</h1>
</div>
<div data-role="content">
I am page 1
</div>
</div>
<div id="page2" data-role="page" data-prev="#page1" data-next="#page3">
<div data-role="header" data-position='fixed'>
<h1>Page 2</h1>
</div>
<div data-role="content">
I am page 2
</div>
</div>
<div id="page3" data-role="page" data-prev="#page2" data-next="">
<div data-role="header" data-position='fixed'>
<h1>Page 3</h1>
</div>
<div data-role="content">
I am page 3
</div>
</div>
Then you just update the example's javascript to use the internal url without adding the ".html" to it:
function navnext( next ) {
$( ":mobile-pagecontainer" ).pagecontainer( "change", next, {
transition: "slide"
});
}
function navprev( prev ) {
$( ":mobile-pagecontainer" ).pagecontainer( "change", prev, {
transition: "slide",
reverse: true
});
}
$( document ).one( "pagecreate", "#page1", function() {
// Handler for navigating to the next page
// Navigate to the next page on swipeleft
$( document ).on( "swipeleft", ".ui-page", function( event ) {
// Get the filename of the next page. We stored that in the data-next
// attribute in the original markup.
var next = $( this ).jqmData( "next" );
if ( next ) {
navnext( next );
}
});
// The same for the navigating to the previous page
$( document ).on( "swiperight", ".ui-page", function( event ) {
var prev = $( this ).jqmData( "prev" );
if (prev) {
navprev( prev );
}
});
});
jQM uses the jqmData() function to get and set data attributes.
Here is a working DEMO
I have a list view inside a popup , when the user select a list element I want to change this element "li" background color , I have tried the following code it was working on jQuery mobile 1.3.2 but it didn't work when i upgraded my app to 1.4.0 , How can I change the background color of the list element when the user click on it ? please help me
<div data-role="page" id="index">
<div data-role="header" data-theme="b">Main</div>
<div data-role="content">
Show Popup
</div>
<div data-role="popup" id="MyPOPUP" data-position-to="window" data-corners="false" data- overlay-theme="a" data-dismissible="false">
<div data-role="header" data-theme="a">
<div style="text-align:center;float:center;padding-top:11px;">
<font size="6px" color="white">Countries</font>
</div>
</div>
<div id="scrollContent" class="content" data-role="content" style="background-color: white;">
<ul data-role="listview" id="countrieslist" style="margin: 0 !important;">
</ul>
</div>
</div>
</div>
Java script code
$('#index').on( 'pageinit',function(){
for(var i=1;i<=50;i++)
{
$('#countrieslist').append('<li id="'+i+'">'+''+'<font>'+Country+i+'</font>' +'</li>');
}
$('#countrieslist').listview('refresh');
});
$('#Btn1').on('touchstart', function(){
$(this).css({background: 'white'});
$(this).attr('href','#MyPOPUP');
});
$('#countrieslist').on('click','li', function() {
$(this).css({background: 'blue'});
selected_elem = $(this).attr('id');
alert('you selected' + selected_elem);
$('#MyPOPUP').popup('close');
});
You have a little typo in your loop that creates the countries, but other than that the code seems to work,
Here is a working DEMO
Because pageinit is deprecated n 1.4, I have used pagecreate; and in the for loop the word country after the font tag should be within the single quotes as it is not a variable. Also, in the li click, I reset all other countries to transparent background before setting the newly selected one:
$(document).on( 'pagecreate', '#index',function(){
for(var i=1;i<=50;i++) {
$('#countrieslist').append('<li id="'+i+'"><font>Country' + i +'</font></li>');
}
$('#countrieslist').listview('refresh');
$('#Btn1').on('click', function(){
$(this).css({background: 'white'});
$(this).attr('href','#MyPOPUP');
});
$('#countrieslist').on('click','li', function() {
$('#countrieslist li').css({background: 'transparent'});
$(this).css({background: 'blue'});
selected_elem = $(this).attr('id');
alert('you selected' + selected_elem);
$('#MyPOPUP').popup('close');
});
});
JQM 1.3 Iscrollview 1.3.1
I have a list of links to events using data-iscroll. Each event is also a list (title/date-location/description).
Each time I click on the event list, the event is displayed. If I scroll down the content, when I go back to the event list and then click on another event, the view scrolls to where the previous view was stopped.
I've successfully stopped this by launching an empty() on the event content and then calling updatelayout on the back button of the event content :
$("#bhome").on('vclick', function(e) {
$('#econt').empty().trigger('updatelayout');
$.mobile.loading('show');
e.preventDefault();
$.mobile.changePage("#page1");
});
But, of course, android users don't use a back button and use the back key instead.
I've tried to empty() and updatelayout on the pagebeforehide event but apparently, the position is saved before that event happens :
$('#event').on('pagebeforehide', function(event, data) {
$('#econt').empty();
$('#econt').trigger('updatelayout');
$('#escroll').trigger('updatelayout');
});
I've also tried to use the silentscroll function but it's not working either :
$(document).on('pageshow', '#event', function(){
$.mobile.silentScroll(0);
});
How can I make sure that on viewing a new event, the position is back to the top ?
Here is a snippet of my index.html file :
<div id='container'>
<div data-role='page' id='page1' data-theme="c" style="background: black;">
</div>
<div data-iscroll style='background-color:#ddd;'>
<ul id="el"></ul>
</div>
<div data-role='footer' data-position='fixed' data-theme="a" data-tap-toggle="false">
</div>
</div>
<div data-role="page" id="event" data-theme="c" style="background:black;">
<div data-role='header' data-position='fixed' data-theme="a" style="height:42px;">
<a id="bhome" class="ui-btn-left ret" data-icon="arrow-l" href="#" data-iconshadow="false">Back</a>
<h1 id='eh1'></h1>
</div>
<!-- data-role='content' entraine un scroll horizontal -->
<div data-iscroll style='background-color:white;' id='escroll'>
<ul id='econt'></ul>
</div>
</div>
The answer was given by the iscrollview author (works perfectly) :
$("#escroll").iscrollview("scrollTo", 0, 0, 0, false);
I create a list view dynamically, but i have no clue on how i get the data from the selected list to appear in the dialog box. I have tried several ways but all failed. The only thing i have got going correctly is calling the dialog on click.
I want to display the whole message in the pop up dialog box
my code
<body>
<div data-role="page" id="messages">
<div data-theme="a" data-role="header">
<a data-role="button" href="index.html" data-transition="slide"
data-icon="arrow-l" data-iconpos="left" class="ui-btn-left">
Back
</a>
Refresh
<h3>
Alerts
</h3>
</div>
<div data-role="content">
<script type="text/javascript">
$(document).on("pagebeforeshow", "#messages", function() {
var uid = $.cookies.get( 'uid' );
if(uid == null){
$.mobile.changePage( "account-login.html", { transition: "slide"} );}
$(function(){
$.getJSON("ajaxResponder.php?method=messages",function(data){
var htmlString ="";
$.each(data.messages,function(index,item){
htmlString +='<li data-name='+item.msgid+'><a href="#">'+
'<h3 class="topic">'+item.subject+'</h3>' +
'<p>From: '+item.sender+'</p>' +
'<p>Date: '+item.added+'</p>' +
'<p>'+ item.message +'</p></a></li>' ;
});
$('#list').html(htmlString);
$('#list').listview('refresh');
});
});
});
</script>
<ul id="list" data-role="listview" data-theme="d"></ul>
</div>
<script>
$('#list').on('click', 'li', function() {
$.mobile.changePage("#dialog", {
role: "dialog"
});
});
</script>
</div>
<div data-role="page" data-rel="dialog" id="dialog" data-theme="c" data-close-btn="right">
<div data-role="header" data-position="fixed" data-theme="b">
<h1>New values added!</h1>
</div>
hello
<!-- display item.message here -->
</ul>
</div>
</body>
</html>
updated
Based on your comment, to read any data of the parent element, use .closest().
Just read the text of <a> button and append it to the dialog. I used div #msg for demo purposes.
Demo
$('#list').on('click', 'li a', function () {
var text = $(this).closest('li').attr('data-name');
$('#msg').empty();
$('#msg').append(text);
$.mobile.changePage("#dialog", {
role: "dialog"
});
});
Here's my code in the <head></head>:
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.1.0/jquery.mobile-1.1.0.min.css" />
<script type="text/javascript" src="http://code.jquery.com/jquery-1.7.1.min.js"></script> <script type="text/javascript" src="http://code.jquery.com/mobile/1.1.0/jquery.mobile-1.1.0.min.js"></script>
<script type="text/javascript">
// <![CDATA[
$(function() {
});
$(document).ready(function () {
$('#dialog1')
.dialog({
position: 'center',
modal: true,
autoOpen: false
})
;
$('.panier')
.unbind('click')
.click(function(event) {
event.preventDefault();
$('#dialog1').dialog('open');
});
});
// ]]>
</script>
And the html code:
<div data-role="page">
<div data-role="header">
<h1>Choisissez vos pizzas !</h1>
</div>
<div data-role="content">
<div data-role="footer">
<div data-role="navbar">
<ul>
<li><a class="panier" href="/" data-role="button" data-icon="search">Voir panier</a></li>
</ul>
</div>
</div>
</div>
</div>
<div data-role="dialog" id="dialog1" class="app-dialog">
<div data-role="header">
<h3>A dialog</h3>
</div>
<div id="content" data-role="content">
<p>I am a dialog....!</p>
</div>
</div>
When I launch my page everything's fine until I click on the "panier" button: the error raised is:
Uncaught no such method 'open' for dialog widget instance
I really don't know why this doesn't work, because a dialog widget instance should have an open() method.
Any idea?
I think you are confusing the jquery mobile dialog with the jquery UI dialog. A jquery mobile dialog is really another JQM page just styled to look a bit more like a dialog (overlay, rounded corners). To display a JQM you just use the $.mobile.changePage('#yourDialog', optionalTranistion) method. That said JQM dialogs do have a close method (I'm not sure but there may have also had an open method at some point).
So for your code,
$(document).ready(function () {
/* $('#dialog1') this is JQUI code
.dialog({
position: 'center',
modal: true,
autoOpen: false
})
;*/
$('.panier')
.unbind('click')
.click(function (event) {
event.preventDefault();
//$('#dialog1').dialog('open');
$.mobile.changePage('#dialog1');
});
});
There is also a popup widget for JQM that is in development (and has been for a little while now).
You may also be interested in the simple dialog plug in for JQM.