


Detailed explanation of EasyUI's DataGrid binding Json data source method
EasyUI binds data to tables is the most common method. This article mainly introduces the sample code of EasyUI's DataGrid binding Json data source. The editor will share the two summary methods of binding tables, hoping to help to everyone.
The first type: the data is stored in the data set, each row corresponds to multiple values, and a loop is used to bind the data to the table
Front-end code:
<table id="dg" class="easyui-datagrid" style="width:100%;height:100%;" title="需要设置表格标题" data-options=" rownumbers:true, singleSelect:true, @*autoRowHeight:false,*@ pagination:true @*pageSize:10*@"> <thead> <tr> <th field="colum1">列1</th> <th field="colum2">列2</th> <th field="colum3">列3</th> <th field="colum4">列4</th> <th field="colum5">列5</th> <th field="colum6">列6</th> </tr> </thead> </table>
JS code:
(function ($) { function pagerFilter(data) { if ($.isArray(data)) { // is array data = { total: data.length, rows: data } } var target = this; var dg = $(target); var state = dg.data('datagrid'); var opts = dg.datagrid('options'); if (!state.allRows) { state.allRows = (data.rows); } if (!opts.remoteSort && opts.sortName) { var names = opts.sortName.split(','); var orders = opts.sortOrder.split(','); state.allRows.sort(function (r1, r2) { var r = 0; for (var i = 0; i < names.length; i++) { var sn = names[i]; var so = orders[i]; var col = $(target).datagrid('getColumnOption', sn); var sortFunc = col.sorter || function (a, b) { return a == b ? 0 : (a > b ? 1 : -1); }; r = sortFunc(r1[sn], r2[sn]) * (so == 'asc' ? 1 : -1); if (r != 0) { return r; } } return r; }); } var start = (opts.pageNumber - 1) * parseInt(opts.pageSize); var end = start + parseInt(opts.pageSize); data.rows = state.allRows.slice(start, end); return data; } var loadDataMethod = $.fn.datagrid.methods.loadData; var deleteRowMethod = $.fn.datagrid.methods.deleteRow; $.extend($.fn.datagrid.methods, { clientPaging: function (jq) { return jq.each(function () { var dg = $(this); var state = dg.data('datagrid'); var opts = state.options; opts.loadFilter = pagerFilter; var onBeforeLoad = opts.onBeforeLoad; opts.onBeforeLoad = function (param) { state.allRows = null; return onBeforeLoad.call(this, param); } var pager = dg.datagrid('getPager'); pager.pagination({ onSelectPage: function (pageNum, pageSize) { opts.pageNumber = pageNum; opts.pageSize = pageSize; pager.pagination('refresh', { pageNumber: pageNum, pageSize: pageSize }); dg.datagrid('loadData', state.allRows); } }); $(this).datagrid('loadData', state.data); if (opts.url) { $(this).datagrid('reload'); } }); }, loadData: function (jq, data) { jq.each(function () { $(this).data('datagrid').allRows = null; }); return loadDataMethod.call($.fn.datagrid.methods, jq, data); }, deleteRow: function (jq, index) { return jq.each(function () { var row = $(this).datagrid('getRows')[index]; deleteRowMethod.call($.fn.datagrid.methods, $(this), index); var state = $(this).data('datagrid'); if (state.options.loadFilter == pagerFilter) { for (var i = 0; i < state.allRows.length; i++) { if (state.allRows[i] == row) { state.allRows.splice(i, 1); break; } } $(this).datagrid('loadData', state.allRows); } }); }, getAllRows: function (jq) { return jq.data('datagrid').allRows; } }) })(jQuery);
$.ajax({ type: "get", //AJAX提交方式 url: "路径", datatype: "json", data: "userid=" + "id"+ "&username=" + "name", //向后台传递参数,无需传递参数就可以删除 success: function (data) { var rows = []; for (var i = 0; i < data.length; i++) { //data是返回值的集合 rows.push({ //把data数据对应的值压到rows对应数组中 colum1: data[i].userid, colum2: data[i].leve, colum3: data[i].Username, colum4: data[i].Tel, colum5: data[i].Mail, colum6: data[i].Explain }); } $('#dg').datagrid({ data: rows }).datagrid('clientPaging'); }, error: function () { //执行出错时执行的方法 $.messager.alert("操作提示", "表格失败,请联系管理员!", "warning"); } });
Called when a table needs to be bound AJAX method, after AJAX is executed, the display data method will be automatically called, and the table data will be displayed.
Second: Set the column names directly in the front desk and JS, and automatically bind them
Front desk code:
<table id="dg" class="easyui-datagrid" title="需要显示表格标题 " data-options=" rownumbers:true, singleSelect:true, autoRowHeight:false, pagination:true, "> <thead> <tr> <th data-options="field:'colum1',align:'center'">列名1</th> <th data-options="field:'colum2',align:'center'">列名2</th> <th data-options="field:'colum3',align:'center'">列名3</th> <th data-options="field:'colum4',align:'center'">列名4</th> <th data-options="field:'colum5',align:'center'">列名5</th> <th data-options="field:'colum6',align:'center'">列名6</th> </tr> </thead> </table>
JS code:
##
$('#dg').datagrid({ url: '路径?Name=' + Name + "&combox=" + combox, //设置访问后台路径和传递参数,如果没有参数可以删除 dataType: 'json', width: "100%", //宽度 striped: true, //把行条纹化(奇偶行背景色不同) idField: 'quesID', //标识字段 loadMsg: '正在加载用户的信息.......', //从远程站点加载数据是,显示的提示消息 pagination: true, //数据网格底部显示分页工具栏 singleSelect: false, //只允许选中一行 pageList: [10, 20, 30, 40, 50], //设置每页记录条数的列表 pageSize: 10, //初始化页面尺寸(默认分页大小) pageNumber: 1, //初始化页面(默认显示第一页) beforePageText: '第', //页数文本框前显示的汉字 afterPageText: '页 共 {pages} 页', displayMsg: '第{from}到{to}条,共{total}条', columns: [[ //每页具体内容 { field: 'colum1', title: '标题1', width: "13%", align: 'center', editor: 'text' }, { field: 'colum2', title: '标题2', width: "13%", align: 'center', editor: 'text' }, { field: 'colum3', title: '标题3', width: "13%", align: 'center', editor: 'text' }, { field: 'colum4', title: '标题4', width: "13%", align: 'center', editor: 'text' }, { field: 'colum5', title: '标题5', width: "13%", align: 'center', editor: 'text' }, { field: 'colum6', title: ' 标题6 ', width: "13%", align: 'center', editor: 'text' }, ]], onLoadSuccess: function (data) { //表格加载成功后执行的代码,如果不需要可以删除 } })
In-depth understanding of JSON data source format_javascript skills
Detailed explanation of 4 different forms of data source configuration
The above is the detailed content of Detailed explanation of EasyUI's DataGrid binding Json data source method. For more information, please follow other related articles on the PHP Chinese website!

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

How to use WebSocket and JavaScript to implement an online speech recognition system Introduction: With the continuous development of technology, speech recognition technology has become an important part of the field of artificial intelligence. The online speech recognition system based on WebSocket and JavaScript has the characteristics of low latency, real-time and cross-platform, and has become a widely used solution. This article will introduce how to use WebSocket and JavaScript to implement an online speech recognition system.

WebSocket and JavaScript: Key technologies for realizing real-time monitoring systems Introduction: With the rapid development of Internet technology, real-time monitoring systems have been widely used in various fields. One of the key technologies to achieve real-time monitoring is the combination of WebSocket and JavaScript. This article will introduce the application of WebSocket and JavaScript in real-time monitoring systems, give code examples, and explain their implementation principles in detail. 1. WebSocket technology

Introduction to how to use JavaScript and WebSocket to implement a real-time online ordering system: With the popularity of the Internet and the advancement of technology, more and more restaurants have begun to provide online ordering services. In order to implement a real-time online ordering system, we can use JavaScript and WebSocket technology. WebSocket is a full-duplex communication protocol based on the TCP protocol, which can realize real-time two-way communication between the client and the server. In the real-time online ordering system, when the user selects dishes and places an order

How to use WebSocket and JavaScript to implement an online reservation system. In today's digital era, more and more businesses and services need to provide online reservation functions. It is crucial to implement an efficient and real-time online reservation system. This article will introduce how to use WebSocket and JavaScript to implement an online reservation system, and provide specific code examples. 1. What is WebSocket? WebSocket is a full-duplex method on a single TCP connection.

JavaScript and WebSocket: Building an efficient real-time weather forecast system Introduction: Today, the accuracy of weather forecasts is of great significance to daily life and decision-making. As technology develops, we can provide more accurate and reliable weather forecasts by obtaining weather data in real time. In this article, we will learn how to use JavaScript and WebSocket technology to build an efficient real-time weather forecast system. This article will demonstrate the implementation process through specific code examples. We

JavaScript tutorial: How to get HTTP status code, specific code examples are required. Preface: In web development, data interaction with the server is often involved. When communicating with the server, we often need to obtain the returned HTTP status code to determine whether the operation is successful, and perform corresponding processing based on different status codes. This article will teach you how to use JavaScript to obtain HTTP status codes and provide some practical code examples. Using XMLHttpRequest

Usage: In JavaScript, the insertBefore() method is used to insert a new node in the DOM tree. This method requires two parameters: the new node to be inserted and the reference node (that is, the node where the new node will be inserted).

JavaScript is a programming language widely used in web development, while WebSocket is a network protocol used for real-time communication. Combining the powerful functions of the two, we can create an efficient real-time image processing system. This article will introduce how to implement this system using JavaScript and WebSocket, and provide specific code examples. First, we need to clarify the requirements and goals of the real-time image processing system. Suppose we have a camera device that can collect real-time image data
