Home > Web Front-end > JS Tutorial > body text

jQuery plug-in Grid dynamically obtains method instances of columns and column fields

小云云
Release: 2018-01-23 16:12:47
Original
2608 people have browsed it

This article mainly introduces the method of jQuery plug-in jqGrid to dynamically obtain columns and column fields, and analyzes the related operation skills of table plug-in jqGrid for table field attributes in the form of examples. Friends who need it can refer to it. I hope it can help everyone.

1. Problem background

jqGrid table plug-in uses its own method to obtain the header and table fields of the table; obtain the column names and column field names and display them in the pop-up window. Use the check box to check

2. Implement the source code


<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
    <title>jqGrid动态获取列和列字段</title>
    <link rel="stylesheet" href="css/ui.jqgrid.css" rel="external nofollow" />
    <link rel="stylesheet" href="css/ui.jqgrid-bootstrap-ui.css" rel="external nofollow" />
    <link rel="stylesheet" href="css/bootstrap.css" rel="external nofollow" />
    <link rel="stylesheet" href="css/bootstrap-theme.css" rel="external nofollow" />
    <link rel="stylesheet" href="css/jquery-ui.css" rel="external nofollow" />
    <link rel="stylesheet" href="css/jquery-ui.theme.css" rel="external nofollow" />
    <script type="text/javascript" src="js/jquery-1.11.0.min.js" ></script>
    <script type="text/javascript" src="js/jquery-ui.js"></script>
    <script type="text/javascript" src="js/jquery.jqGrid.min.js" ></script>
    <script type="text/javascript" src="plugins/grid.setcolumns.js"></script>
    <style>
      th{
        border: 1px solid #ABABAB;
        line-height: 20px;
        vertical-align: middle;
      }
      td{
        line-height: 20px;
      }
    </style>
    <script>
      $(document).ready(function(){
        $("#jqTable").jqGrid({
          url:"data/student.json",
          height:380,
          datatype:"json",
          colNames:["序号","姓名","年龄","性别","QQ号","电话","地址"],
          colModel:[{
            name : &#39;id&#39;,
            index : &#39;id&#39;,
            label : &#39;序号&#39;,
            width : 60,
            align:&#39;center&#39;
          },{
            name : &#39;name&#39;,
            index : &#39;name&#39;,
            label : &#39;姓名&#39;,
            width : 120,
            align:&#39;center&#39;
          },{
            name : &#39;age&#39;,
            index : &#39;age&#39;,
            label : &#39;年龄&#39;,
            width : 120,
            align:&#39;center&#39;
          },{
            name : &#39;sex&#39;,
            index : &#39;sex&#39;,
            label : &#39;性别&#39;,
            width : 120,
            edittype : "select",
            formatter : &#39;select&#39;,
            editoptions : {
              value :&#39;0:男;1:女;&#39;
            },
            align:&#39;center&#39;
          },{
            name : &#39;qq&#39;,
            index : &#39;qq&#39;,
            label : &#39;QQ号&#39;,
            width : 120,
            align:&#39;center&#39;
          },{
            name : &#39;phone&#39;,
            index : &#39;phone&#39;,
            label : &#39;电话&#39;,
            width : 120,
            align:&#39;center&#39;
          },{
            name : &#39;address&#39;,
            index : &#39;address&#39;,
            label : &#39;地址&#39;,
            width : 200,
            align:&#39;center&#39;
          }],
          sortname : "id",
          sortorder : "desc",
          viewrecords : true,
          rownumbers:true,
          autowidth:true,
          jsonReader : {
            repeatitems : false
          }
        });
        var dialog = $("#dialog-column").dialog({
          autoOpen :false,
          modal : true,
          resizable : true,
          height: "auto",
          width: 400,
          align:&#39;center&#39;,
          buttons: {
            "确定": function() {
              $(this).dialog( "close" );
            },
            "关闭": function() {
              $(this).dialog( "close" );
            }
          }
        });
        $("#column").button().on("click", function() {
          dialog.dialog("open");
          //获取列名
          var colNames=$("#jqTable").jqGrid(&#39;getGridParam&#39;,&#39;colNames&#39;);
          //获取列字段
          var colModel=$("#jqTable").jqGrid(&#39;getGridParam&#39;,&#39;colModel&#39;);
          var table = "";
          var newColumnName = [];
          var newColumnValue = [];
          for (var i=0;i<colNames.length;i++)
          {
            var columnHidden = colModel[i].hidden;
            var columnName = colModel[i].name;
            if(columnHidden==false && columnName != "rn")
            {
              newColumnName.push(colNames[i]);
              newColumnValue.push(columnName);
            }
            console.info(columnName);
          }
          for(var j=0;j<newColumnName.length;j++)
          {
            if(j%5==0)
            {
              table += "<tr>";
            }
            table += "<td><input type=&#39;checkbox&#39; id=&#39;"+newColumnValue[j]+"&#39; name=&#39;column&#39; checked=&#39;checked&#39;><label for=&#39;"+newColumnValue[j]+"&#39;>"+newColumnName[j]+"</label></td>";
            if((j+1)%5==0)
            {
              table += "</tr>";
            }
          }
          $("#tableColumn").empty().append(table);
        });
      });
    </script>
  </head>
  <body>
    <p>
      <table id="jqTable" class="table"></table>
    </p>
    <p>
      <button id="column" type="button">显示</button>
    </p>
    <p id="dialog-column" title="设置列">
      <table id="tableColumn" style="width: 100%; height: 100px;">
      </table>
    </p>
  </body>
</html>
Copy after login

3. Implement the result

(1)Initialization

(2) Click the button

Related recommendations:

jQuery implements functional examples similar to GridView

JS quickly generates various grid layout tools Grid introduction

Example detailed explanation jqgrid implementation is simple Single line editing function


#

The above is the detailed content of jQuery plug-in Grid dynamically obtains method instances of columns and column fields. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template