Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add Dynamically Links to jQuery Mobile

I read a lot about how to add stuff dynamically in jquery mobile, but I couldn't figure out how to add links.

Currently my solution looks like this:

  1. Add a new Page - with id (id="list-1")
  2. Creating a Link for it (href="#list-1")

This solution works perfectly in static pages, but I want to do it dynamically. I have tried a lot with page() and stuff like that but nothing helped me.

My questions are:

  1. How do I add dynamic links & pages?
  2. Did I choose the right way to use ids & anchors (#list-1) as links or is there another solution for jquery mobile?

Let me know if you need more information

like image 708
e382df99a7950919789725ceeec126 Avatar asked Mar 11 '26 22:03

e382df99a7950919789725ceeec126


1 Answers

To add dynamic links, I have found the easiest way is to just have an event listener waiting for a click on those links. This event listener then saves any parameters you want to pass into the next page you are visiting. You pass the parameters from the list element to the event listener by just specifying parameters within each "li" element.

(create the HTML for a list dynamically & store it into list-1-html)
$("div#my-page div[data-role=content]").html(list-1-html);
$("div.list-1 ul").listview();
$("div.list-1 ul").listview('refresh');

Then your event listener would look something like:

$('#my-page').delegate('li', 'click', function() {
    passedParameter = $(this).get(0).getAttribute('passed-parameter');
});

When jQuery Mobile loads your next page, you'll probably want to load this page dynamically and you'll have this passedParameter variable available to you. To load the page dynamically, just add a listener that waits for JQM to try to load the page:

$('[data-role=page]').live('pageshow',function(e, ui){ 
    page_name = e.target.id;
    if (page_name == 'my-page-2'){
        (do something with passedParameter)
    }
});

This is the workflow I use with jQuery Mobile and it has been working just fine. I'm guessing in future releases, though, that they'll build in some kind of support for passing dynamic parameters to pages.

like image 111
Spike Avatar answered Mar 14 '26 14:03

Spike



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!