ITPub博客

首页 > 应用开发 > IT综合 > jquery.edatagrid.js

jquery.edatagrid.js

IT综合 作者:shelly_kuang 时间:2016-05-11 23:59:07 0 删除 编辑

  1. /**
  2.  * edatagrid - jQuery EasyUI
  3.  *
  4.  * Licensed under the GPL:
  5.  * http://www.gnu.org/licenses/gpl.txt
  6.  *
  7.  * Copyright 2011-2015
  8.  *
  9.  * Dependencies:
  10.  * datagrid
  11.  * messager
  12.  *
  13.  */
  14. (function($){
  15.     // var oldLoadDataMethod = $.fn.datagrid.methods.loadData;
  16.     // $.fn.datagrid.methods.loadData = function(jq, data){
  17.     //     jq.each(function(){
  18.     //         $.data(this, 'datagrid').filterSource = null;
  19.     //     });
  20.     //     return oldLoadDataMethod.call($.fn.datagrid.methods, jq, data);
  21.     // };

  22.     var autoGrids = [];
  23.     function checkAutoGrid(){
  24.         autoGrids = $.grep(autoGrids, function(t){
  25.             return t.length && t.data('edatagrid');
  26.         });
  27.     }
  28.     function saveAutoGrid(omit){
  29.         checkAutoGrid();
  30.         $.map(autoGrids, function(t){
  31.             if (t[0] != $(omit)[0]){
  32.                 t.edatagrid('saveRow');
  33.             }
  34.         });
  35.         checkAutoGrid();
  36.     }
  37.     function addAutoGrid(dg){
  38.         checkAutoGrid();
  39.         for(var i=0; i<autoGrids.length; i++){
  40.             if ($(autoGrids[i])[0] == $(dg)[0]){return;}
  41.         }
  42.         autoGrids.push($(dg));
  43.     }
  44.     function delAutoGrid(dg){
  45.         checkAutoGrid();
  46.         autoGrids = $.grep(autoGrids, function(t){
  47.             return $(t)[0] != $(dg)[0];
  48.         });
  49.     }

  50.     $(function(){
  51.         $(document).unbind('.edatagrid').bind('mousedown.edatagrid', function(e){
  52.             var p = $(e.target).closest('div.datagrid-view,div.combo-panel,div.window,div.window-mask');
  53.             if (p.length){
  54.                 if (p.hasClass('datagrid-view')){
  55.                     saveAutoGrid(p.children('table'));
  56.                 }
  57.                 return;
  58.             }
  59.             saveAutoGrid();
  60.         });
  61.     });
  62.     
  63.     function buildGrid(target){
  64.         var opts = $.data(target, 'edatagrid').options;
  65.         $(target).datagrid($.extend({}, opts, {
  66.             onDblClickCell:function(index,field,value){
  67.                 if (opts.editing){
  68.                     $(this).edatagrid('editRow', index);
  69.                     focusEditor(target, field);
  70.                 }
  71.                 if (opts.onDblClickCell){
  72.                     opts.onDblClickCell.call(target, index, field, value);
  73.                 }
  74.             },
  75.             onClickCell:function(index,field,value){
  76.                 // if (opts.editing && opts.editIndex >= 0){
  77.                 //     $(this).edatagrid('editRow', index);
  78.                 //     focusEditor(target, field);
  79.                 // }
  80.                 if (opts.editIndex >= 0){
  81.                     var dg = $(this);
  82.                     if (opts.editing){
  83.                         dg.edatagrid('editRow', index);
  84.                     } else {
  85.                         setTimeout(function(){
  86.                             dg.edatagrid('selectRow', opts.editIndex);
  87.                         }, 0);
  88.                     }
  89.                     focusEditor(target, field);
  90.                 }
  91.                 if (opts.onClickCell){
  92.                     opts.onClickCell.call(target, index, field, value);
  93.                 }
  94.             },
  95.             onBeforeEdit: function(index, row){
  96.                 if (opts.onBeforeEdit){
  97.                     if (opts.onBeforeEdit.call(target, index, row) == false){
  98.                         return false;
  99.                     }
  100.                 }
  101.                 if (opts.autoSave){
  102.                     addAutoGrid(this);
  103.                 }
  104.                 opts.originalRow = $.extend(true, [], row);
  105.             },
  106.             onAfterEdit: function(index, row){
  107.                 delAutoGrid(this);
  108.                 opts.editIndex = -1;
  109.                 var url = row.isNewRecord ? opts.saveUrl : opts.updateUrl;
  110.                 if (url){
  111.                     var changed = false;
  112.                     var fields = $(this).edatagrid('getColumnFields',true).concat($(this).edatagrid('getColumnFields'));
  113.                     for(var i=0; i<fields.length; i++){
  114.                         var field = fields[i];
  115.                         var col = $(this).edatagrid('getColumnOption', field);
  116.                         if (col.editor && opts.originalRow[field] != row[field]){
  117.                             changed = true;
  118.                             break;
  119.                         }
  120.                     }
  121.                     if (changed){
  122.                         opts.poster.call(target, url, row, function(data){
  123.                             if (data.isError){
  124.                                 var originalRow = opts.originalRow;
  125.                                 $(target).edatagrid('cancelRow',index);
  126.                                 $(target).edatagrid('selectRow',index);
  127.                                 $(target).edatagrid('editRow',index);
  128.                                 opts.originalRow = originalRow;
  129.                                 opts.onError.call(target, index, data);
  130.                                 return;
  131.                             }
  132.                             data.isNewRecord = null;
  133.                             $(target).datagrid('updateRow', {
  134.                                 index: index,
  135.                                 row: data
  136.                             });
  137.                             if (opts.tree){
  138.                                 var idValue = row[opts.idField||'id'];
  139.                                 var t = $(opts.tree);
  140.                                 var node = t.tree('find', idValue);
  141.                                 if (node){
  142.                                     node.text = row[opts.treeTextField];
  143.                                     t.tree('update', node);
  144.                                 } else {
  145.                                     var pnode = t.tree('find', row[opts.treeParentField]);
  146.                                     t.tree('append', {
  147.                                         parent: (pnode ? pnode.target : null),
  148.                                         data: [{id:idValue,text:row[opts.treeTextField]}]
  149.                                     });
  150.                                 }
  151.                             }
  152.                             opts.onSuccess.call(target, index, row);
  153.                             opts.onSave.call(target, index, row);
  154.                         }, function(data){
  155.                             opts.onError.call(target, index, data);
  156.                         });
  157.                     } else {
  158.                         opts.onSave.call(target, index, row);
  159.                     }
  160.                 } else {
  161.                     opts.onSave.call(target, index, row);
  162.                 }
  163.                 if (opts.onAfterEdit) opts.onAfterEdit.call(target, index, row);
  164.             },
  165.             onCancelEdit: function(index, row){
  166.                 delAutoGrid(this);
  167.                 opts.editIndex = -1;
  168.                 if (row.isNewRecord) {
  169.                     $(this).datagrid('deleteRow', index);
  170.                 }
  171.                 if (opts.onCancelEdit) opts.onCancelEdit.call(target, index, row);
  172.             },
  173.             onBeforeLoad: function(param){
  174.                 if (opts.onBeforeLoad.call(target, param) == false){return false}
  175.                 $(this).edatagrid('cancelRow');
  176.                 if (opts.tree){
  177.                     var node = $(opts.tree).tree('getSelected');
  178.                     param[opts.treeParentField] = node ? node.id : undefined;
  179.                 }
  180.             }
  181.         }));
  182.         
  183.         
  184.         
  185.         if (opts.tree){
  186.             $(opts.tree).tree({
  187.                 url: opts.treeUrl,
  188.                 onClick: function(node){
  189.                     $(target).datagrid('load');
  190.                 },
  191.                 onDrop: function(dest,source,point){
  192.                     var targetId = $(this).tree('getNode', dest).id;
  193.                     var data = {
  194.                         id:source.id,
  195.                         targetId:targetId,
  196.                         point:point
  197.                     };
  198.                     opts.poster.call(target, opts.treeDndUrl, data, function(result){
  199.                         $(target).datagrid('load');
  200.                     });
  201.                 }
  202.             });
  203.         }
  204.     }

  205.     function focusEditor(target, field){
  206.         var opts = $(target).edatagrid('options');
  207.         var t;
  208.         var editor = $(target).datagrid('getEditor', {index:opts.editIndex,field:field});
  209.         if (editor){
  210.             t = editor.target;
  211.         } else {
  212.             var editors = $(target).datagrid('getEditors', opts.editIndex);
  213.             if (editors.length){
  214.                 t = editors[0].target;
  215.             }
  216.         }
  217.         if (t){
  218.             if ($(t).hasClass('textbox-f')){
  219.                 $(t).textbox('textbox').focus();
  220.             } else {
  221.                 $(t).focus();                    
  222.             }
  223.         }
  224.     }
  225.     
  226.     $.fn.edatagrid = function(options, param){
  227.         if (typeof options == 'string'){
  228.             var method = $.fn.edatagrid.methods[options];
  229.             if (method){
  230.                 return method(this, param);
  231.             } else {
  232.                 return this.datagrid(options, param);
  233.             }
  234.         }
  235.         
  236.         options = options || {};
  237.         return this.each(function(){
  238.             var state = $.data(this, 'edatagrid');
  239.             if (state){
  240.                 $.extend(state.options, options);
  241.             } else {
  242.                 $.data(this, 'edatagrid', {
  243.                     options: $.extend({}, $.fn.edatagrid.defaults, $.fn.edatagrid.parseOptions(this), options)
  244.                 });
  245.             }
  246.             buildGrid(this);
  247.         });
  248.     };
  249.     
  250.     $.fn.edatagrid.parseOptions = function(target){
  251.         return $.extend({}, $.fn.datagrid.parseOptions(target), {
  252.         });
  253.     };
  254.     
  255.     $.fn.edatagrid.methods = {
  256.         options: function(jq){
  257.             var opts = $.data(jq[0], 'edatagrid').options;
  258.             return opts;
  259.         },
  260.         loadData: function(jq, data){
  261.             return jq.each(function(){
  262.                 $(this).edatagrid('cancelRow');
  263.                 $(this).datagrid('loadData', data);
  264.             });
  265.         },
  266.         enableEditing: function(jq){
  267.             return jq.each(function(){
  268.                 var opts = $.data(this, 'edatagrid').options;
  269.                 opts.editing = true;
  270.             });
  271.         },
  272.         disableEditing: function(jq){
  273.             return jq.each(function(){
  274.                 var opts = $.data(this, 'edatagrid').options;
  275.                 opts.editing = false;
  276.             });
  277.         },
  278.         isEditing: function(jq, index){
  279.             var opts = $.data(jq[0], 'edatagrid').options;
  280.             var tr = opts.finder.getTr(jq[0], index);
  281.             return tr.length && tr.hasClass('datagrid-row-editing');
  282.         },
  283.         editRow: function(jq, index){
  284.             return jq.each(function(){
  285.                 var dg = $(this);
  286.                 var opts = $.data(this, 'edatagrid').options;
  287.                 var editIndex = opts.editIndex;
  288.                 if (editIndex != index){
  289.                     if (dg.datagrid('validateRow', editIndex)){
  290.                         if (editIndex>=0){
  291.                             if (opts.onBeforeSave.call(this, editIndex) == false) {
  292.                                 setTimeout(function(){
  293.                                     dg.datagrid('selectRow', editIndex);
  294.                                 },0);
  295.                                 return;
  296.                             }
  297.                         }
  298.                         dg.datagrid('endEdit', editIndex);
  299.                         dg.datagrid('beginEdit', index);
  300.                         if (!dg.edatagrid('isEditing', index)){
  301.                             return;
  302.                         }
  303.                         opts.editIndex = index;
  304.                         focusEditor(this);
  305.                         
  306.                         var rows = dg.datagrid('getRows');
  307.                         opts.onEdit.call(this, index, rows[index]);
  308.                     } else {
  309.                         setTimeout(function(){
  310.                             dg.datagrid('selectRow', editIndex);
  311.                         }, 0);
  312.                     }
  313.                 }
  314.             });
  315.         },
  316.         addRow: function(jq, index){
  317.             return jq.each(function(){
  318.                 var dg = $(this);
  319.                 var opts = $.data(this, 'edatagrid').options;
  320.                 if (opts.editIndex >= 0){
  321.                     if (!dg.datagrid('validateRow', opts.editIndex)){
  322.                         dg.datagrid('selectRow', opts.editIndex);
  323.                         return;
  324.                     }
  325.                     if (opts.onBeforeSave.call(this, opts.editIndex) == false){
  326.                         setTimeout(function(){
  327.                             dg.datagrid('selectRow', opts.editIndex);
  328.                         },0);
  329.                         return;
  330.                     }
  331.                     dg.datagrid('endEdit', opts.editIndex);
  332.                 }
  333.                 var rows = dg.datagrid('getRows');
  334.                 
  335.                 function _add(index, row){
  336.                     if (index == undefined){
  337.                         dg.datagrid('appendRow', row);
  338.                         opts.editIndex = rows.length - 1;
  339.                     } else {
  340.                         dg.datagrid('insertRow', {index:index,row:row});
  341.                         opts.editIndex = index;
  342.                     }
  343.                 }
  344.                 if (typeof index == 'object'){
  345.                     _add(index.index, $.extend(index.row, {isNewRecord:true}))
  346.                 } else {
  347.                     _add(index, {isNewRecord:true});
  348.                 }
  349.                 
  350. //                if (index == undefined){
  351. //                    dg.datagrid('appendRow', {isNewRecord:true});
  352. //                    opts.editIndex = rows.length - 1;
  353. //                } else {
  354. //                    dg.datagrid('insertRow', {
  355. //                        index: index,
  356. //                        row: {isNewRecord:true}
  357. //                    });
  358. //                    opts.editIndex = index;
  359. //                }
  360.                 
  361.                 dg.datagrid('beginEdit', opts.editIndex);
  362.                 dg.datagrid('selectRow', opts.editIndex);
  363.                 
  364.                 if (opts.tree){
  365.                     var node = $(opts.tree).tree('getSelected');
  366.                     rows[opts.editIndex][opts.treeParentField] = (node ? node.id : 0);
  367.                 }
  368.                 
  369.                 opts.onAdd.call(this, opts.editIndex, rows[opts.editIndex]);
  370.             });
  371.         },
  372.         saveRow: function(jq){
  373.             return jq.each(function(){
  374.                 var dg = $(this);
  375.                 var opts = $.data(this, 'edatagrid').options;
  376.                 if (opts.editIndex >= 0){
  377.                     if (opts.onBeforeSave.call(this, opts.editIndex) == false) {
  378.                         setTimeout(function(){
  379.                             dg.datagrid('selectRow', opts.editIndex);
  380.                         },0);
  381.                         return;
  382.                     }
  383.                     $(this).datagrid('endEdit', opts.editIndex);
  384.                 }
  385.             });
  386.         },
  387.         cancelRow: function(jq){
  388.             return jq.each(function(){
  389.                 var opts = $.data(this, 'edatagrid').options;
  390.                 if (opts.editIndex >= 0){
  391.                     $(this).datagrid('cancelEdit', opts.editIndex);
  392.                 }
  393.             });
  394.         },
  395.         destroyRow: function(jq, index){
  396.             return jq.each(function(){
  397.                 var dg = $(this);
  398.                 var opts = $.data(this, 'edatagrid').options;
  399.                 
  400.                 var rows = [];
  401.                 if (index == undefined){
  402.                     rows = dg.datagrid('getSelections');
  403.                 } else {
  404.                     var rowIndexes = $.isArray(index) ? index : [index];
  405.                     for(var i=0; i<rowIndexes.length; i++){
  406.                         var row = opts.finder.getRow(this, rowIndexes[i]);
  407.                         if (row){
  408.                             rows.push(row);
  409.                         }
  410.                     }
  411.                 }
  412.                 
  413.                 if (!rows.length){
  414.                     $.messager.show({
  415.                         title: opts.destroyMsg.norecord.title,
  416.                         msg: opts.destroyMsg.norecord.msg
  417.                     });
  418.                     return;
  419.                 }
  420.                 
  421.                 $.messager.confirm(opts.destroyMsg.confirm.title,opts.destroyMsg.confirm.msg,function(r){
  422.                     if (r){
  423.                         for(var i=0; i<rows.length; i++){
  424.                             _del(rows[i]);
  425.                         }
  426.                         dg.datagrid('clearSelections');
  427.                     }
  428.                 });
  429.                 
  430.                 function _del(row){
  431.                     var index = dg.datagrid('getRowIndex', row);
  432.                     if (index == -1){return}
  433.                     if (row.isNewRecord){
  434.                         dg.datagrid('cancelEdit', index);
  435.                     } else {
  436.                         if (opts.destroyUrl){
  437.                             var idValue = row[opts.idField||'id'];
  438.                             opts.poster.call(dg[0], opts.destroyUrl, {id:idValue}, function(data){
  439.                                 var index = dg.datagrid('getRowIndex', idValue);
  440.                                 if (data.isError){
  441.                                     dg.datagrid('selectRow', index);
  442.                                     opts.onError.call(dg[0], index, data);
  443.                                     return;
  444.                                 }
  445.                                 if (opts.tree){
  446.                                     dg.datagrid('reload');
  447.                                     var t = $(opts.tree);
  448.                                     var node = t.tree('find', idValue);
  449.                                     if (node){
  450.                                         t.tree('remove', node.target);
  451.                                     }
  452.                                 } else {
  453.                                     dg.datagrid('cancelEdit', index);
  454.                                     dg.datagrid('deleteRow', index);
  455.                                 }
  456.                                 opts.onDestroy.call(dg[0], index, row);
  457.                                 var pager = dg.datagrid('getPager');
  458.                                 if (pager.length && !dg.datagrid('getRows').length){
  459.                                     dg.datagrid('options').pageNumber = pager.pagination('options').pageNumber;
  460.                                     dg.datagrid('reload');
  461.                                 }
  462.                             }, function(data){
  463.                                 opts.onError.call(dg[0], index, data);
  464.                             });
  465.                         } else {
  466.                             dg.datagrid('cancelEdit', index);
  467.                             dg.datagrid('deleteRow', index);
  468.                             opts.onDestroy.call(dg[0], index, row);
  469.                         }
  470.                     }
  471.                 }
  472.             });
  473.         }
  474.     };
  475.     
  476.     $.fn.edatagrid.defaults = $.extend({}, $.fn.datagrid.defaults, {
  477.         singleSelect: true,
  478.         editing: true,
  479.         editIndex: -1,
  480.         destroyMsg:{
  481.             norecord:{
  482.                 title:'Warning',
  483.                 msg:'No record is selected.'
  484.             },
  485.             confirm:{
  486.                 title:'Confirm',
  487.                 msg:'Are you sure you want to delete?'
  488.             }
  489.         },
  490.         poster: function(url, data, success, error){
  491.             $.ajax({
  492.                 type: 'post',
  493.                 url: url,
  494.                 data: data,
  495.                 dataType: 'json',
  496.                 success: function(data){
  497.                     success(data);
  498.                 },
  499.                 error: function(jqXHR, textStatus, errorThrown){
  500.                     error({
  501.                         jqXHR: jqXHR,
  502.                         textStatus: textStatus,
  503.                         errorThrown: errorThrown
  504.                     });
  505.                 }
  506.             });
  507.         },
  508.         
  509.         autoSave: false,    // auto save the editing row when click out of datagrid
  510.         url: null,    // return the datagrid data
  511.         saveUrl: null,    // return the added row
  512.         updateUrl: null,    // return the updated row
  513.         destroyUrl: null,    // return {success:true}
  514.         
  515.         tree: null,        // the tree selector
  516.         treeUrl: null,    // return tree data
  517.         treeDndUrl: null,    // to process the drag and drop operation, return {success:true}
  518.         treeTextField: 'name',
  519.         treeParentField: 'parentId',
  520.         
  521.         onAdd: function(index, row){},
  522.         onEdit: function(index, row){},
  523.         onBeforeSave: function(index){},
  524.         onSave: function(index, row){},
  525.         onSuccess: function(index, row){},
  526.         onDestroy: function(index, row){},
  527.         onError: function(index, row){}
  528.     });
  529.     
  530.     ////////////////////////////////
  531.     $.parser.plugins.push('edatagrid');
  532. })(jQuery);

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/29404710/viewspace-2098201/ ,如需转载,请注明出处,否则将追究法律责任。

上一篇: editable-datagrid.html
下一篇: 没有了~
请登录后发表评论 登录
全部评论

注册时间:2015-01-16

  • 博文量
    23
  • 访问量
    49457