Search code examples
javascripthtmlcssmobileonsen-ui

Onsen UI - list resize


I am doing a mobile app based on Cordova and Onsen UI. My html page is divided in 3 sections. The first one is a chat box, which is filling 35% of the screen. Then i have a list(ons-list), which i want to fill 40% of the screen and at last i have a textbox and a button that fill 25% of the screen, both.

My problem is that i can't resize the list, and its list items, based on mobile screen resolution.

My idea is that i want the app to fill the entire screen without scroll.

Any help? thanks

<ons-template id="Main.html">
        <ons-page id="homepage">
            <ons-navigator var="myNavigator">
                <ons-toolbar id="topBar" fixed-style>
                    <div class="center"><font size="5">Main</font></div>
                    <div class="left">
                        <ons-toolbar-button ng-click="menu.toggleMenu()"><ons-icon icon="ion-navicon" style="font-size: 32px; width: 1em;"></ons-icon></ons-toolbar-button>
                    </div>
                </ons-toolbar>

                <ul id="conversationBox" class="ChatLog"></ul>
                <script>
                    //localStorage.clear();
                    //fillConversationBox();
                </script>
                <ons-list>
                    <ons-list-header>Messages</ons-list-header>
                    <ons-list-item class="list-item-container" modifier="chevron" onclick="myNavigator.pushPage('mensagens_predefinidas1.html');">
                        <ons-row>
                            <ons-col class="colImg">
                                <img class="thumbnail img_crop" src="images/ambulance48.png">
                            </ons-col>
                            <ons-col class="text_col">
                                Item 1
                            </ons-col>
                        </ons-row>
                    </ons-list-item>
                    <ons-list-item class="list-item-container" modifier="chevron" onclick="myNavigator.pushPage('mensagens_predefinidas2.html');">
                        <ons-row>
                            <ons-col class="colImg">
                                <img class="thumbnail img_crop" src="images/police48.png">
                            </ons-col>
                            <ons-col class="text_col">
                                Item 2
                            </ons-col>
                        </ons-row>
                    </ons-list-item>
                    <ons-list-item class="list-item-container" modifier="chevron" onclick="myNavigator.pushPage('mensagens_predefinidas3.html');">
                        <ons-row>
                            <ons-col class="colImg">
                                <img class="thumbnail img_crop" src="images/firetruck48.png">
                            </ons-col>
                            <ons-col class="text_col">
                                Item 3
                            </ons-col>
                        </ons-row>
                    </ons-list-item>
                    <ons-list-item class="list-item-container" modifier="chevron" onclick="myNavigator.pushPage('minhas_mensagens.html');">
                        <ons-row>
                            <ons-col class="colImg">
                                <img src="images/mymsgs.png" class="thumbnail">
                            </ons-col>
                            <ons-col class="text_col">
                                Item4
                            </ons-col>
                        </ons-row>
                    </ons-list-item>
                </ons-list>

                <div align="center" style="margin-bottom: 2%;margin-top: 4%; position: absolute; left: 0; right: 0;bottom: 0;">
                    <textarea id="messageTxtSend" rows="3" style="resize: none" placeholder="Introduzir mensagem"></textarea>
                    <button id="btnSend" class="button button--cta">Enviar</button>
                </div>
            </ons-navigator>
        </ons-page>
    </ons-template>


Solution

  • First of all, I would say it might not be a good idea to make the content not scrollable and adjust the height, if there are a lot of cells, because then the elements might become not readable on small phones. That being said, here is the solution:

    I created a jsfiddle for you https://jsfiddle.net/philolo1/m4gdu5s0/15/.

    The first trick is to use flexbox (flexbox.io/): The parent of the list is set to "display:flex" and all the kids have the same height "flex: 1". Here is the html file:

    <body> 
        <ons-page>
            <ons-toolbar modifier="material">
                <div class="center">Adjustable Height</div>
            </ons-toolbar>
            <div style="height: 35%; background: green;"></div>
            <ons-list  style="display: flex; flex: 1; flex-direction: column; height: 40%; background: blue;">
                <div id="container" style=" flex: 1; border: solid; border-color: white;"> 
                    <ons-list-item class="myItem"> Text 1 </ons-list-item> 
                </div>
                <div id="container" style=" flex: 1; border: solid; border-color: white;"> 
                    <ons-list-item class="myItem"> Text 2 </ons-list-item> 
                </div>
                <div id="container" style=" flex: 1; border: solid; border-color: white;"> 
                    <ons-list-item class="myItem"> Text 3 </ons-list-item> 
                </div>
            </ons-list>
            <div style="height: 25%; background: red;"></div>
        </ons-page>
    </body>
    

    In the Javascript one needs to update the css properties of the list-item to set it to the proper height (and also the font and other elements).

    ons.ready(function() {
        var fontSize = parseInt($("#container").height() *0.8)+"px";
        $(".myItem").css('min-height', 0);
        $(".myItem").css('line-height', fontSize);
        $(".myItem").css('font-size', fontSize);
    });