Search code examples
jquery-mobilehtml-tablereflow

jQuery mobile table reflow with name/value columns


jQuery mobile table reflow works for tables with THEAD. But for the table with name/value columns, for example, 4 columns without THEAD

Name:   John                ID:     00002
Age:    22                  Date:   2015-2-3

How to make it responsive? Like the following on smart phones

Name:   John                
ID:     00002
Age:    22                  
Date:  2015-2-3

or

Name:   
John                
ID:     
00002
Age:    
22                  
Date:  
2015-2-3

Solution

  • What you want is a responsive grid rather than a table. You can do this with the jQM listview and some CSS Media Queries to change the layout at different device widths.

    The markup would be along these lines:

    <ul data-role="listview" data-inset="true">
        <li>
            <div class="ui-grid-a">
                <div class="ui-block-a">Name:</div>
                <div class="ui-block-b">John</div>
            </div>
        </li>
        <li>
            <div class="ui-grid-a">
                <div class="ui-block-a">ID:</div>
                <div class="ui-block-b">00002</div>
            </div>
        </li>
        <li>
            <div class="ui-grid-a">
                <div class="ui-block-a">Age:</div>
                <div class="ui-block-b">22</div>
            </div>
        </li>
        <li>
            <div class="ui-grid-a">
                <div class="ui-block-a">Date:</div>
                <div class="ui-block-b">2015/2/3</div>
            </div>
        </li>
    </ul>
    

    Then Use CSS to make the list items float left at different breakpoints:

    html {
        box-sizing: border-box;
        margin: 0;
        padding: 0;
    }
    *, *:before, *:after {
      box-sizing: inherit;
    }
    
    .my-page .ui-listview li .ui-block-a{
        color: #999;
    }
    .my-page .ui-listview li .ui-block-b{
        color: #111;
    }
    
    /* First breakpoint is 48em (768px). 2 column layout.  */
     @media (min-width: 48em) {
        .my-page .ui-listview li {
            float: left;
            width: 50%;
            margin: 0;
            border-radius: 0;
            border: rgb(221, 221, 221) solid 1px;
        }
    }
    /* Second breakpoint is 63.75em (1020px). 4 column layout.  */
     @media (min-width: 63.75em) {
        .my-page .ui-listview li {
            width: 25%;
        }
    }
    

    Here is a DEMO with 3 breakpoints

    Obviously you should tweak the breakpoints to fit your content...