datagrid行内新增,行内编辑

实现效果

在这里插入图片描述
在这里插入图片描述

<div data-options="region:'east',split:true,border:false" title="部门列表" style="width:13%;">
            <table id="dept_datagrid" class="easyui-datagrid"
                   data-options="
                        url:'${path}/company/deptdg.do',
                        toolbar:'#dept_tools',
                        fit:true,
                        rownumbers:true,
                        fitColumns: true,
                        singleSelect:true,
                        onBeforeEdit: edit2,
                        onAfterEdit: edit1,
                        onCancelEdit:edit1,
                        onClickRow:function(index, row){
                        rowchange2(row.id);}">
                <thead>
                <th data-options="field:'id'" >ID</th>
                <th data-options="field:'name',width:100,editor:{type:'text'}">部门名称</th>
                </thead>
            </table>
            <div id="dept_tools">
                <a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-add'" onclick="addRow()">添加</a>
                <a href="#" id="editdeptbtn" class="easyui-linkbutton" data-options="iconCls:'icon-edit'" onclick="editDept()">编辑</a>
                <a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-del'" onclick="delDept()">删除</a>
            </div>
        </div>
    //添加行
    function addRow() {
        $('#dept_datagrid').datagrid('appendRow', {});      //追加一个新行。新行将被添加到最后的位置。
        var lastRowIndex=$('#dept_datagrid').datagrid('getRows').length-1;//返回当前页的最后一行。
        $('#dept_datagrid').datagrid('beginEdit', lastRowIndex);    //开始编辑行。
        $('#dept_datagrid').datagrid('selectRow',lastRowIndex); //选中最新行
        $("#editdeptbtn").find(".l-btn-text").text("完成");
    }
//编辑
    function editDept(){
        if(rowid==null){
            $.messager.alert("提示","请选择企业","info");
            return;
        }
        var type = $("#editdeptbtn").find(".l-btn-text").text();
        var row = $("#dept_datagrid").datagrid("getSelected");
        var index = $('#dept_datagrid').datagrid('getRowIndex', row);
        if(type=="完成"){
            $('#dept_datagrid').datagrid('endEdit', index);    //结束编辑行。
            $.ajax({
                type:"post",dataType:"json",
                url:"/company/addDept.do",
                data:{name:row.name,deptId:row.id,companyId:rowid},
                success:function(){
                    $("#editdeptbtn").find(".l-btn-text").text("编辑");
                    $('#dept_datagrid').datagrid('reload');
                    $('#dept_datagrid').datagrid('selectRow',index);
                }
            });
        }else{
            $('#dept_datagrid').datagrid('beginEdit', index);    //开始编辑行。
            $("#editdeptbtn").find(".l-btn-text").text("完成");
        }
    }

    //删除部门
    function delDept() {
        var select = $("#dept_datagrid").datagrid("getSelected");
        if(select == null){
            $.messager.alert("提示","请选择部门","info");
            return;
        }       $.messager.confirm("确认","确定要删除吗?\n确定删除将连同该部门下员工信息一同删除",function(yes){
            if(yes){
                commonDelete("${path}/company/delDept.do",select.id,"dept_datagrid");
            }
        });
    }
//删除公共调用方法
    function commonDelete(url,id,datagrid){
        $.messager.progress();
        $.ajax({
            url:url,
            type:"post",
            data:{id : id},
            dataType:"json",
            success:function(data){
                $.messager.progress("close");
                if(data.success){
                    $.messager.show({
                        title:"删除成功",
                        msg:"删除成功!",
                        timeout:600,
                        style:"left:30%;top:10%",
                        showType:"fade"
                    });
                    $("#"+datagrid).datagrid("reload");
                    $("#"+datagrid).datagrid('clearSelections');
                }else{
                    $.messager.alert("删除失败", data.msg,"error");
                }
            },
            error:function(){
                $.messager.progress("close");
                $.messager.alert("删除失败", "服务器连接失败!","error");
            }
        });
    }
function edit1 (index,row)
    {
        row.editing = false;
        $('#dept_datagrid').datagrid('refreshRow', index);
    }
    function edit2 (index,row)
    {
        row.editing = true;
        $('#dept_datagrid').datagrid('refreshRow', index);
    }
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
在 easyui datagrid 中实现编辑 checkbox 可以通过以下步骤: 1. 在 datagrid 的 columns 属性中添加一个 checkbox 列: ``` { field: 'checked', title: 'Checked', checkbox: true }, ``` 2. 在 datagrid 的 onBeforeEdit 事件中设置 checkbox 列的编辑器为 null,即禁用编辑功能: ``` onBeforeEdit: function(index, row) { $('#datagrid').datagrid('getColumnOption', 'checked').editor = null; }, ``` 3. 在 datagrid 的 onAfterEdit 事件中获取 checkbox 列的值并更到对应的数据中: ``` onAfterEdit: function(index, row, changes) { var checked = $('#datagrid').datagrid('getEditor', {index: index, field: 'checked'}); row.checked = $(checked.target).is(':checked'); $('#datagrid').datagrid('refreshRow', index); }, ``` 这样就可以实现编辑 checkbox 功能了。完整的代码示例可以参考下面的例子: ``` $('#datagrid').datagrid({ url: 'datagrid_data.json', columns: [[ { field: 'id', title: 'ID', width: 50 }, { field: 'name', title: 'Name', width: 100, editor: 'text' }, { field: 'checked', title: 'Checked', checkbox: true } ]], onBeforeEdit: function(index, row) { $('#datagrid').datagrid('getColumnOption', 'checked').editor = null; }, onAfterEdit: function(index, row, changes) { var checked = $('#datagrid').datagrid('getEditor', {index: index, field: 'checked'}); row.checked = $(checked.target).is(':checked'); $('#datagrid').datagrid('refreshRow', index); }, toolbar: [{ text: 'Save', iconCls: 'icon-save', handler: function() { $('#datagrid').datagrid('endEdit', 0); } }] }); ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

占星安啦

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值