Home Web Front-end JS Tutorial How to bind Json data source using EasyUI

How to bind Json data source using EasyUI

Jun 20, 2018 pm 03:22 PM

This article mainly introduces the sample code of EasyUI's DataGrid binding Json data source. The editor thinks it is quite good, so I will share it with you now and give it as a reference. Let’s follow the editor to take a look.

EasyUI is the most commonly used method to bind data to tables. The editor will share the two summarized methods of binding tables. Knowledge lies in accumulation.

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>
Copy after login

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(&#39;datagrid&#39;); 
    var opts = dg.datagrid(&#39;options&#39;); 
    if (!state.allRows) { 
      state.allRows = (data.rows); 
    } 
    if (!opts.remoteSort && opts.sortName) { 
      var names = opts.sortName.split(&#39;,&#39;); 
      var orders = opts.sortOrder.split(&#39;,&#39;); 
      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(&#39;getColumnOption&#39;, sn); 
          var sortFunc = col.sorter || function (a, b) { 
            return a == b ? 0 : (a > b ? 1 : -1); 
          }; 
          r = sortFunc(r1[sn], r2[sn]) * (so == &#39;asc&#39; ? 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(&#39;datagrid&#39;); 
        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(&#39;getPager&#39;); 
        pager.pagination({ 
          onSelectPage: function (pageNum, pageSize) { 
            opts.pageNumber = pageNum; 
            opts.pageSize = pageSize; 
            pager.pagination(&#39;refresh&#39;, { 
              pageNumber: pageNum, 
              pageSize: pageSize 
            }); 
            dg.datagrid(&#39;loadData&#39;, state.allRows); 
          } 
        }); 
        $(this).datagrid(&#39;loadData&#39;, state.data); 
        if (opts.url) { 
          $(this).datagrid(&#39;reload&#39;); 
        } 
      }); 
    }, 
    loadData: function (jq, data) { 
      jq.each(function () { 
        $(this).data(&#39;datagrid&#39;).allRows = null; 
      }); 
      return loadDataMethod.call($.fn.datagrid.methods, jq, data); 
    }, 
    deleteRow: function (jq, index) { 
      return jq.each(function () { 
        var row = $(this).datagrid(&#39;getRows&#39;)[index]; 
        deleteRowMethod.call($.fn.datagrid.methods, $(this), index); 
        var state = $(this).data(&#39;datagrid&#39;); 
        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(&#39;loadData&#39;, state.allRows); 
        } 
      }); 
    }, 
    getAllRows: function (jq) { 
      return jq.data(&#39;datagrid&#39;).allRows; 
    } 
  }) 
})(jQuery);
Copy after login
  $.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 
        }); 
      } 
      $(&#39;#dg&#39;).datagrid({ data: rows }).datagrid(&#39;clientPaging&#39;); 
    }, error: function () {            //执行出错时执行的方法 
      $.messager.alert("操作提示", "表格失败,请联系管理员!", "warning"); 
    } 
  });
Copy after login

Call the AJAX method when you need to bind the table. After AJAX is executed, the display data method will be automatically called, and the table data will be displayed.

Second: Set it up directly in the front desk and JS Column name, automatic binding

Front-end code:

<table id="dg" class="easyui-datagrid" title="需要显示表格标题 " data-options="         
        rownumbers:true, 
        singleSelect:true, 
        autoRowHeight:false, 
        pagination:true, 
        "> 
        <thead> 
          <tr> 
            <th data-options="field:&#39;colum1&#39;,align:&#39;center&#39;">列名1</th> 
            <th data-options="field:&#39;colum2&#39;,align:&#39;center&#39;">列名2</th> 
            <th data-options="field:&#39;colum3&#39;,align:&#39;center&#39;">列名3</th> 
            <th data-options="field:&#39;colum4&#39;,align:&#39;center&#39;">列名4</th> 
            <th data-options="field:&#39;colum5&#39;,align:&#39;center&#39;">列名5</th> 
            <th data-options="field:&#39;colum6&#39;,align:&#39;center&#39;">列名6</th> 
          </tr> 
        </thead> 
      </table>
Copy after login

JS code:

 $(&#39;#dg&#39;).datagrid({ 
    url: &#39;路径?Name=&#39; + Name + "&combox=" + combox,  //设置访问后台路径和传递参数,如果没有参数可以删除 
    dataType: &#39;json&#39;, 
    width: "100%", //宽度 
    striped: true, //把行条纹化(奇偶行背景色不同) 
    idField: &#39;quesID&#39;, //标识字段 
    loadMsg: &#39;正在加载用户的信息.......&#39;, //从远程站点加载数据是,显示的提示消息 
    pagination: true, //数据网格底部显示分页工具栏 
    singleSelect: false, //只允许选中一行 
    pageList: [10, 20, 30, 40, 50], //设置每页记录条数的列表 
    pageSize: 10, //初始化页面尺寸(默认分页大小) 
    pageNumber: 1, //初始化页面(默认显示第一页) 
    beforePageText: &#39;第&#39;, //页数文本框前显示的汉字  
    afterPageText: &#39;页 共 {pages} 页&#39;, 
    displayMsg: &#39;第{from}到{to}条,共{total}条&#39;, 
    columns: [[ //每页具体内容 
          { field: &#39;colum1&#39;, title: &#39;标题1&#39;, width: "13%", align: &#39;center&#39;, editor: &#39;text&#39; }, 
          { field: &#39;colum2&#39;, title: &#39;标题2&#39;, width: "13%", align: &#39;center&#39;, editor: &#39;text&#39; }, 
          { field: &#39;colum3&#39;, title: &#39;标题3&#39;, width: "13%", align: &#39;center&#39;, editor: &#39;text&#39; }, 
          { field: &#39;colum4&#39;, title: &#39;标题4&#39;, width: "13%", align: &#39;center&#39;, editor: &#39;text&#39; }, 
          { field: &#39;colum5&#39;, title: &#39;标题5&#39;, width: "13%", align: &#39;center&#39;, editor: &#39;text&#39; }, 
          { field: &#39;colum6&#39;, title: &#39; 标题6 &#39;, width: "13%", align: &#39;center&#39;, editor: &#39;text&#39; }, 
    ]], 
 
    onLoadSuccess: function (data) { 
 
      //表格加载成功后执行的代码,如果不需要可以删除 
    } 
  })
Copy after login

Just put the JS code in a function function, and the function will be executed Then the form can be bound to data

The above is what I compiled for everyone. I hope it will be helpful to everyone in the future.

Related articles:

Detailed introduction to controlled components and uncontrolled components in React

How to implement a basic shopping cart using Angular Function

Detailed introduction to routing and middleware in node.js

How to implement entry/leave animation in Vue

Detailed interpretation of the entry function run in webpack

The above is the detailed content of How to bind Json data source using EasyUI. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What should I do if I encounter garbled code printing for front-end thermal paper receipts? What should I do if I encounter garbled code printing for front-end thermal paper receipts? Apr 04, 2025 pm 02:42 PM

Frequently Asked Questions and Solutions for Front-end Thermal Paper Ticket Printing In Front-end Development, Ticket Printing is a common requirement. However, many developers are implementing...

Demystifying JavaScript: What It Does and Why It Matters Demystifying JavaScript: What It Does and Why It Matters Apr 09, 2025 am 12:07 AM

JavaScript is the cornerstone of modern web development, and its main functions include event-driven programming, dynamic content generation and asynchronous programming. 1) Event-driven programming allows web pages to change dynamically according to user operations. 2) Dynamic content generation allows page content to be adjusted according to conditions. 3) Asynchronous programming ensures that the user interface is not blocked. JavaScript is widely used in web interaction, single-page application and server-side development, greatly improving the flexibility of user experience and cross-platform development.

Who gets paid more Python or JavaScript? Who gets paid more Python or JavaScript? Apr 04, 2025 am 12:09 AM

There is no absolute salary for Python and JavaScript developers, depending on skills and industry needs. 1. Python may be paid more in data science and machine learning. 2. JavaScript has great demand in front-end and full-stack development, and its salary is also considerable. 3. Influencing factors include experience, geographical location, company size and specific skills.

How to merge array elements with the same ID into one object using JavaScript? How to merge array elements with the same ID into one object using JavaScript? Apr 04, 2025 pm 05:09 PM

How to merge array elements with the same ID into one object in JavaScript? When processing data, we often encounter the need to have the same ID...

Is JavaScript hard to learn? Is JavaScript hard to learn? Apr 03, 2025 am 12:20 AM

Learning JavaScript is not difficult, but it is challenging. 1) Understand basic concepts such as variables, data types, functions, etc. 2) Master asynchronous programming and implement it through event loops. 3) Use DOM operations and Promise to handle asynchronous requests. 4) Avoid common mistakes and use debugging techniques. 5) Optimize performance and follow best practices.

How to achieve parallax scrolling and element animation effects, like Shiseido's official website?
or:
How can we achieve the animation effect accompanied by page scrolling like Shiseido's official website? How to achieve parallax scrolling and element animation effects, like Shiseido's official website? or: How can we achieve the animation effect accompanied by page scrolling like Shiseido's official website? Apr 04, 2025 pm 05:36 PM

Discussion on the realization of parallax scrolling and element animation effects in this article will explore how to achieve similar to Shiseido official website (https://www.shiseido.co.jp/sb/wonderland/)...

The difference in console.log output result: Why are the two calls different? The difference in console.log output result: Why are the two calls different? Apr 04, 2025 pm 05:12 PM

In-depth discussion of the root causes of the difference in console.log output. This article will analyze the differences in the output results of console.log function in a piece of code and explain the reasons behind it. �...

The Evolution of JavaScript: Current Trends and Future Prospects The Evolution of JavaScript: Current Trends and Future Prospects Apr 10, 2025 am 09:33 AM

The latest trends in JavaScript include the rise of TypeScript, the popularity of modern frameworks and libraries, and the application of WebAssembly. Future prospects cover more powerful type systems, the development of server-side JavaScript, the expansion of artificial intelligence and machine learning, and the potential of IoT and edge computing.

See all articles