


A brief discussion on MVC+EF easyui dataGrid dynamic loading of paging tables
First, enter the javascript code
<script type="text/javascript"> $(function () { LoadGrid(); }) //加载表格!!! function LoadGrid() { $('#roleGrid').datagrid({ width: 900, striped: true, //交替条纹 fitColumns: true, //防止水平滚动 fit: true,//自动补全 iconCls: "icon-save",//图标 idField: 'RoleId', //唯一列 url: "GetRoles", dataType: "json", singleSelect: true, //设置为true将只允许选择一行 loadMsg: '正在拼命加载,请稍后...', rownumbers: false, //显示行数 pagination: true, //底部分页工具栏 nowrap: true, //截取超出部分的数据 checkOnSelect: true,//点击一行的时候 checkbox checked(选择)/unchecked(取消选择) pageNumber: 1,//初始化分页码。 pageSize: 10, //初始化每页记录数。 pageList: [5, 10, 30], //初始化每页记录数列表 showFooter: false, //定义是否显示行底 columns: [[ { field: "RoleId", title: "角色编号", width: 60, align: "center", sortable: "true" }, { field: "RoleName", title: "角色名称", width: 100, align: "center" }, { field: "RoleRemarks", title: "备注", width: 100, align: "center" }, { field: "IsStatus", title: "状态", width: 60, align: "center", formatter: function (value, row, index) { if (value == "0") { return "正常"; } else if (value == "1") { return "停用"; } } }, { field: "edit", title: "操作", align: "center", width: 80, formatter: function (value, row, index) { var detail = '<a style="padding:1px;color:black;" onclick="editRole(' + index + ')"><i class="fa fa-edit"></i>编辑</a>'; var deleteBtn = '<a style="color:black;" onclick="delRole(' + index + ')"><i class="fa fa-trash-o"></i>删除</>'; var setrole = '<a style="color:black;" onclick="setRights(' + index + ')"><i class="fa fa-exclamation-triangle"></i>设置权限</>'; return " " + detail + " | " + deleteBtn + " | " + setrole; } } ]] //列 }); }; function editRole(i) { //编辑按钮的方法 var rows = $("#roleGrid").datagrid("getRows"); layer.open({ title: false, type: 2, closeBtn: false, area: ['420px', '418px'], skin: 'layui-layer-rim', //加上边框 content: ['/Admin/ShowForm/EidtRole', 'no'], success: function (layero, index) { var body = layer.getChildFrame('body', index); body.contents().find("#roleId").val(rows[i].RoleId); body.contents().find("#roleName").val(rows[i].RoleName); if (rows[i].RoleRemarks != "-") { body.contents().find("#remarks").val(rows[i].RoleRemarks); } body.contents().find("#isstutas").val(rows[i].IsStatus); } }); } function delRole(i) { //删除用户 var rows = $("#roleGrid").datagrid("getRows"); var postData = { roleId: rows[i].RoleId }; layer.confirm('确认删除该角色?', { btn: ['确认', '取消'], //按钮 shade: false //不显示遮罩 }, function (index) { $.ajax({ type: "POST", url: "DeleRole", data: postData, success: function (result) { if (result == "true") { layer.msg("操作成功!", { icon: 6, time: 1000, }, function () { $("#roleGrid").datagrid("reload"); layer.close(index); }); } else if (result == "false") { layer.msg("操作失败!", { icon: 2 }); } else if (result == "msg") { layer.msg("系统错误,请联系管理员!", { icon: 0 }); } } }); }, function (index) { layer.close(index); }); } 然后是html <table id="roleGrid"> </table>
Finally, there are the controller methods (this part is the most important, whether the table can display data depends on this part)
/// <summary> /// 动态生成表格的数据 /// </summary> /// <param name="page"></param> /// <param name="rows"></param> /// <returns></returns> public JsonResult GetRoles(int? page, int? rows) { page = page == null ? 1 : page; //第几页 rows = rows == null ? 1 : rows; //行数 List<role> rList = rService.GetAllRoles(Convert.ToInt32(page), Convert.ToInt32(rows)); List<role> roleList = new List<role>(); for (int i = 0; i < rList.Count; i++) { role r = new role(); r.RoleId = rList[i].RoleId; r.RoleName = rList[i].RoleName; if (string.IsNullOrEmpty(rList[i].RoleRemarks)) { r.RoleRemarks = "-"; } else { r.RoleRemarks = rList[i].RoleRemarks; } r.IsStatus = rList[i].IsStatus; roleList.Add(r); } var json = new { total = rService.GetTotal(), rows = roleList }; return Json(json, JsonRequestBehavior.AllowGet); }
The last one is the controller-related methods
/// <summary> /// 分页的数据 /// </summary> /// <param name="page"></param> /// <param name="rows"></param> /// <returns></returns> public List<role> GetAllRoles(int page, int rows) { using (diamondEntities entity = new diamondEntities()) { IQueryable<role> role = entity.roles.OrderBy(a => a.RoleId).Skip((page - 1) * rows).Take(rows); List<role> roleList = role.ToList<role>(); if (roleList.Count > 0) { return roleList; } else { return null; } } } /// <summary> /// 获取总页数 /// </summary> /// <returns></returns> public int GetTotal() { using (diamondEntities entity = new diamondEntities()) { IQueryable<role> user = entity.roles.Select(m => m); List<role> userList = user.ToList(); return userList.Count; } }

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Introduction In today's rapidly evolving digital world, it is crucial to build robust, flexible and maintainable WEB applications. The PHPmvc architecture provides an ideal solution to achieve this goal. MVC (Model-View-Controller) is a widely used design pattern that separates various aspects of an application into independent components. The foundation of MVC architecture The core principle of MVC architecture is separation of concerns: Model: encapsulates the data and business logic of the application. View: Responsible for presenting data and handling user interaction. Controller: Coordinates the interaction between models and views, manages user requests and business logic. PHPMVC Architecture The phpMVC architecture follows the traditional MVC pattern, but also introduces language-specific features. The following is PHPMVC

The MVC architecture (Model-View-Controller) is one of the most popular patterns in PHP development because it provides a clear structure for organizing code and simplifying the development of WEB applications. While basic MVC principles are sufficient for most web applications, it has some limitations for applications that need to handle complex data or implement advanced functionality. Separating the model layer Separating the model layer is a common technique in advanced MVC architecture. It involves breaking down a model class into smaller subclasses, each focusing on a specific functionality. For example, for an e-commerce application, you might break down the main model class into an order model, a product model, and a customer model. This separation helps improve code maintainability and reusability. Use dependency injection

SpringMVC framework decrypted: Why is it so popular, specific code examples are needed Introduction: In today's software development field, the SpringMVC framework has become a very popular choice among developers. It is a Web framework based on the MVC architecture pattern, providing a flexible, lightweight, and efficient development method. This article will delve into the charm of the SpringMVC framework and demonstrate its power through specific code examples. 1. Advantages of SpringMVC framework Flexible configuration method Spr

The MVC (Model-View-Controller) pattern is a commonly used software design pattern that can help developers better organize and manage code. The MVC pattern divides the application into three parts: Model, View and Controller, each part has its own role and responsibilities. In this article, we will discuss how to implement the MVC pattern using PHP. Model A model represents an application's data and data processing. usually,

How to implement a scalable MVC architecture in the PHP8 framework Introduction: With the rapid development of the Internet, more and more websites and applications adopt the MVC (Model-View-Controller) architecture pattern. The main goal of MVC architecture is to separate different parts of the application in order to improve the maintainability and scalability of the code. In this article, we will introduce how to implement a scalable MVC architecture in the PHP8 framework. 1. Understand the MVC architecture pattern. The MVC architecture pattern is a software design

In Web development, MVC (Model-View-Controller) is a commonly used architectural pattern for processing and managing an application's data, user interface, and control logic. As a popular web development language, PHP can also use the MVC architecture to design and build web applications. This article will introduce how to use MVC architecture to design projects in PHP, and explain its advantages and precautions. What is MVCMVC is a software architecture pattern commonly used in web applications. MV

Developing MVC with PHP8 framework: Important concepts and techniques that beginners need to know Introduction: With the rapid development of the Internet, Web development plays an important role in today's software development industry. PHP is widely used for web development, and there are many mature frameworks that help developers build applications more efficiently. Among them, the MVC (Model-View-Controller) architecture is one of the most common and widely used patterns. This article will introduce how beginners can use the PHP8 framework to develop MVC applications.

Model-view-controller (mvc) architecture is a powerful design pattern for building maintainable and scalable WEB applications. The PHPMVC architecture decomposes application logic into three distinct components: Model: represents the data and business logic in the application. View: Responsible for presenting data to users. Controller: Acts as a bridge between the model and the view, handling user requests and coordinating other components. Advantages of MVC architecture: Code separation: MVC separates application logic from the presentation layer, improving maintainability and scalability. Reusability: View and model components can be reused across different applications, reducing code duplication. Performance Optimization: MVC architecture allows caching of view and model results, thus increasing website speed. Test Friendly: Detachment
