1. 程式人生 > >Easyui--datagrid 的使用 (彈出框的使用)

Easyui--datagrid 的使用 (彈出框的使用)

1

 $('#dlg').window('open');  //彈出這個dialog框

Home控制器

using DbService;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Script.Serialization;

namespace MvcAppEF.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index9()
        {
            return View();
        }
        public ActionResult getData()
        {
            JavaScriptSerializer jsz = new JavaScriptSerializer();
            salesEntities db = new salesEntities();
            int countData = db.T_UserInfo.Count();
            var list = db.T_UserInfo.ToList();

            var x = new { total = countData, rows = list };

            return Content(jsz.Serialize(x));
        }

        public ActionResult getUserInfo(int id)
        {
            JavaScriptSerializer jsz = new JavaScriptSerializer();
            salesEntities db = new salesEntities();
            int countData = db.T_UserInfo.Where(r => r.Id == id).ToList().Count();
            var list = db.T_UserInfo.Where(r => r.Id == id).ToList();

            var x = new { total = countData, rows = list };

            return Content(jsz.Serialize(x));
        }
    }


}

檢視

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index9</title>
    <script src="~/Scripts/jquery-1.8.2.js"></script>
    <script src="~/jquery-easyui/jquery.easyui.min.js"></script>
    <script src="~/jquery-easyui/jquery.easyui.min.js"></script>
    <script src="~/jquery-easyui/locale/easyui-lang-zh_CN.js"></script>
    <link href="~/jquery-easyui/themes/default/easyui.css" rel="stylesheet" />
    <link href="~/jquery-easyui/themes/icon.css" rel="stylesheet" />
</head>
<body>


    <table id="Cse_Bespeak_Log" class="easyui-datagrid" style="width: auto; height: 350px;">
        <thead>
            <tr>
                <!--可以詳寫,也可以簡寫,詳寫如:data-options="field:'Id'",簡寫如:field="Name"-->
                @*<th data-options="field:'Id'" align="center" width="100" sortable="true">
                        編號
                    </th>
                    <th field="UserName" align="center" width="120" sortable="true">
                        使用者名稱
                    </th>
                    <th field="Name" align="center" width="80" sortable="true">
                        姓名
                    </th>
                    <th field="Age" align="center" width="80" sortable="true">
                        年齡
                    </th>
                    <th field="Email" align="center" width="80" sortable="true">
                        郵箱
                    </th>*@
                @*<th align="center" width="120px">操作</th>*@
            </tr>
        </thead>
    </table>
  
    <!--這是一個彈出視窗easyui-dialog,我在它裡面放了一個datagrid-->
    <div id="dlg" class="easyui-dialog" style="width: 600px; height: auto; padding: 10px 20px"
         data-options="closed:true,buttons:'#dlg-buttons'">
        <table id="datagrid" class="easyui-datagrid" style="width:600px;height:150px">
            <thead>
                <tr>
                    <th data-options="field:'Id'" align="center" width="100" sortable="true">
                        編號
                    </th>
                    <th field="UserName" align="center" width="120" sortable="true">
                        使用者名稱
                    </th>
                    <th field="Name" align="center" width="80" sortable="true">
                        姓名
                    </th>
                    <th field="Age" align="center" width="80" sortable="true">
                        年齡
                    </th>
                    <th field="Email" align="center" width="80" sortable="true">
                        郵箱
                    </th>

                </tr>
            </thead>
        </table>
    </div>
    <div id="dlg-buttons">
        <a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-ok'" onclick="saveUser()">儲存</a>
        <a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-cancel'" onclick="javascript:$('#dlg').dialog('close')">關閉</a>
    </div>
    <input type="button" value="查詢詳情" onclick="" />
</body>
</html>
<script type="text/javascript">
    $(function () {



        $("#Cse_Bespeak_Log").datagrid({
            url: "/Home/getData",
            iconCls: "icon-add",
            fitColumns: false,
            loadMsg: "資料載入中......",
            pagination: true,
            rownumbers: true,
            nowrap: false,
            showFooter: true,
            singleSelect: true,
            pageList: [100, 50, 20, 10],

            columns: [[
                {
                    field: 'Id', title: '編號', width: 50, align: 'center',
                },
                {
                    field: 'UserName', title: '使用者名稱', width: 50, align: 'center',
                },
                {
                    field: 'Name', title: '姓名', width: 50, align: 'center',
                },
                {
                    field: 'opt', title: '操作', width: 100, align: 'center',
                    formatter: function (value, row,index) { //引數row表示當前行, 引數index表示當前行的索引值

                        //row.Id表示這個button按鈕所在的那一行的Id這個欄位的值
                        var btn = '<input type="button" id='+index+' value="查詢詳情"  onclick="return LoadUserInfo('+row.Id+')"/>';
                        return btn;
                    }
                }]]

        })

    })

    function LoadUserInfo(row) {

        /*獲取選中行*/
        //var row = $('#Cse_Bespeak_Log').datagrid('getSelected'); //獲取選中行  
        
        $("#datagrid").datagrid({
            url: "/Home/getUserInfo?id=" + row,
            iconCls: "icon-add",
            fitColumns: false,
            loadMsg: "資料載入中......",
            pagination: true,
            rownumbers: true,
            nowrap: false,
            showFooter: true,
            singleSelect: true,
            pageList: [100, 50, 20, 10],
        })


        $('#dlg').window('open');  //彈出這個dialog框
    };

</script>