基於MVC4+EasyUI的Web開發架構經驗總結(13)--DataGrid控制項實現自動適應寬頻高度

來源:互聯網
上載者:User

標籤:style   blog   http   io   color   ar   os   使用   java   

在預設情況下,EasyUI的DataGrid好像都沒有具備自動寬度的適應功能,一般是指定像素寬度的,但是使用的人員電腦的螢幕解析度可能不一樣,因此導致有些地方顯示太大或者太小,總是不能達到好的預期效果,如果DataGrid能夠根據視窗尺寸進行伸縮,效果應該好很多。本文主要介紹DataGrid控制項實現自動適應寬頻高度的操作。

首先我們需要定義一個resizeDataGrid的擴充函數,方便在頁面裡面進行調用,擴充函數定義如下所示。

//datagrid寬度高度自動調整的函數$.fn.extend({    resizeDataGrid: function (heightMargin, widthMargin, minHeight, minWidth) {        var height = $(document.body).height() - heightMargin;        var width = $(document.body).width() - widthMargin;        height = height < minHeight ? minHeight : height;        width = width < minWidth ? minWidth : width;        $(this).datagrid(‘resize‘, {            height: height,            width: width        });    }});

 定義好上面的函數後,我們就可以在頁面裡面使用Javascript進行調用,調用方法如下所示:$(‘#grid‘).resizeDataGrid。

        var heightMargin = $("#toolbar").height() + 60;        var widthMargin = $(document.body).width() - $("#tb").width();        // 第一次載入時和當視窗大小發生變化時,自動變化大小        $(‘#grid‘).resizeDataGrid(heightMargin, widthMargin, 0, 0);        $(window).resize(function () {            $(‘#grid‘).resizeDataGrid(heightMargin, widthMargin, 0, 0);        });

通過上面的代碼,我們就可以定義兩個高度、寬度的邊界,但是這些我們不應該固定化,應該通過一些介面代碼的對象動態擷取邊框大小。

 HTML代碼如下所示。

    <div id="tb" style="padding:5px;height:auto">        <!-------------------------------搜尋方塊----------------------------------->        <fieldset>            <legend>資訊查詢</legend>            <form id="ffSearch" method="post">                <div id="toolbar">                    <table cellspacing="0" cellpadding="0">                        <tr>                             <th>                                <label for="txtProvinceName">省份名稱:</label>                            </th>                            <td>                                <input type="text" ID="txtProvinceName" name="txtProvinceName" style="width:100px"  />                            </td>                            <td colspan="2">                                <a href="#" class="easyui-linkbutton" data-options="iconCls:‘icon-search‘" id="btnSearch">查詢</a>                                <a href="javascript:void(0)" class="easyui-linkbutton" id="btnImport" iconcls="icon-excel" onclick="ShowImport()">匯入</a>                                <a href="javascript:void(0)" class="easyui-linkbutton" id="btnExport" iconcls="icon-excel" onclick="ShowExport()">匯出</a>                            </td>                          </tr>                    </table>                </div>            </form>        </fieldset>                        <!-------------------------------詳細資料展示表格----------------------------------->        <table id="grid" style="width: 940px" title="使用者操作" data-options="iconCls:‘icon-view‘">                    </table>    </div>

這個介面效果如下所示。

其他類似的介面類似效果如下所示。

對比上面的介面,下面的介面增加了左邊一個面板,這裡的代碼也不需要特殊的設定。

            var heightMargin = $("#toolbar").height() + 40;            var widthMargin = $(document.body).width() - $("#tb").width() + 20;            // 第一次載入時和當視窗大小發生變化時,自動變化大小            $(‘#grid‘).resizeDataGrid(heightMargin, widthMargin, 0, 0);            $(window).resize(function () {                $(‘#grid‘).resizeDataGrid(heightMargin, widthMargin, 0, 0);            });

上面的代碼也只是根據效果進行了一些微調,基本和第一部分的設定寬度代碼差不多。

基於MVC4+EasyUI的Web開發架構經驗總結(13)--DataGrid控制項實現自動適應寬頻高度

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.