Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1164870
  • 博文数量: 234
  • 博客积分: 5592
  • 博客等级: 大校
  • 技术积分: 1987
  • 用 户 组: 普通用户
  • 注册时间: 2009-12-23 14:12
文章分类

全部博文(234)

文章存档

2015年(1)

2013年(4)

2012年(16)

2011年(204)

2010年(9)

分类: JavaScript

2015-01-28 17:23:36

以前自己用jquery写了一个datagrid嵌套combogrid的小插件。
现在,很多页面替换成easyui了。想找个跟之前自己写的差不多的。google 半天找不到。自己组装了一下。
如果想做类似功能的就不用那么累google了。

还有一些功能没完善。满用。





点击(此处)折叠或打开

  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  2. <html>
  3. <head>
  4. <title>datagrid嵌套combogrid上下左右</title>
  5. <link rel="stylesheet" type="text/css" href="../script/easyui/themes/default/easyui.css">
  6. <link rel="stylesheet" type="text/css" href="../script/easyui/themes/icon.css">
  7. <script type="text/javascript" src="../script/easyui/jquery-1.8.0.min.js"></script>
  8. <script type="text/javascript" src="../script/easyui/jquery.easyui.min.js"></script>
  9. <script type="text/javascript">
  10. function isEmpty(obj){
  11.     for (var name in obj){
  12.         return false;
  13.     }
  14.     return true;
  15. };

  16. $.extend($.fn.datagrid.defaults.editors, {
  17.     combogrid: {
  18.         init: function(container, options){
  19.             var input = $('').appendTo(container);
  20.             input.combogrid(options);
  21.             return input;
  22.         },
  23.         destroy: function(target){
  24.             $(target).combogrid('destroy');
  25.         },
  26.         getValue: function(target){
  27.             return $(target).combogrid('getValue');
  28.         },
  29.         setValue: function(target, value){
  30.             $(target).combogrid('setValue', value);
  31.         },
  32.         resize: function(target, width){
  33.             $(target).combogrid('resize',width);
  34.         }
  35.     }
  36. });

  37.    var SampleInfo={};
  38.    
  39.    $.extend($.fn.datagrid.methods, {
  40.             editCell: function(jq,param){
  41.                 return jq.each(function(){
  42.                     var opts = $(this).datagrid('options');
  43.                     var fields = $(this).datagrid('getColumnFields',true).concat($(this).datagrid('getColumnFields'));
  44.                     for(var i=0; i<fields.length; i++){
  45.                         var col = $(this).datagrid('getColumnOption', fields[i]);
  46.                         col.editor1 = col.editor;
  47.                         if (fields[i] != param.field){
  48.                             col.editor = null;
  49.                         }
  50.                     }
  51.                     $(this).datagrid('beginEdit', param.index);
  52.                     var ed = $(this).datagrid('getEditor', param);
  53.                     if (ed){
  54.                         if ($(ed.target).hasClass('textbox-f')){
  55.                                console.log("textbox-f");
  56.                             $(ed.target).textbox('textbox').focus();
  57.                             $(ed.target).textbox('textbox').select();
  58.                         } else if($(ed.target).hasClass('combogrid-editable-input')){
  59.                             console.log("combogrid-editable-input");
  60.                             $(ed.target).parent().find(".combo-text").focus();
  61.                             $(ed.target).parent().find(".combo-text").select();
  62.                             //绑定key事件
  63.                             $(ed.target).parent().find(".combo-text").bind('keydown', function (e) {
  64.                                 if(e.keyCode==13){
  65.                                     $('#dg').datagrid('updateRow', {
  66.              index: SampleInfo.editorIndex,
  67.              row: {
  68.              goods_name:SampleInfo.rowData.goods_name,
  69.                  Unit3: SampleInfo.rowData.Unit3,
  70.                  WPGG: SampleInfo.rowData.wpgg
  71.              }
  72.          });
  73.                  opts.onClickCell(SampleInfo.editorIndex,"Unit3");
  74.                                 }
  75.                             });
  76.                             
  77.                         } else {
  78.                             console.log("textbox-other");
  79.                             $(ed.target).focus();
  80.                             $(ed.target).select();
  81.                         }
  82.                     }
  83.                     for(var i=0; i<fields.length; i++){
  84.                         var col = $(this).datagrid('getColumnOption', fields[i]);
  85.                         col.editor = col.editor1;
  86.                     }
  87.                 });
  88.             },
  89.             
  90.               enableCellEditing: function(jq){
  91.                 return jq.each(function(){
  92.                     var dg = $(this);
  93.                     var opts = dg.datagrid('options');
  94.                     opts.oldOnClickCell = opts.onClickCell;
  95.                     opts.onClickCell = function(index, field){
  96.                         SampleInfo.editorIndex=index;
  97.                         SampleInfo.editorField=field;
  98.                    
  99.                         if (opts.editIndex != undefined){
  100.                             if (dg.datagrid('validateRow', opts.editIndex)){
  101.                                 dg.datagrid('endEdit', opts.editIndex);
  102.                                 opts.editIndex = undefined;
  103.                             } else {
  104.                                 return;
  105.                             }
  106.                         }
  107.                         dg.datagrid('selectRow', index).datagrid('editCell', {
  108.                             index: index,
  109.                             field: field
  110.                         });
  111.                         opts.editIndex = index;
  112.                         opts.oldOnClickCell.call(this, index, field);
  113.                     }
  114.                 });
  115.                 }
  116.         });
  117.         
  118.         
  119.         
  120.   
  121. $.extend($.fn.datagrid.methods, {
  122.     keyCtr : function (jq) {
  123.         return jq.each(function () {
  124.             var grid = $(this);
  125.             grid.datagrid('getPanel').panel('panel').attr('tabindex', 1).bind('keydown', function (e) {
  126.                 if(SampleInfo.editorIndex == -1){
  127.                     return;
  128.                 }
  129.                 var keyCode = e.keyCode;
  130.                 //当键盘按下键为上下左右键时,把网格键盘按下的事件默认动作去除,要不然会影响按上下左右键选择文本框值
  131.                 if( keyCode == 37 || keyCode == 39 || keyCode == 38 || keyCode == 40 || keyCode==9){
  132.                     e.preventDefault();
  133.                     e.stopPropagation();
  134.                 }
  135.                 rows = grid.datagrid('getRows');
  136.                 var editors = grid.datagrid('getEditors',SampleInfo.editorIndex);
  137.                    var editor = grid.datagrid('getEditor', {index:SampleInfo.editorIndex,field:SampleInfo.editorField});
  138.                 switch (keyCode) {
  139.                 case 38: // up    
  140.                     if (editors) {
  141.                         var editor = grid.datagrid('getEditor', {index:SampleInfo.editorIndex,field:SampleInfo.editorField});
  142.                         if(editor.type=='combogrid') return ;
  143.                         
  144.                         if(rows.length>SampleInfo.editorIndex && SampleInfo.editorIndex>=1){
  145.                             SampleInfo.editorIndex--;
  146.                         }else{
  147.                             SampleInfo.editorIndex = rows.length-1;
  148.                         }
  149.                         var opts = grid.datagrid('options');
  150.                         opts.onClickCell(SampleInfo.editorIndex,SampleInfo.editorField);
  151.                     }
  152.                     break;
  153.                 case 40: // down
  154.                     if (editors) {
  155.                            var editor = grid.datagrid('getEditor', {index:SampleInfo.editorIndex,field:SampleInfo.editorField});
  156.                         if(editor.type=='combogrid') return ;
  157.                         
  158.                            if(rows.length-1>SampleInfo.editorIndex){
  159.                             SampleInfo.editorIndex++;
  160.                         }else{
  161.                             SampleInfo.editorIndex = 0;
  162.                         }
  163.                            var opts = grid.datagrid('options');
  164.                         opts.onClickCell(SampleInfo.editorIndex,SampleInfo.editorField);
  165.                     }
  166.                     break;
  167.                 case 37: // left
  168.                     if (editors) {
  169.                         if(SampleInfo.editorField == 'CJ_Name'){
  170.                             SampleInfo.editorField = 'Price';
  171.                         }else if(SampleInfo.editorField == 'Price'){
  172.                             SampleInfo.editorField = 'Unit3';
  173.                         }else if(SampleInfo.editorField == 'Unit3'){
  174.                             SampleInfo.editorField = 'Goods_Name';
  175.                         }
  176.                           var opts = grid.datagrid('options');
  177.                         opts.onClickCell(SampleInfo.editorIndex,SampleInfo.editorField);
  178.                     }
  179.                     break;
  180.                 case 39: // right
  181.                        if (editors) {
  182.                         if(SampleInfo.editorField == 'Goods_Name'){
  183.                             SampleInfo.editorField = 'Unit3';
  184.                         }else if(SampleInfo.editorField == 'Unit3'){
  185.                             SampleInfo.editorField = 'Price';
  186.                         }else if(SampleInfo.editorField == 'Price'){
  187.                             SampleInfo.editorField = 'CJ_Name';
  188.                         }
  189.                         var opts = grid.datagrid('options');
  190.                         opts.onClickCell(SampleInfo.editorIndex,SampleInfo.editorField);
  191.                        }
  192.                     break;
  193.                 case 13: // enter键
  194.                        if (editors) {
  195.                         if(SampleInfo.editorField == 'Goods_Name'){
  196.                             SampleInfo.editorField = 'Unit3';
  197.                         }else if(SampleInfo.editorField == 'Unit3'){
  198.                             SampleInfo.editorField = 'Price';
  199.                         }else if(SampleInfo.editorField == 'Price'){
  200.                             SampleInfo.editorField = 'CJ_Name';
  201.                         }
  202.                         var opts = grid.datagrid('options');
  203.                         opts.onClickCell(SampleInfo.editorIndex,SampleInfo.editorField);
  204.                        }
  205.                     break;
  206.                 case 9: // Tab键
  207.           if (editors) {
  208.                            var editor = grid.datagrid('getEditor', {index:SampleInfo.editorIndex,field:SampleInfo.editorField});
  209.                            if(rows.length-1>SampleInfo.editorIndex){
  210.                             SampleInfo.editorIndex++;
  211.                         }else{
  212.                             SampleInfo.editorIndex = 0;
  213.                         }
  214.                            var opts = grid.datagrid('options');
  215.                         opts.onClickCell(SampleInfo.editorIndex,"Goods_Name");
  216.                     }
  217.                     break;
  218.                 case 27: //F2保存键
  219.                       $('#dg').datagrid('endEdit',SampleInfo.editorIndex);
  220.                      break;
  221.                 }
  222.             });
  223.         });
  224.     }
  225. });
  226.         
  227.  
  228.     $(function(){
  229.         $('#dg').datagrid({
  230.             singleSelect:false,
  231.          url:"../goodslist1.json",
  232.             columns:[[
  233.                 {field:'Goods_Name',title:'物品名称',width:100,
  234.                     editor:{
  235.                         type:'combogrid',
  236.                         options:{
  237.                             required: false,
  238.                             panelWidth:450,
  239.                             idField:'id',
  240.                             textField:'goods_name',
  241.                          value:'',//缺省值
  242.                          mode:'remote',
  243.                      url:"../goodslist2.json",
  244.                             columns:[[
  245.                                 {field:'id',title:'id',width:60},
  246.                                 {field:'goods_name',title:'编码',width:60},
  247.                                 {field:'wpgg',title:'规格',width:100},
  248.                                 {field:'unit3',title:'单位',width:120}
  249.                             ]],
  250.                             onCheck:function(rowIndex,rowData){
  251.                             },
  252.                             onClickRow:function(newValue, oldValue){
  253.                                 var opts = $('#dg').datagrid('options');
  254.              opts.onClickCell(SampleInfo.editorIndex,"Unit3");
  255.                             },
  256.                      onSelect:function(rowIndex, rowData) {
  257.                          SampleInfo.rowData=rowData;
  258.          }
  259.                         },
  260.                     }
  261.                 },
  262.                 {field:'WPGG',title:'规格',width:100,align:'right',halign:'center'},
  263.                 {field:'Unit3',title:'单位',width:100,align:'right',halign:'center',editor:'text'},
  264.                 {field:'Price',title:'单价',width:100,align:'right',halign:'center',editor:{type:'numberbox',options:{precision:2}}},
  265.                 {field:'Provider_Name',title:'供应商',width:100,align:'right',halign:'center'},
  266.                 {field:'CJ_Name',title:'厂家',width:100,align:'right',halign:'center',editor:'text'}
  267.             ]]
  268.         });
  269.         $('#dg').datagrid().datagrid('enableCellEditing');
  270.         $("#dg").datagrid().datagrid("keyCtr");
  271. });

  272. </script>
  273. </head>
  274. <body>
  275.     <table id="dg" title="datagrid嵌套combogrid" width="100%" height="auto"
  276.       pagesize="15" pageList="[10,15,20]" toolbar="#toolbar" tools="#tools" pagination="true" rownumbers="true" fitColumns="true" fit="true">
  277.     </table>
  278. </body>
  279. </html>


阅读(5667) | 评论(0) | 转发(0) |
0

上一篇:基于Spring注解方式配置项目

下一篇:没有了

给主人留下些什么吧!~~