现在,很多页面替换成easyui了。想找个跟之前自己写的差不多的。google 半天找不到。自己组装了一下。
如果想做类似功能的就不用那么累google了。
还有一些功能没完善。满用。
点击(此处)折叠或打开
-
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
-
<html>
-
<head>
-
<title>datagrid嵌套combogrid上下左右</title>
-
<link rel="stylesheet" type="text/css" href="../script/easyui/themes/default/easyui.css">
-
<link rel="stylesheet" type="text/css" href="../script/easyui/themes/icon.css">
-
<script type="text/javascript" src="../script/easyui/jquery-1.8.0.min.js"></script>
-
<script type="text/javascript" src="../script/easyui/jquery.easyui.min.js"></script>
-
<script type="text/javascript">
-
function isEmpty(obj){
-
for (var name in obj){
-
return false;
-
}
-
return true;
-
};
-
-
$.extend($.fn.datagrid.defaults.editors, {
-
combogrid: {
-
init: function(container, options){
-
var input = $('').appendTo(container);
-
input.combogrid(options);
-
return input;
-
},
-
destroy: function(target){
-
$(target).combogrid('destroy');
-
},
-
getValue: function(target){
-
return $(target).combogrid('getValue');
-
},
-
setValue: function(target, value){
-
$(target).combogrid('setValue', value);
-
},
-
resize: function(target, width){
-
$(target).combogrid('resize',width);
-
}
-
}
-
});
-
-
var SampleInfo={};
-
-
$.extend($.fn.datagrid.methods, {
-
editCell: function(jq,param){
-
return jq.each(function(){
-
var opts = $(this).datagrid('options');
-
var fields = $(this).datagrid('getColumnFields',true).concat($(this).datagrid('getColumnFields'));
-
for(var i=0; i<fields.length; i++){
-
var col = $(this).datagrid('getColumnOption', fields[i]);
-
col.editor1 = col.editor;
-
if (fields[i] != param.field){
-
col.editor = null;
-
}
-
}
-
$(this).datagrid('beginEdit', param.index);
-
var ed = $(this).datagrid('getEditor', param);
-
if (ed){
-
if ($(ed.target).hasClass('textbox-f')){
-
console.log("textbox-f");
-
$(ed.target).textbox('textbox').focus();
-
$(ed.target).textbox('textbox').select();
-
} else if($(ed.target).hasClass('combogrid-editable-input')){
-
console.log("combogrid-editable-input");
-
$(ed.target).parent().find(".combo-text").focus();
-
$(ed.target).parent().find(".combo-text").select();
-
//绑定key事件
-
$(ed.target).parent().find(".combo-text").bind('keydown', function (e) {
-
if(e.keyCode==13){
-
$('#dg').datagrid('updateRow', {
-
index: SampleInfo.editorIndex,
-
row: {
-
goods_name:SampleInfo.rowData.goods_name,
-
Unit3: SampleInfo.rowData.Unit3,
-
WPGG: SampleInfo.rowData.wpgg
-
}
-
});
-
opts.onClickCell(SampleInfo.editorIndex,"Unit3");
-
}
-
});
-
-
} else {
-
console.log("textbox-other");
-
$(ed.target).focus();
-
$(ed.target).select();
-
}
-
}
-
for(var i=0; i<fields.length; i++){
-
var col = $(this).datagrid('getColumnOption', fields[i]);
-
col.editor = col.editor1;
-
}
-
});
-
},
-
-
enableCellEditing: function(jq){
-
return jq.each(function(){
-
var dg = $(this);
-
var opts = dg.datagrid('options');
-
opts.oldOnClickCell = opts.onClickCell;
-
opts.onClickCell = function(index, field){
-
SampleInfo.editorIndex=index;
-
SampleInfo.editorField=field;
-
-
if (opts.editIndex != undefined){
-
if (dg.datagrid('validateRow', opts.editIndex)){
-
dg.datagrid('endEdit', opts.editIndex);
-
opts.editIndex = undefined;
-
} else {
-
return;
-
}
-
}
-
dg.datagrid('selectRow', index).datagrid('editCell', {
-
index: index,
-
field: field
-
});
-
opts.editIndex = index;
-
opts.oldOnClickCell.call(this, index, field);
-
}
-
});
-
}
-
});
-
-
-
-
-
$.extend($.fn.datagrid.methods, {
-
keyCtr : function (jq) {
-
return jq.each(function () {
-
var grid = $(this);
-
grid.datagrid('getPanel').panel('panel').attr('tabindex', 1).bind('keydown', function (e) {
-
if(SampleInfo.editorIndex == -1){
-
return;
-
}
-
var keyCode = e.keyCode;
-
//当键盘按下键为上下左右键时,把网格键盘按下的事件默认动作去除,要不然会影响按上下左右键选择文本框值
-
if( keyCode == 37 || keyCode == 39 || keyCode == 38 || keyCode == 40 || keyCode==9){
-
e.preventDefault();
-
e.stopPropagation();
-
}
-
rows = grid.datagrid('getRows');
-
var editors = grid.datagrid('getEditors',SampleInfo.editorIndex);
-
var editor = grid.datagrid('getEditor', {index:SampleInfo.editorIndex,field:SampleInfo.editorField});
-
switch (keyCode) {
-
case 38: // up
-
if (editors) {
-
var editor = grid.datagrid('getEditor', {index:SampleInfo.editorIndex,field:SampleInfo.editorField});
-
if(editor.type=='combogrid') return ;
-
-
if(rows.length>SampleInfo.editorIndex && SampleInfo.editorIndex>=1){
-
SampleInfo.editorIndex--;
-
}else{
-
SampleInfo.editorIndex = rows.length-1;
-
}
-
var opts = grid.datagrid('options');
-
opts.onClickCell(SampleInfo.editorIndex,SampleInfo.editorField);
-
}
-
break;
-
case 40: // down
-
if (editors) {
-
var editor = grid.datagrid('getEditor', {index:SampleInfo.editorIndex,field:SampleInfo.editorField});
-
if(editor.type=='combogrid') return ;
-
-
if(rows.length-1>SampleInfo.editorIndex){
-
SampleInfo.editorIndex++;
-
}else{
-
SampleInfo.editorIndex = 0;
-
}
-
var opts = grid.datagrid('options');
-
opts.onClickCell(SampleInfo.editorIndex,SampleInfo.editorField);
-
}
-
break;
-
case 37: // left
-
if (editors) {
-
if(SampleInfo.editorField == 'CJ_Name'){
-
SampleInfo.editorField = 'Price';
-
}else if(SampleInfo.editorField == 'Price'){
-
SampleInfo.editorField = 'Unit3';
-
}else if(SampleInfo.editorField == 'Unit3'){
-
SampleInfo.editorField = 'Goods_Name';
-
}
-
var opts = grid.datagrid('options');
-
opts.onClickCell(SampleInfo.editorIndex,SampleInfo.editorField);
-
}
-
break;
-
case 39: // right
-
if (editors) {
-
if(SampleInfo.editorField == 'Goods_Name'){
-
SampleInfo.editorField = 'Unit3';
-
}else if(SampleInfo.editorField == 'Unit3'){
-
SampleInfo.editorField = 'Price';
-
}else if(SampleInfo.editorField == 'Price'){
-
SampleInfo.editorField = 'CJ_Name';
-
}
-
var opts = grid.datagrid('options');
-
opts.onClickCell(SampleInfo.editorIndex,SampleInfo.editorField);
-
}
-
break;
-
case 13: // enter键
-
if (editors) {
-
if(SampleInfo.editorField == 'Goods_Name'){
-
SampleInfo.editorField = 'Unit3';
-
}else if(SampleInfo.editorField == 'Unit3'){
-
SampleInfo.editorField = 'Price';
-
}else if(SampleInfo.editorField == 'Price'){
-
SampleInfo.editorField = 'CJ_Name';
-
}
-
var opts = grid.datagrid('options');
-
opts.onClickCell(SampleInfo.editorIndex,SampleInfo.editorField);
-
}
-
break;
-
case 9: // Tab键
-
if (editors) {
-
var editor = grid.datagrid('getEditor', {index:SampleInfo.editorIndex,field:SampleInfo.editorField});
-
if(rows.length-1>SampleInfo.editorIndex){
-
SampleInfo.editorIndex++;
-
}else{
-
SampleInfo.editorIndex = 0;
-
}
-
var opts = grid.datagrid('options');
-
opts.onClickCell(SampleInfo.editorIndex,"Goods_Name");
-
}
-
break;
-
case 27: //F2保存键
-
$('#dg').datagrid('endEdit',SampleInfo.editorIndex);
-
break;
-
}
-
});
-
});
-
}
-
});
-
-
-
$(function(){
-
$('#dg').datagrid({
-
singleSelect:false,
-
url:"../goodslist1.json",
-
columns:[[
-
{field:'Goods_Name',title:'物品名称',width:100,
-
editor:{
-
type:'combogrid',
-
options:{
-
required: false,
-
panelWidth:450,
-
idField:'id',
-
textField:'goods_name',
-
value:'',//缺省值
-
mode:'remote',
-
url:"../goodslist2.json",
-
columns:[[
-
{field:'id',title:'id',width:60},
-
{field:'goods_name',title:'编码',width:60},
-
{field:'wpgg',title:'规格',width:100},
-
{field:'unit3',title:'单位',width:120}
-
]],
-
onCheck:function(rowIndex,rowData){
-
},
-
onClickRow:function(newValue, oldValue){
-
var opts = $('#dg').datagrid('options');
-
opts.onClickCell(SampleInfo.editorIndex,"Unit3");
-
},
-
onSelect:function(rowIndex, rowData) {
-
SampleInfo.rowData=rowData;
-
}
-
},
-
}
-
},
-
{field:'WPGG',title:'规格',width:100,align:'right',halign:'center'},
-
{field:'Unit3',title:'单位',width:100,align:'right',halign:'center',editor:'text'},
-
{field:'Price',title:'单价',width:100,align:'right',halign:'center',editor:{type:'numberbox',options:{precision:2}}},
-
{field:'Provider_Name',title:'供应商',width:100,align:'right',halign:'center'},
-
{field:'CJ_Name',title:'厂家',width:100,align:'right',halign:'center',editor:'text'}
-
]]
-
});
-
$('#dg').datagrid().datagrid('enableCellEditing');
-
$("#dg").datagrid().datagrid("keyCtr");
-
});
-
-
</script>
-
</head>
-
<body>
-
<table id="dg" title="datagrid嵌套combogrid" width="100%" height="auto"
-
pagesize="15" pageList="[10,15,20]" toolbar="#toolbar" tools="#tools" pagination="true" rownumbers="true" fitColumns="true" fit="true">
-
</table>
-
</body>
- </html>