logo

EasyUI DataGrid能编辑2012-07-08

可编辑特征是最近添加的。它能让用户添加新行。用户也可以更新一行或多行。这个教程显示了如何创建使用行内编辑的DataGrid。
\
创建DataGrid
<table id="tt"></table>
$('#tt').datagrid({
    title:'Editable DataGrid',
    iconCls:'icon-edit',
    width:660,
    height:250,
    singleSelect:true,
    idField:'itemid',
    url:'datagrid_data.json',
    columns:[[
        {field:'itemid',title:'Item ID',width:60},
        {field:'productid',title:'Product',width:100,
            formatter:function(value){
                for(var i=0; i<products.length; i++){
                    if (products[i].productid == value) return products[i].name;
                }
                return value;
            },
            editor:{
                type:'combobox',
                options:{
                    valueField:'productid',
                    textField:'name',
                    data:products,
                    required:true
                }
            }
        },
        {field:'listprice',title:'List Price',width:80,align:'right',editor:{type:'numberbox',options:{precision:1}}},
        {field:'unitcost',title:'Unit Cost',width:80,align:'right',editor:'numberbox'},
        {field:'attr1',title:'Attribute',width:150,editor:'text'},
        {field:'status',title:'Status',width:50,align:'center',
            editor:{
                type:'checkbox',
                options:{
                    on: 'P',
                    off: ''
                }
            }
        },
        {field:'action',title:'Action',width:70,align:'center',
            formatter:function(value,row,index){
                if (row.editing){
                    var s = '<a href="#" onclick="saverow('+index+')">Save</a> ';
                    var c = '<a href="#" onclick="cancelrow('+index+')">Cancel</a>';
                    return s+c;
                } else {
                    var e = '<a href="#" onclick="editrow('+index+')">Edit</a> ';
                    var d = '<a href="#" onclick="deleterow('+index+')">Delete</a>';
                    return e+d;
                }
            }
        }
    ]],
    onBeforeEdit:function(index,row){
        row.editing = true;
        $('#tt').datagrid('refreshRow', index);
    },
    onAfterEdit:function(index,row){
        row.editing = false;
        $('#tt').datagrid('refreshRow', index);
    },
    onCancelEdit:function(index,row){
        row.editing = false;
        $('#tt').datagrid('refreshRow', index);
    }
});
使DataGrid可编辑,你应该添加editor属性到列中。editor告诉DataGrid如何编辑字和如何存储值。我们定义了三个editor:text,combobox,checkbox。
添加编辑功能
function editrow(index){
    $('#tt').datagrid('beginEdit', index);
}
function deleterow(index){
    $.messager.confirm('Confirm','Are you sure?',function(r){
        if (r){
            $('#tt').datagrid('deleteRow', index);
        }
    });
}
function saverow(index){
    $('#tt').datagrid('endEdit', index);
}
function cancelrow(index){
    $('#tt').datagrid('cancelEdit', index);
}

相关话题

热门搜索

  • EasyUI
  • DataGrid编辑

扫描二维码分享话题