UI Pattern Bootstrap and Borderlayout"

From Documentation
 
(9 intermediate revisions by 2 users not shown)
Line 1: Line 1:
{{Template:Smalltalk_Author|
+
#REDIRECT  [[Small_Talks/2016/June/UI Pattern : Bootstrap and Borderlayout]]
|author=Sefi Lin, Engineer, Potix Corporation
 
|date=Jun 2, 2016
 
|version=ZK 8.0.2
 
}}
 
 
 
= Introduction =
 
More and more mobile devices are appearing on the market today, meaning the ability to browse on small screens has now become a necessity. To meet this demand, ZK provides a variety of layout components to help customers build their own user interface, such as TableLayout, VLayout, HLayout or BorderLayout. However, these layouts are not designed for mobile device, which leads to the question- how can we improve our user experience on mobile devices? One of the solutions is to rely on RWD frameworks such as Bootstrap. This article will show how to build a RWD ZK page with BorderLayout and Bootstrap.
 
 
 
= Demo =
 
Video:
 
<gflash width="640px" height="480px">UIPattern_bootstrap_borderlayout.swf</gflash>
 
 
 
Screenshot: desktop layout<br/>
 
[[File:UIPattern_bootstrap_borderlayout-0.png]]
 
 
 
<br/>
 
Screenshot: mobile layout<br/>
 
[[File:UIPattern_bootstrap_borderlayout-1.png]]
 
 
 
= Prerequisites =
 
* ZK 8.0.2 or later
 
* Bootstrap 3 knowledge, read [http://getbootstrap.com/getting-started/ here]
 
 
 
= Idea =
 
This demo shows a simple shopping cart. Since we expect the page to support RWD, we would need to design two different layout templates; one is for desktop browser written in ZK BorderLayout, and the other is for mobile device browser written in Bootstrap. We suggest using the same MVVM module to synchronize data and applying the new feature in ZK 8.0.2 called "MatchMedia" to switch each templates. In this case, you will then be able to define more templates for other devices in different screen sizes.
 
 
 
= Structure =
 
This image explains the relationship of two templates. You can see that each template's block corresponds to another template's block. The desktop template layout on the right side uses ZK Borderlayout, while the mobile template layout on the left side follows the standard Bootstrap layout rules.
 
[[File:UIPattern_bootstrap_borderlayout-2.png]]
 
 
 
*To learn more about Bootstrap layout grid system, please read [http://getbootstrap.com/css/#grid here]
 
 
 
= Main page =
 
We use a MVVM model to control which template will be displayed. The following contains the source code of the main zul page:
 
<source lang="xml">
 
<?meta name="viewport" content="width=device-width, initial-scale=1"?>
 
<zk>
 
    <div viewModel="@id('vm') @init('org.zkoss.zkshopcart_MVVM')" width="100%" height="100%">
 
        <apply template="@load(vm.layoutTemplate)"/>
 
    </div>
 
</zk>
 
</source>
 
 
 
The following are is MVVM java code. For now it simply displays the desktop layout. We will discuss how to detect which template will be displayed later in the section '''Media Queries'''.
 
 
 
<source lang="java">
 
public class zkshopcart_MVVM {
 
        private String _layoutTemplate = "desktop_layout" ;
 
 
 
        public String getLayoutTemplate() {
 
                return _layoutTemplate;
 
        }
 
}
 
</source>
 
 
 
= Desktop layout template =
 
For this demo, we use BorderLayout for the desktop layout template. We only use these four blocks: north, west, center and south (east block not required). The north is the header; the west block is a simple menu tree; the center block acts as the main block that contains the shopping cart items; and the south block represents the footer.
 
<source lang="xml">
 
<template name="desktop_layout">
 
    <borderlayout>
 
        <north>
 
            <apply templateURI="includes/header.zul"/>
 
        </north>
 
        <west width="160px">
 
            <apply templateURI="includes/menu.zul"/>
 
        </west>
 
        <center autoscroll="true">
 
            <apply templateURI="includes/shoplist.zul"/>
 
        </center>
 
        <south>
 
            <apply templateURI="includes/footer.zul"/>
 
        </south>
 
    </borderlayout>
 
</template>
 
</source>
 
 
 
= Mobile layout template =
 
Similar to the desktop layout template, the mobile layout template also has four blocks. These blocks follow the standard Bootstrap rules. The typical layout for mobile is in vertical order. Normally, the order of these blocks from top to bottom is header, menu, content and footer. Each Divs(North, West, Center and South) is a row, and the xxx_content inside each block is a full width column.
 
<source lang="xml">
 
<template name="mobile_layout">
 
    <div sclass="container-fluid">
 
        <div id="north" sclass="north row">
 
            <div id="north_content" sclass="col-xs-12 ">
 
                <apply templateURI="includes/header.zul"/>
 
            </div>
 
        </div>
 
        <div id="west" sclass="west row">
 
            <div id="west_content" sclass="col-xs-12">
 
                <apply templateURI="includes/menu.zul"/>
 
            </div>
 
        </div>
 
        <div id="center" sclass="center row">
 
            <div id="center-content" sclass="col-xs-12">
 
                <apply templateURI="includes/shoplist.zul"/>
 
            </div>
 
        </div>
 
        <div id="south" sclass="south row">
 
            <div id="south-content" sclass="col-xs-12">
 
                <apply templateURI="includes/footer.zul"/>
 
            </div>
 
        </div>
 
    </div>
 
</template>
 
</source>
 
 
 
Although the id is not essential, we add it to make the code more readable.
 
The sclass "container-fluid" at '''line 2''' tells Bootstrap this container is a full width container; "row" at '''line 3, 8, 13, 18''' indicates that this block is a row; and "col-xs-12" at '''line 4, 9, 14, 19''' means this block is a column with full width.
 
 
 
= Media Queries =
 
We use MatchMedia Queries defined the condition when the template should be switched, in this demo, if screen width greater than 640px will show desktop template, otherwise shows mobile template.
 
Remember to add '''@NotifyChange("*")''' after MatchMedia Queries to notify ZK update UI.
 
<source lang="java" high="4,10">
 
public class zkshopcart_MVVM {
 
    private String _layoutTemplate = "desktop_layout" ;
 
 
 
    @MatchMedia("all and (max-width : 640px)")
 
    @NotifyChange("*")
 
    public void getMobileTemplate(){
 
        _layoutTemplate = "mobile_layout";
 
    }
 
 
 
    @MatchMedia("all and (min-width : 640px)")
 
    @NotifyChange("*")
 
    public void getDesktopTemplate(){
 
        _layoutTemplate = "desktop_layout";
 
    }
 
 
 
    public String getLayoutTemplate() {
 
        return _layoutTemplate;
 
    }
 
}
 
</source>
 
Now your page has RWD and will auto switch template when screen width was changed.
 
 
 
= Some other tips =
 
Notice, if you want to use RWD feature (MatchMedia, Bootstrap) in your page, you must add a '''meta tag''' on top of the file to describe the viewport, it is essentially.
 
<source lang="xml" high="1">
 
<?meta name="viewport" content="width=device-width, initial-scale=1"?>
 
</source>
 
 
 
* More information of viewport meta, you can read [http://www.w3schools.com/css/css_rwd_viewport.asp here]
 
 
 
If you want to use Bootstrap style component appearance, maybe you need to use some css to override zk's original style, for instance, following css code clear ZK's button appearance, and you can add another Bootstrap button class on this:
 
<source lang="css">
 
.z-button.btn{
 
    background-image:inherit
 
}
 
.z-button.btn:hover,
 
.z-button.btn:focus{
 
    background-image:inherit
 
}
 
</source>
 

Latest revision as of 08:36, 8 June 2016