GrantU
GrantU

Reputation: 6555

jQuery mobile dynamically added theme to page

I'm building a new page dynamically using jQuery mobile. I would like to now add the theme i.e. data-theme="a" . Is there an easier way to do this? At the moment it looks like I will be added it to every div below.

 var newPage = $("<div data-role='page' id='" + v["id"] +
                "'><div data-role=header><a data-iconpos='left' data-icon='back' href='#' data-role='button' " +
                "data-rel='back'>Back</a>" +
                "<h1>" + v["name"] + "</h1>" +
                "</div>" +
                "<div data-role=content>" + pagecontent +
                "</div>" +
                "<div data-role='footer'>" +
                "<h4>" + v["name"] + "</h4>" +
                " </div>" +
                "</div>");


            // Append the new page info pageContainer
            newPage.appendTo($.mobile.pageContainer);

i.e. it would be good if there was something like this....

 newPage.appendTo($.mobile.pageContainer).theme('a');

Upvotes: 0

Views: 1141

Answers (2)

Omar
Omar

Reputation: 31732

Update - jQuery Mobile 1.4

There is a bug in .page() widget when using .page({"theme"}) or .page("option", "theme"). It doesn't remove current theme class, however, it adds new theme class. Yet, new class doesn't override old class. So here is the fix.

(function($, undefined) {
  $.widget("mobile.page", $.mobile.page, {
    _setOptions: function(o) {
      if (o.theme !== undefined) {
        this.element
          .removeClass(function(i, c) {
            return (c.match(/\bui-page-theme-\w/g) || []).join(' ');
          })
          .addClass("ui-page-theme-" + o.theme);
      }
    }
  });
})(jQuery);

Note: Default theme in jQuery Mobile is "a".


To set theme for dynamically created pages, use $('.selector').page({theme:'e'}); after you append pages and before you navigate to them using $.mobile.changePage().

For a specific page:

$('.selector').page({theme:'e'});

For all pages:

$('[data-role=page]').page({theme:'e'});

Demo - Updated with .page() fix

Upvotes: 5

Juan Camilo Guarin P
Juan Camilo Guarin P

Reputation: 183

I use this and it works really great :D

Mikael Kindborg answer from Change data-theme in jQuery mobile

$.mobile.changeGlobalTheme = function(theme)
    {
        // These themes will be cleared, add more
        // swatch letters as needed.
        var themes = " a b c d e";

        // Updates the theme for all elements that match the
        // CSS selector with the specified theme class.
        function setTheme(cssSelector, themeClass, theme)
        {
            $(cssSelector)
                    .removeClass(themes.split(" ").join(" " + themeClass + "-"))
                    .addClass(themeClass + "-" + theme)
                    .attr("data-theme", theme);
        }

        // Add more selectors/theme classes as needed.
        setTheme(".ui-mobile-viewport", "ui-overlay", theme);
        setTheme("[data-role='page']", "ui-page-theme", theme);
        setTheme("[data-role='header']", "ui-bar", theme);
        setTheme("[data-role='listview'] > li", "ui-bar", theme);
        setTheme(".ui-btn", "ui-btn-up", theme);
        setTheme(".ui-btn", "ui-btn-hover", theme);
    };

Upvotes: 0

Related Questions