JQuery Mobile listview inside a panel

1.7k views Asked by At

I am trying to use listviews with collapsable items within a panel, but I am getting strange results. Specifically, this is a minimal code I am using. Note that everything in the list view is taken from a JQuery Mobile example, so I would expect it to work out of the box.

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
<html>
<head>
<title>Test</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="jquery.mobile-1.4.5.min.css" />
<script src="jquery-1.11.3.min.js"></script>
<script src="jquery.mobile-1.4.5.min.js"></script>
</head>

<body>
<h1></h1>

<div data-role="page" id="mainpage">

<div data-role="panel" id="leftpanel" data-position="left" data-display="push">
  <!-- panel content goes here -->
  <div data-role="tabs" id="tabs">
  <div data-role="navbar">
    <ul>
      <li><a href="#one-tab" data-ajax="false">one</a></li>
      <li><a href="#two-tab" data-ajax="false">two</a></li>
    </ul>
  </div>
  <div id="one-tab" class="ui-body-d ui-content">
    Empty one!
  </div>
  <div id="two-tab" class="ui-body-d ui-content">
    <ul data-role="listview" data-inset="true" data-shadow="false">
      <li data-role="collapsible" data-iconpos="right" data-inset="false">
      <h2>Birds</h2>
      <ul data-role="listview" data-theme="b">
        <li><a href=“#">Condor</a></li>
        <li><a href="#">Eagle</a></li>
        <li><a href="#">Sparrow</a></li>
      </ul>
      </li>
      <li><a href="#">Humans</a></li>
      <li data-role="collapsible" data-iconpos="right" data-inset="false">
      <h2>Fish</h2>
      <ul data-role="listview" data-theme="b">
        <li><a href="#">Salmon</a></li>
        <li><a href="#">Pollock</a></li>
        <li><a href="#">Trout</a></li>
      </ul>
      </li>
    </ul>
  </div>
  </div>
  </div>

  <div id="mainheader" data-role="header" data-position="fixed">
    <a href="#leftpanel" data-role="button" data-iconpos="notext" data-icon="bars"></a>
    <h1>Main page</h1>
  </div>

  Main page
  </div>  
  </body>
</html>

What I get is something like enter image description here

Which arguably is not exactly OK. What am I doing wrong?

1

There are 1 answers

1
ezanker On BEST ANSWER

You are mixing listviews and collapsibles in a way that jQM was not designed to deal with. However, you can make this look 'right' with some CSS.

<ul id="tabTwoList" data-role="listview" data-inset="true" data-shadow="false">
    <li>
        <div data-role="collapsible" data-inset="false" data-iconpos="right">
            <h2>Birds</h2>
            <ul data-role="listview" data-theme="b">
                <li><a href="#">Condor</a></li>
                <li><a href="#">Eagle</a></li>
                <li><a href="#">Sparrow</a></li>
            </ul>
        </div>
    </li>
    <li><a href="#">Humans</a></li>
    <li>
        <div data-role="collapsible" data-inset="false" data-iconpos="right">
            <h2>Fish</h2>
            <ul data-role="listview" data-theme="b">
                <li><a href="#">Salmon</a></li>
                <li><a href="#">Pollock</a></li>
                <li><a href="#">Trout</a></li>
            </ul>
        </div>
    </li>
</ul>

In the markup I have given the list an ID so we can easily refer to it in CSS and I have made the collapsibles DIVs within the LIs. Now we use CSS to remove some padding/margins/borders to make all items line up nicely:

#tabTwoList li.ui-li-static {
    padding-top: 0;
    padding-bottom: 0;
}
#tabTwoList li .ui-collapsible-heading {
    margin-top: 0;
    margin-bottom: 0;
}
#tabTwoList li .ui-collapsible-heading-toggle {
    border: 0;
}

Working DEMO