Detailed explanation of how to use JqGrid
JqGrid is a table plugin based on jQuery that can assist developers in quickly building powerful tables and data display interfaces. Below is a detailed explanation of how to use JqGrid.
- Include the necessary files: Add the jQuery library, JqGrid plugin file, and required style files to the HTML page.
<script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/free-jqgrid@4.15.6/dist/jquery.jqgrid.min.js"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/free-jqgrid@4.15.6/dist/css/ui.jqgrid.min.css">
- Define an HTML table container: Define a div container in an HTML page for displaying tables.
<div id="gridContainer"></div>
- jQuery is a fast and concise JavaScript library.
$(function() {
$("#gridContainer").jqGrid({
// 表格配置选项
url: "data.json", // 后台数据接口
datatype: "json", // 数据类型
colModel: [ // 列定义
{label: "姓名", name: "name"},
{label: "年龄", name: "age"},
{label: "性别", name: "gender"}
],
rowNum: 10, // 每页显示的记录数
rowList: [10, 20, 30], // 每页显示记录数选项
pager: "#gridPager", // 分页栏
height: 400, // 表格高度
autowidth: true, // 自动调整列宽
caption: "用户列表" // 表格标题
});
});
- Load data: By setting the options of url and datatype, data provided by the server can be loaded.
$("#gridContainer").jqGrid("setGridParam", {url: "data.json", datatype: "json"}).trigger("reloadGrid");
- Define other operations: You can define other operations such as sorting, filtering, and pagination using the methods provided by JqGrid.
$("#gridContainer").jqGrid("sortGrid", "name", true); // 根据姓名排序(升序)
$("#gridContainer").jqGrid("filterToolbar", {searchOnEnter: false}); // 显示筛选工具栏
$("#gridContainer").jqGrid("navGrid", "#gridPager", {edit: false, add: false, del: false}); // 显示导航栏(编辑、新增、删除操作)
This is a detailed explanation of how to use JqGrid. By simply configuring and operating it, you can achieve a powerful table display interface.