以下是以资深软件开发工程师的视角,对“jQuery EasyUI 数据网格 – 添加工具栏(Toolbar)”的专业回答。我将详细讲解如何在 jQuery EasyUI 的数据网格(DataGrid)中添加工具栏,包括基础用法、高级配置和动态应用,包含目录、小标题、代码示例和内部链接,确保内容清晰、技术准确且易于理解。回答基于截至 2025 年 3 月 16 日的最新信息,使用 jQuery EasyUI 的最新稳定版(1.10.x)为参考。


jQuery EasyUI 数据网格 – 添加工具栏(Toolbar)

目录

  1. 引言
  2. 工具栏概述
  1. 实现基础工具栏
  1. 高级工具栏配置
  1. 最佳实践与注意事项
  1. 结论

1. 引言

jQuery EasyUI 的数据网格(DataGrid)通过工具栏(Toolbar)为用户提供操作入口,增强表格的交互性。无论是简单的按钮还是复杂的控件组合,工具栏都能显著提升用户体验。本教程将从基础到高级,带你掌握如何在 DataGrid 中添加工具栏,并提供实用示例。


2. 工具栏概述

2.1 什么是工具栏?

  • 定义:工具栏是 DataGrid 的顶部区域,通过 toolbar 属性配置,用于放置按钮、输入框或其他控件,提供数据操作功能。
  • 实现方式:支持 HTML 元素引用(如 #id)或 JavaScript 对象数组定义。

2.2 工具栏的应用场景

  • 数据管理:如添加、编辑、删除行。
  • 辅助功能:如搜索、刷新或导出数据。
  • 参考:jQuery EasyUI 官方文档 DataGrid Toolbar 部分(jeasyui.com)。

3. 实现基础工具栏

3.1 准备工作

  • 引入依赖:需要 jQuery、EasyUI 的核心文件和主题文件。
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script src="https://www.jeasyui.com/easyui/jquery.easyui.min.js"></script>
<link rel="stylesheet" href="https://www.jeasyui.com/easyui/themes/default/easyui.css">
<link rel="stylesheet" href="https://www.jeasyui.com/easyui/themes/icon.css">

3.2 添加基础工具栏

  • 方法:通过 toolbar 属性指定一个 HTML 元素的 ID,内嵌按钮或其他控件。
  • 示例
<div id="tb">
  <a href="#" class="easyui-linkbutton" onclick="alert('添加')">添加</a>
</div>
<table id="dg" class="easyui-datagrid" toolbar="#tb"></table>

3.3 实例:基础工具栏

以下是一个基础工具栏的完整示例:

<!DOCTYPE html>
<html lang="zh">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>jQuery EasyUI 基础工具栏</title>
  <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
  <script src="https://www.jeasyui.com/easyui/jquery.easyui.min.js"></script>
  <link rel="stylesheet" href="https://www.jeasyui.com/easyui/themes/default/easyui.css">
  <link rel="stylesheet" href="https://www.jeasyui.com/easyui/themes/icon.css">
  <style>
    body { padding: 20px; }
  </style>
</head>
<body>
  <div class="container">
    <h2>基础工具栏</h2>
    <div id="tb">
      <a href="#" class="easyui-linkbutton" iconCls="icon-add" plain="true" onclick="addRow()">添加</a>
      <a href="#" class="easyui-linkbutton" iconCls="icon-reload" plain="true" onclick="reloadGrid()">刷新</a>
    </div>
    <table id="dg" class="easyui-datagrid" style="width: 600px; height: 300px;" toolbar="#tb"></table>
  </div>
  <script>
    var data = [
      { id: 1, name: '项目 A', price: 100 },
      { id: 2, name: '项目 B', price: 200 }
    ];

    $('#dg').datagrid({
      data: data,
      columns: [[
        { field: 'id', title: 'ID', width: 100 },
        { field: 'name', title: '名称', width: 200 },
        { field: 'price', title: '价格', width: 150 }
      ]]
    });

    function addRow() {
      $('#dg').datagrid('appendRow', { id: data.length + 1, name: '新项目', price: 0 });
      data = $('#dg').datagrid('getData').rows;
      $.messager.show({ title: '提示', msg: '已添加新行', timeout: 2000, showType: 'slide' });
    }

    function reloadGrid() {
      $('#dg').datagrid('reload');
      $.messager.show({ title: '提示', msg: '表格已刷新', timeout: 2000, showType: 'slide' });
    }
  </script>
</body>
</html>
  • 运行方法:保存为 HTML 文件,在浏览器中打开。
  • 效果
  • 工具栏显示“添加”和“刷新”按钮,带图标。
  • 点击“添加”增加新行并提示;点击“刷新”重载表格并提示。

4. 高级工具栏配置

4.1 工具栏的多样化配置

  • 多样化形式
  • 使用 JavaScript 数组定义工具栏。
  • 集成输入框、下拉菜单等控件。
  • 示例
$('#dg').datagrid({
  toolbar: [{
    text: '添加',
    iconCls: 'icon-add',
    handler: function() { alert('添加'); }
  }, '-', { // 分隔符
    text: '刷新',
    iconCls: 'icon-reload',
    handler: function() { $('#dg').datagrid('reload'); }
  }]
});

4.2 动态工具栏和交互

  • 动态工具栏:运行时修改工具栏内容。
  • 交互:结合事件响应用户操作。
  • 示例:动态添加按钮:
function addToolbarButton() {
  var toolbar = $('#dg').datagrid('getPanel').find('.datagrid-toolbar');
  toolbar.append('<a href="#" class="easyui-linkbutton" onclick="alert(\'新按钮\')">新按钮</a>');
  $.parser.parse(toolbar); // 解析新添加的 EasyUI 组件
}

4.3 实例:带交互的动态工具栏

以下是一个带交互的动态工具栏示例:

<!DOCTYPE html>
<html lang="zh">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>jQuery EasyUI 带交互的动态工具栏</title>
  <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
  <script src="https://www.jeasyui.com/easyui/jquery.easyui.min.js"></script>
  <link rel="stylesheet" href="https://www.jeasyui.com/easyui/themes/default/easyui.css">
  <link rel="stylesheet" href="https://www.jeasyui.com/easyui/themes/icon.css">
  <style>
    body { padding: 20px; }
    .tb-item { margin-right: 10px; }
  </style>
</head>
<body>
  <div class="container">
    <h2>带交互的动态工具栏</h2>
    <table id="dg" class="easyui-datagrid" style="width: 600px; height: 300px;"></table>
  </div>
  <script>
    var data = [
      { id: 1, name: '项目 A', price: 100, status: '进行中' },
      { id: 2, name: '项目 B', price: 200, status: '完成' },
      { id: 3, name: '项目 C', price: 300, status: '进行中' }
    ];

    $('#dg').datagrid({
      data: data,
      columns: [[
        { field: 'id', title: 'ID', width: 100 },
        { field: 'name', title: '名称', width: 200 },
        { field: 'price', title: '价格', width: 150, formatter: function(value) { return '¥' + value; } },
        { field: 'status', title: '状态', width: 100 }
      ]],
      toolbar: [{
        text: '添加',
        iconCls: 'icon-add',
        handler: addRow
      }, {
        text: '删除',
        iconCls: 'icon-remove',
        handler: deleteRow
      }, '-', {
        text: '搜索',
        iconCls: 'icon-search',
        handler: function() {
          addSearchField();
        }
      }]
    });

    function addRow() {
      $('#dg').datagrid('appendRow', { id: data.length + 1, name: '新项目', price: 0, status: '新建' });
      data = $('#dg').datagrid('getData').rows;
      $.messager.show({ title: '提示', msg: '已添加新行', timeout: 2000, showType: 'slide' });
    }

    function deleteRow() {
      var row = $('#dg').datagrid('getSelected');
      if (row) {
        var index = $('#dg').datagrid('getRowIndex', row);
        $('#dg').datagrid('deleteRow', index);
        data = $('#dg').datagrid('getData').rows;
        $.messager.show({ title: '提示', msg: '已删除行', timeout: 2000, showType: 'slide' });
      } else {
        $.messager.alert('提示', '请选择一行!', 'info');
      }
    }

    function addSearchField() {
      var toolbar = $('#dg').datagrid('getPanel').find('.datagrid-toolbar');
      if (toolbar.find('#searchInput').length === 0) {
        toolbar.append(`
          <span class="tb-item">
            <input id="searchInput" class="easyui-textbox" style="width: 150px;" data-options="prompt: '输入名称搜索', onChange: search">
          </span>
        `);
        $.parser.parse(toolbar);
      }
    }

    function search(newVal, oldVal) {
      var keyword = newVal.toLowerCase();
      var rows = data.filter(function(row) {
        return row.name.toLowerCase().indexOf(keyword) !== -1;
      });
      $('#dg').datagrid('loadData', rows);
    }
  </script>
</body>
</html>
  • 运行方法:保存为 HTML 文件,在浏览器中打开。
  • 效果
  • 工具栏初始包含“添加”、“删除”和“搜索”按钮。
  • 点击“添加”增加新行;“删除”移除选中行并提示。
  • 点击“搜索”动态添加搜索输入框,输入名称实时过滤表格数据。

5. 最佳实践与注意事项

5.1 设计与实现建议

  • 按钮分组:使用分隔符(如 '-')区分功能区域。
  • 一致性:优先使用 EasyUI 的 linkbutton 和图标样式。
  • 动态性:避免重复添加控件,检查是否存在后再操作。

5.2 性能优化与调试

  • 性能:动态添加控件时,使用 $.parser.parse 确保渲染,减少不必要的 DOM 操作。
  • 调试:检查工具栏事件是否正确触发,确认数据更新后表格同步刷新。

6. 结论

jQuery EasyUI 的工具栏通过 toolbar 属性为 DataGrid 提供了灵活的操作扩展,适用于简单和复杂场景。本文通过基础和高级示例展示了其用法,帮助你快速上手。如果需要更复杂的工具栏布局,可参考 jQuery EasyUI 复杂工具栏 或访问官方文档(jeasyui.com)。


回答特点

  • 结构:包含目录、带锚点的小标题和代码示例,逻辑清晰。
  • 实用性:从基础工具栏到动态交互,覆盖实际需求。
  • 内部链接:通过 <a href="#ID"> 跳转,如 实现基础工具栏
  • 出站链接:嵌入正文,指向官方文档。

如何运行

  • 直接保存为 HTML 文件,在浏览器中打开即可(需联网加载 CDN 资源)。