easyUI合并DataGrid单元格

@author YHC

datagrid经常需要合并一些单元格,这个教程将向你展示如何在datagrid合并单元格.

合并你的datagrid 单元格,简单的调用'mergeCells' 方法和传入合并信息参数告诉datagrid 如何合并单元格,在所有合并单元格中,除了第一个单元格,

将会隐藏当单元格合并.


创建DataGrid

<table id="tt" title="Merge Cells" style="width:550px;height:250px"  
        url="data/datagrid_data.json"  
        singleSelect="true" iconCls="icon-save" rownumbers="true"  
        idField="itemid" pagination="true">  
    <thead frozen="true">  
        <tr>  
            <th field="productid" width="80" formatter="formatProduct">Product ID</th>  
            <th field="itemid" width="100">Item ID</th>  
        </tr>  
    </thead>  
    <thead>  
        <tr>  
            <th colspan="2">Price</th>  
            <th rowspan="2" field="attr1" width="150">Attribute</th>  
            <th rowspan="2" field="status" width="60" align="center">Stauts</th>  
        </tr>  
        <tr>  
            <th field="listprice" width="80" align="right">List Price</th>  
            <th field="unitcost" width="80" align="right">Unit Cost</th>  
        </tr>  
    </thead>  
</table> 
合并单元格
当数据加载之后,我们合并一些在datagrid中的单元格,所以放置以下在 onLoadSuccess 回调函数中.

$('#tt').datagrid({  
    onLoadSuccess:function(){  
        var merges = [{  
            index:2,  
            rowspan:2  
        },{  
            index:5,  
            rowspan:2  
        },{  
            index:7,  
            rowspan:2  
        }];  
        for(var i=0; i<merges.length; i++)  
            $('#tt').datagrid('mergeCells',{  
                index:merges[i].index,  
                field:'productid',  
                rowspan:merges[i].rowspan  
            });  
    }  
}); 
下载 EasyUI 示例代码:




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
对于 EasyUIDataGrid 组件,你可以使用它提供的编辑器和验证规则来校验单元格的必填项。下面是一种实现方式: 1. 首先,为 DataGrid 中需要校验的列配置编辑器,比如使用 text 或 combobox 编辑器。可以通过设置 `editor` 属性来指定编辑器类型。 2. 然后,在 DataGrid 的 `onBeforeSave` 事件中获取当前编辑的行数据,并进行必填校验。可以通过获取编辑器的值或其他方式来判断单元格是否有值。 3. 如果单元格没有值,则可以通过返回 false 来阻止保存操作,并弹出提示信息告知用户。 下面是一个示例代码: ```javascript $('#datagrid').datagrid({ // 配置列信息,包括编辑器和验证规则 columns: [{ field: 'name', title: '姓名', editor: { type: 'text' }, // 配置验证规则,设置为必填项 validate: { rules: { required: true }, messages: { required: '该字段为必填项' } } }, { field: 'age', title: '年龄', editor: { type: 'text' }, validate: { rules: { required: true }, messages: { required: '该字段为必填项' } } }], // 配置保存前的事件 onBeforeSave: function(index, row) { // 遍历列定义,判断必填项是否有值 var isValid = true; var columns = $(this).datagrid('getColumnFields'); for (var i = 0; i < columns.length; i++) { var field = columns[i]; var col = $(this).datagrid('getColumnOption', field); if (col.validate && col.validate.rules && col.validate.rules.required) { var value = row[field]; if (!value || value.trim() === '') { isValid = false; // 弹出提示信息 $.messager.alert('提示', col.validate.messages.required, 'warning'); break; } } } return isValid; } }); ``` 以上代码中,我们通过配置 `editor` 和 `validate` 属性来定义列的编辑器和验证规则。在 `onBeforeSave` 事件中,我们遍历列定义,判断必填项是否有值,若没有则阻止保存操作并弹出提示信息。 你可以根据自己的实际需求进行修改和扩展。希望能对你有所帮助!如有其他问题,请继续提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值