<think>我们正在讨论EasyUI DataGrid的columns配置。EasyUI是一个基于jQuery的UI插件集合,其中DataGrid用于显示表格数据。
DataGrid的columns属性是一个数组,用于定义列的配置信息。每个列配置对象可以包含多个属性,如标题(title)、字段(field)、宽度(width)、对齐方式(align)等。
下面我们将详细说明如何配置DataGrid的columns,并给出一个示例。
</think>以下是关于 EasyUI DataGrid 的 columns 配置详解。DataGrid 是 EasyUI 中最常用的组件之一,用于展示表格数据,而 columns 配置决定了表格的列结构、显示方式和功能。
### 📊 EasyUI DataGrid Columns 配置详解
DataGrid 的 columns 属性是一个二维数组(数组的数组),每个内部数组表示一行列分组。每列可配置多个属性控制其行为和显示方式。
#### ✅ 基础列配置示例
```javascript
$('#dg').datagrid({
columns: [[
{field: 'code', title: '代码', width: 100, align: 'center'},
{field: 'name', title: '名称', width: 200},
{field: 'price', title: '价格', width: 100, formatter: formatPrice}
]]
});
// 格式化函数示例
function formatPrice(value, row, index) {
return '¥' + value.toFixed(2);
}
```
#### 🔍 核心配置属性说明
| 属性 | 类型 | 说明 | 示例 |
|------|------|------|------|
| **field** | string | 关联的数据字段名 | `'productName'` |
| **title** | string | 列标题文本 | `'产品名称'` |
| **width** | number | 列宽度(像素) | `150` |
| **align** | string | 文本对齐方式 | `'left'`, `'center'`, `'right'` |
| **halign** | string | 标题对齐方式 | `'center'` |
| **sortable** | boolean | 是否可排序 | `true` |
| **hidden** | boolean | 是否隐藏列 | `false` |
| **checkbox** | boolean | 是否显示复选框 | `true` |
| **formatter** | function | 自定义单元格内容格式化 | `function(value, row, index)` |
| **styler** | function | 自定义单元格样式 | `function(value, row, index)` |
| **editor** | string/object | 行编辑时使用的编辑器 | `'text'`, `{type:'numberbox', options:{precision:1}}` |
#### ✅ 高级功能示例
**1. 带操作按钮的列**
```javascript
columns: [[
{field: 'name', title: '姓名', width: 100},
{field: 'age', title: '年龄', width: 80},
{title: '操作', width: 150, align: 'center',
formatter: function(value, row, index){
return '<a href="#" onclick="editUser(' + index + ')">编辑</a> | ' +
'<a href="#" onclick="deleteUser(' + index + ')">删除</a>';
}
}
]]
```
**2. 分组列头**
```javascript
columns: [[
{title: '基本信息', align: 'center', colspan: 2},
{title: '联系方式', align: 'center', colspan: 2}
], [
{field: 'name', title: '姓名', width: 100, align: 'center'},
{field: 'gender', title: '性别', width: 80, align: 'center'},
{field: 'phone', title: '电话', width: 120},
{field: 'email', title: '邮箱', width: 150}
]]
```
**3. 行编辑配置**
```javascript
columns: [[
{field: 'productId', title: 'ID', width: 80},
{field: 'productName', title: '产品名', width: 150,
editor: {type: 'validatebox', options: {required: true}}},
{field: 'price', title: '价格', width: 100,
editor: {type: 'numberbox', options: {precision: 2}}},
{field: 'status', title: '状态', width: 100,
editor: {type: 'combobox', options: {data: statusData, valueField: 'id', textField: 'text'}}}
]]
```
#### 💡 实用技巧
1. **动态列加载**:
```javascript
// 从服务器加载列配置
$('#dg').datagrid({
url: 'get_columns.php',
onLoadSuccess: function(data){
$(this).datagrid('reload');
}
});
```
2. **列显示/隐藏控制**:
```javascript
// 隐藏列
$('#dg').datagrid('hideColumn', 'fieldName');
// 显示列
$('#dg').datagrid('showColumn', 'fieldName');
```
3. **冻结列**:
```javascript
frozenColumns: [[
{field: 'id', title: 'ID', width: 80}
]],
columns: [[
{field: 'name', title: 'Name', width: 150}
// 其他列...
]]
```
#### ⚠️ 常见问题解决方案
1. **列宽度自适应**:
```javascript
columns: [[
{field: 'title', title: '标题', width: '25%'},
{field: 'content', title: '内容', width: '75%'}
]]
```
2. **中文排序问题**:
```javascript
columns: [[
{field: 'name', title: '姓名', sortable: true,
sorter: function(a, b){
return a.localeCompare(b, 'zh-CN');
}}
]]
```
3. **复杂表头对齐**:
使用 `rowspan` 和 `colspan` 配合自定义 CSS 类解决复杂表头样式问题
---
###