代码篇——DataGrid


一:DataGrid如何绑定controller中的数据进行显示


view代码:


以下代码为easyUI,所以需要引用easyui中的js,才能正常显示。


<span style="font-size:18px;"><span style="font-size:18px;"><div id="examInfomation" style="text-align:center;margin-left:200px;">
    
                <table id="Chapter1" class="easyui-datagrid" title="考试信息" style="width:600px; height: 300px;" data-options="url:'/AddExamPlace/QueryExamPlace'", fitColumns:true >
                   <thead>
                        <tr>
                            <th data-options="field:'ck',checkbox:true"></th>
                            <th data-options="field:'ExamPlaceID',width:150">考场ID</th>
                            <th data-options="field:'ExamPlace',width:100">考场地点</th>
                            <th data-options="field:'Examiner',width:100">监考老师</th>
                            <th data-options="field:'StartTime',width:100">开始时间</th>
                            <th data-options="field:'EndTime',width:100">结束时间</th>
                        </tr>
                    </thead>   
           </table>
    </div></span></span>



Controllers中的代码:


方法一:


需要引用System.Web.Script.Serialization;


<span style="font-size:18px;"><span style="font-size:18px;"> public string QueryExamPlace()
        {

            List<ExamDetailsViewModel> queryAllExamPlace = new List<ExamDetailsViewModel>();
            queryAllExamPlace = IexamdetailsServiceBll.queryExamPlace();
            JavaScriptSerializer servializer = new JavaScriptSerializer(); // 自定义类型解析程序对象。
            var tempType = from c in queryAllExamPlace
                           select new
                           {
                               ExamPlaceID = c.ExamPlaceID,
                               ExamPlace = c.ExamPlace,
                               Examiner = c.Examiner,
                               StartTime = c.StartTime,
                               EndTime = c.EndTime,
                           };
            return servializer.Serialize(tempType); //返回序列化对象    
        }</span></span>



方法二:


<span style="font-size:18px;"><span style="font-size:18px;"> public ActionResult QueryExamPlace()
        {
            List<ExamDetailsViewModel> QueryExamPlace = IexamdetailsServiceBll.queryExamPlace(); ;

            return Json(QueryExamPlace, JsonRequestBehavior.AllowGet);
        }</span></span>

      通过页面上的一个url,来实现绑定controller中传过来的数据,忽然觉得好方便。



二:JS中创建表格,页面显示数据


View的html代码:


<span style="font-size:18px;"><span style="font-size:18px;">  <div id="examInfomation" style="text-align:center;margin-left:200px;">
    
                <table id="Chapter1" class="easyui-datagrid" title="考试信息" style="width:600px; height: 300px;" data-options="url:'/AddExamPlace/QueryExamPlace'", fitColumns:true >
                   <thead>
                       
                    </thead>   
           </table></span></span>



JS代码:


<span style="font-size:18px;"><span style="font-size:18px;">  function loadGrid() {
            //加载数据  
            $('#Chapter1').datagrid({
                width: 'auto',
                height: 300,
                striped: true,
                singleSelect: true,
                url: '/AddExamPlace/QueryExamPlace',
                loadMsg: '数据加载中请稍后……',
                pagination: true,
                rownumbers: true,

                fit: true,//自动补全  
                columns: [[
                    { field: 'ExamPlaceID', title: '考场ID', align: 'center', width:100  },
                    { field: 'Examiner', title: '监考老师', align: 'center', width:100  },
                    { field: 'StartTime', title: '结束时间', align: 'center', width:100 }
                ]]
            });
        }</span></span>



       通过JS让页面显示数据,直接在一个js中调用loadGrid()方法即可。这样的话,表格得到了重复利用。



三:怎么样在执行一个添加之后动态更新表格


        可以重新调用的表格的JS,也可以执行datagrid的reload事件   $("#Chapter1").datagrid('reload');


       

四:foreach加载表格内容



control中的代码:


<span style="font-size:18px;">  public ActionResult ExcelQuestionRecord(string papertype)
        {
            IExcelQuestionRecordService Excelquestionrecord = SpringHelper.GetObject<IExcelQuestionRecordService>("ExcelQuestionRecordService");
            papertype = "A";
            List<ExcelQuestionRecordEntity> ExcelQuestion = new List<ExcelQuestionRecordEntity>();
            //StringBuilder answeringCardHtml = new StringBuilder();
            ExcelQuestion = Excelquestionrecord.ShowExcelInformation(papertype);
            ViewData["DataList"] = ExcelQuestion;
            return View();
        }
</span>


        通过ViewData绑定了返回来的值,并传到view中。



view中的代码:


<span style="font-size:18px;">@using NCREModel;
@{
    ViewBag.Title = "ExcelQuestionRecord";
}

<h2>QuestionRecord</h2>

<div>
    <table id="tbList">
        <tr>
            <th>题号</th>
            <th>学生学号</th>
            <th>试卷类型</th>
            <th>考题内容</th>
            <th>正确答案</th>
            <th>考生答案</th>
            <th>分数</th>
            <th>时间</th>
        </tr>
        @foreach (ExcelQuestionRecordEntity a in ViewData["DataList"] as List<ExcelQuestionRecordEntity>)  
        { 
            <tr>
                 <td>@a.QuestionID</td>
                 <td>@a.StudentID</td>
                 <td>@a.PaperType</td>
                 <td>@a.QuestionContent</td>
                 <td>@a.CorrectAnswer</td>
                 <td>@a.ExamAnswer</td>
                 <td>@a.Fration</td>
                 <td>@a.TimeStamp</td>
            </tr>
        }
    </table>     
</div>

</span>

        需要在最前面引用model,这个需要注意。


    

        代码积累,多多益善。大家可以在官网上了解更多datagrid的知识。EasyUI中的Datagrid。。。










  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 28
    评论
以下是一个简单的使用 EasyUI 的 datagrid 组件的例子: ```html <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Datagrid Example</title> <link rel="stylesheet" type="text/css" href="https://cdn.bootcdn.net/ajax/libs/jquery-easyui/1.9.0/themes/default/easyui.css"> <link rel="stylesheet" type="text/css" href="https://cdn.bootcdn.net/ajax/libs/jquery-easyui/1.9.0/themes/icon.css"> <script type="text/javascript" src="https://cdn.bootcdn.net/ajax/libs/jquery/1.8.0/jquery.min.js"></script> <script type="text/javascript" src="https://cdn.bootcdn.net/ajax/libs/jquery-easyui/1.9.0/jquery.easyui.min.js"></script> </head> <body> <table id="dg" class="easyui-datagrid" style="width:100%;height:300px" url="data.json" toolbar="#toolbar" pagination="true" rownumbers="true" fitColumns="true" singleSelect="true"> <thead> <tr> <th field="id" width="50">ID</th> <th field="name" width="50">Name</th> <th field="age" width="50" align="right">Age</th> <th field="address" width="50">Address</th> </tr> </thead> </table> <div id="toolbar"> <a href="#" class="easyui-linkbutton" iconCls="icon-add" plain="true" onclick="newUser()">New User</a> <a href="#" class="easyui-linkbutton" iconCls="icon-edit" plain="true" onclick="editUser()">Edit User</a> <a href="#" class="easyui-linkbutton" iconCls="icon-remove" plain="true" onclick="destroyUser()">Remove User</a> </div> <script> function newUser(){ $('#dlg').dialog('open').dialog('setTitle','New User'); $('#fm').form('clear'); url = 'save_user.php'; } function editUser(){ var row = $('#dg').datagrid('getSelected'); if (row){ $('#dlg').dialog('open').dialog('setTitle','Edit User'); $('#fm').form('load',row); url = 'update_user.php?id='+row.id; } } function saveUser(){ $('#fm').form('submit',{ url: url, onSubmit: function(){ return $(this).form('validate'); }, success: function(result){ var result = eval('('+result+')'); if (result.errorMsg){ $.messager.show({ title: 'Error', msg: result.errorMsg }); } else { $('#dlg').dialog('close'); // close the dialog $('#dg').datagrid('reload'); // reload the user data } } }); } function destroyUser(){ var row = $('#dg').datagrid('getSelected'); if (row){ $.messager.confirm('Confirm','Are you sure you want to destroy this user?',function(r){ if (r){ $.post('destroy_user.php',{id:row.id},function(result){ if (result.success){ $('#dg').datagrid('reload'); // reload the user data } else { $.messager.show({ // show error message title: 'Error', msg: result.errorMsg }); } },'json'); } }); } } </script> </body> </html> ``` 上述代码展示了一个具有分页、行号、自适应列宽等功能的 datagrid,其中数据来自于 data.json 文件,通过 toolbar 中的按钮可以增加、编辑和删除表格中的数据。
评论 28
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值