JQueryEasyUI學習筆記(七)datagrid


歡迎大家轉載,轉載請注明出處!

希望這個筆記對自己和大家有用,但是本人水平有限,如果出錯的地方,希望大家指出,多多批評,謝謝!

今天說說這個datagrid框架的基本使用,這個框架一直以來都是大家比較頭疼的框架,尤其是Json數據的拼接,后台前台都很重要,使用這個框架,最重要的是仔細:

無需廢話,上代碼了:

<link href="jquery-easyui-1.3.2/themes/default/easyui.css" rel="stylesheet" type="text/css" />
        <!--easyui最全的樣式包也可單獨引用你想使用的樣式包-->
        <link href="jquery-easyui-1.3.2/themes/icon.css" rel="stylesheet" type="text/css" />
        <!--easyui自帶圖片樣式包,也可自行添加-->
        <script src="jquery-easyui-1.3.2/jquery-1.8.0.min.js" type="text/javascript"></script>
        <!--我使用的是easyui 1.3.2,基於jquery-1.8.0-->
        <script src="jquery-easyui-1.3.2/jquery.easyui.min.js" type="text/javascript"></script>
        <!--easyui的js包-->
        <script src="jquery-easyui-1.3.2/locale/easyui-lang-zh_CN.js" type="text/javascript"></script>
        <!--easyui的中文語言包,默認是英文-->
    </head>
<body id="layoutbody" class="easyui-layout">
    <div data-options="region:'north',title:'North Title',split:true" style="height: 100px;">
    </div>
    <div data-options="region:'south',title:'South Title',split:true" style="height: 100px;">
    </div>
    <div data-options="region:'east',iconCls:'icon-reload',title:'East',split:true" style="width: 100px;">
    </div>
    <div data-options="region:'west',title:'West',split:true" style="width: 100px;">
    </div>
    <div data-options="region:'center',title:'center title'" href="HTMLPage.htm" style="padding: 5px;
        background: #eee; overflow: hidden;"><!--這里指向了一個htm頁-->
    </div>
</body>
</html>

 

HTMLPage.htm代碼:

<script type="text/javascript" charst="utf-8">
    //因為layout框架指向href時,只取html頁面body中間的部分,所以該頁面這樣寫即可
    //有datagrid包含屬性較多,所以盡量使用js的方式初始化datagrid框架
    $(function () {
        $("#dg").datagrid({
            url: "GetJson.ashx", //指向一個一般處理程序或者一個控制器,返回數據要求是Json格式,直接賦值Json格式數據也可,我以demo中自帶的Json數據為例,就不寫后台代碼了,但是我會說下后台返回的注意事項
            title: "數據展示表格",
            iconCls: "icon-add",
            fitColumns: false, //設置為true將自動使列適應表格寬度以防止出現水平滾動,false則自動匹配大小
            //toolbar設置表格頂部的工具欄,以數組形式設置
            idField: 'id', //標識列,一般設為id,可能會區分大小寫,大家注意一下
            loadMsg: "正在努力為您加載數據", //加載數據時向用戶展示的語句
            pagination: true, //顯示最下端的分頁工具欄
            rownumbers: true, //顯示行數 1,2,3,4...
            pageSize: 10, //讀取分頁條數,即向后台讀取數據時傳過去的值
            pageList: [10, 20, 30], //可以調整每頁顯示的數據,即調整pageSize每次向后台請求數據時的數據
            //由於datagrid的屬性過多,我就不每個都介紹了,如有需要,可以看它的API
            sortName: "name", //初始化表格時依據的排序 字段 必須和數據庫中的字段名稱相同
            sortOrder: "asc", //正序
            columns: [[
                { field: 'code', title: 'Code', width: 100 },
                { field: 'name', title: 'Name', width: 100 ,sortable:true},//sortable:true點擊該列的時候可以改變升降序
                { field: 'addr', title: 'addr', width: 100,
                    //這里可以添加這樣一個方法,使其顯示數據得到改變
//                    formatter: function (value, row, index) {
//                        if (value == "0") {
//                            return "普通角色";
//                        } else {
//                            return "特殊角色";
//                        }
//                    }
                }
            ]]//這里之所以有兩個方括號,是因為可以做成水晶報表形式,具體可看demo
        });
    });
</script>
<div id="tt" class="easyui-tabs" style="width: 500px; height: 250px;" fit="true"
    border="false">
    <div title="Tab1" style="padding: 20px;" border="false">
        <table id="dg">
        </table>
    </div>
</div>

這是前台請求數據時發送的數據;

 

Json格式數據一定要是雙引號的,單引號無法顯示數據哦;

數據格式如下:

{                                                      
    "total":239,                                                      
    "rows":[                                                          
        {"code":"001","name":"Name 1","addr":"Address 11","col4":"col4 data"},         
        {"code":"002","name":"Name 2","addr":"Address 13","col4":"col4 data"},         
        {"code":"003","name":"Name 3","addr":"Address 87","col4":"col4 data"},         
        {"code":"004","name":"Name 4","addr":"Address 63","col4":"col4 data"},         
        {"code":"005","name":"Name 5","addr":"Address 45","col4":"col4 data"},         
        {"code":"006","name":"Name 6","addr":"Address 16","col4":"col4 data"},          
        {"code":"007","name":"Name 7","addr":"Address 27","col4":"col4 data"},          
        {"code":"008","name":"Name 8","addr":"Address 81","col4":"col4 data"},          
        {"code":"009","name":"Name 9","addr":"Address 69","col4":"col4 data"},          
        {"code":"010","name":"Name 10","addr":"Address 78","col4":"col4 data"}     
    ]                                                          
}                                                           

 

這里呢,后台傳遞數據很重要:


    注意:表格Post或者get回來的請求中
    page:3 代表page為key,然后選擇的當前頁碼為3
    rows:10 代表一頁的大小為10
    后台返回的數據的格式為:{total:'',rows:[{},{}]}  
    只要包含了總數tatol字段,rows是具體的行數
    例如:
    Asp.Net MVC 例子:
        public JsonResult GetAllUserInfos()
        {
            int pageSize = 5;
            int pageIndex = 1;
            int.TryParse(this.Request["page"], out pageIndex);
            int.TryParse(this.Request["rows"], out pageSize);

            pageSize = pageSize <= 0 ? 5 : pageSize;
            pageIndex = pageIndex < 1 ? 1 : pageIndex;

            var temp = db.UserInfo
                .OrderBy(u=>u.Sort)
                .Skip<UserInfo>((pageIndex-1)*pageSize)
                .Take<UserInfo>(pageSize)
                .ToList<UserInfo>();
            Hashtable ht = new Hashtable();
            ht["total"] = db.UserInfo.Count();
            ht["rows"] = temp;
            return Json(ht);
        }
        
    Asp.Net WebForm 例子:
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            var strWebName = context.Request["WebName"] ?? string.Empty;
            var  GoodsNo = context.Request["GoodsNo"] ?? string.Empty;
            int categoryId = 0;

            int pageIndex = 1;
            int pageSize = 10;

            int.TryParse(context.Request["rows"], out pageSize);
            int.TryParse(context.Request["page"], out pageIndex);

            decimal priceLeft = 0;
            decimal priceRight = 1000000;
            int goodsStatus = 0;
            decimal.TryParse(context.Request["PriceLeft"], out priceLeft);
            decimal.TryParse(context.Request["PriceRight"], out priceRight);
            int.TryParse(context.Request["CategoryId"], out categoryId);
            int.TryParse(context.Request["GoodsStatus"], out goodsStatus);
            var goodsQueryParamter = new GoodsQueryParamter();
            
           
            goodsQueryParamter.GoodsStatus = (Model.GoodsModel.GoodsStatusEnum)goodsStatus;

            var ds = goodsService.GetGoodsList(goodsQueryParamter);
            string json = string.Empty;           

            if (ds != null && ds.Tables.Count > 0)
            {
                System.Text.StringBuilder rowJson = new System.Text.StringBuilder();
                int colLen = ds.Tables[0].Columns.Count;
                DataColumnCollection col = ds.Tables[0].Columns;
                foreach (DataRow row in ds.Tables[0].Rows)
                {
                    System.Text.StringBuilder colJson = new System.Text.StringBuilder();
                    rowJson.Append("{");
                    for (int i = 0; i < colLen; i++)
                    {
                        colJson.Append("\"" + col[i].ColumnName + "\":\"" + row[i].ToString() + "\",");
                    }
                    rowJson.Append(colJson.ToString().TrimEnd(','));
                    rowJson.Append("},");
                }
                json = "{\"total\":" + ds.Tables[0].Rows[0]["sumGoods"] + ",\"rows\":[" + rowJson.ToString().TrimEnd(',') + "]}";
            }
            context.Response.Write(json);
        }

ASP.Net中有一個類也可以序列化Json格式數據;

 

今天很是崩潰,用谷歌瀏覽器,連續兩次崩潰了,我擦,直接惡心到死!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM