jQuery Mobile & AngularJS checkboxes horizontal

775 views Asked by At

Using jQuery Mobile and AngularJS together, without a plug-in but having read about it, loading jQuery first, and the two frameworks are mostly playing very nicely and quite powerful having both.

Trying to render jQuery Mobile checkboxes with

<div data-role="fieldcontain">
  <legend>Showing more lodges slows the display</legend>
  <fieldset data-role="controlgroup" data-type="horizontal">
      <label ng-repeat-start="(lodgekey, lodge) in data.lodges" for="chooser_{{lodgekey}}">{{lodge.lodgetitle}}</label>
      <input ng-repeat-end id="chooser_{{lodgekey}}" type="checkbox" ng-model="lodge.selected" />
    </div>
  </fieldset>
</div>

Problem is that jQuery Mobile finishes setting up the checkbox as a button prior to Angular doing the repeat. So the repeated checkboxes stack up vertically even though I have used data-type="horizontal" in the fieldset, and each show as first/last orphan - which they are before AngularJS does its ngRepeat. Viewing the code example at http://demos.jquerymobile.com/1.0a4.1/docs/forms/forms-checkboxes.html and looking at the rendered DOM shows the way it should render.

1

There are 1 answers

0
Nik Dow On

My solution so far has been to reproduce the jQuery Mobile form using Angular, but this is not ideal, here is my code:

<div data-role="fieldcontain" id="lodge-chooser">
  <legend>Showing more lodges slows the display</legend>
  <fieldset data-role="controlgroup" data-type="horizontal" class="ui-corner-all ui-controlgroup ui-controlgroup-horizontal">
    <div class="ui-checkbox" ng-repeat="(lodgekey, lodge) in data.lodges">
      <label ng-class="{'ui-btn-active':lodge.selected, 'ui-corner-left':$first, 'ui-corner-right':$last}" for="{{lodgekey}}">{{lodge.lodgetitle}}</label>
      <input id="{lodgekey}}" type="checkbox" ng-model="lodge.selected" />
    </div>
  </fieldset>
</div>

and CSS:

/* remove incorrect rounded corners which appear on all buttons*/
div#lodge-chooser label.ui-btn.ui-corner-all {
  border-radius:0!important;
}

/* reinstate rounded corners in correct places */
div#lodge-chooser label.ui-btn.ui-corner-left {
  border-bottom-left-radius:inherit!important;
  border-top-left-radius:inherit!important;
}
div#lodge-chooser label.ui-btn.ui-corner-right {
  border-bottom-right-radius:inherit!important;
  border-top-right-radius:inherit!important;
}

This works, noting that the div.ui-checkbox is nested redundantly because jQuery Mobile still adds it in but my addition provides the styles needed and the extra nested div doesn't appear to do any harm.