我也不知道叫什么好
2017-09-23 06:27
采纳率: 50%
浏览 4.0k
已采纳

easyUI datagrid 列编辑

datagrid行上多个字段都是可编辑的,都有editor 属性,但是现在想要单击或双击某一列,打开当前列的编辑状态,求助大神,谢谢

答案找到了,谢谢

$.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); for ( var i = 0; i < fields.length; i++) { var col = $(this).datagrid('getColumnOption', fields[i]); col.editor = col.editor1; } }); } }); var editIndex = undefined; //结束编辑 function endEditing() { if (editIndex == undefined) { return true } if ($('#dg').datagrid('validateRow', editIndex)) { $('#dg').datagrid('endEdit', editIndex); editIndex = undefined; return true; } else { return false; } } //单击单元格 function onClickCell(index, field) { if (endEditing()) { $('#dg').datagrid('selectRow', index).datagrid('editCell', { index : index, field : field }); editIndex = index; } }
  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • 微笑客 2017-09-23 08:20
    已采纳

    公司自己封装的这个 你可以复制过去直接用

    类似于这样

    var tableOptions = {
    url : '${listurl}',
    toolbar : '#tb',
    selector : '#grid',
    sortName : 'implement_record_id',
    sortOrder : 'desc',
    title : '${bussi.bussinessName}',
    //导出url
    exportURL : '/s:url',
    //新建窗口
    newURL : '/s:url',
    //查看编辑窗口
    viewURL : '/s:url',
    //keyId字段
    keyIdField : 'implement_record_id',
    //启用侧边栏
    workflowSideBar : false,
    authNo : 'credit_auth/s:ifcredit_main_company_user_auth/s:else '
    };
    var dg = new DefDataGrid(tableOptions);

    /**

    • 新建表单
    • @param {Object} tableOptions
    • 参数明细:
    • rb:刷新按钮选择器,默认'#refreshButton'
    • fb:显示筛选栏按钮:默认'#filterButton'
    • newb:新建按钮:默认'#addBtn'
    • viewb:查看按钮:默认'#editBtn'
    • editb:查看按钮:默认'#editDetailBtn'
    • delb:删除按钮:默认:'#delBtn'
    • exportb:导出按钮:默认'#exportBtn'
    • filterForm:筛选表格选择器:默认'#filterForm'
    • searchb:筛选按钮:默认'#searchBtn'
    • resetb:重置按钮:默认'#resetBtn'
    • newURL:新建页面
    • viewURL:查看页面
    • editURL:编辑页面
    • exportURL:导出url
    • delURL:删除URL
    • delayLoad:false 延迟加载数据
    • db2edit:双击进入编辑页面,默认false
    • keyIdFiled:主键keyId字段
    • enableSideBar:启用侧边栏;默认false
    • sideBarJsonURL:json url
    • sideBar:选择器, 默认:'#sidebar'
    • sideBarInput:筛选input选择器,默认:'#filter_sidebar'
    • defaultSideBarVal:侧边栏默认值
    • sideBarCallback : 侧边栏回调函数
    • loadSidebar('customerSidebarJson.action','#sidebar','#filter_sidebar',filterSearch,11102);
    • workflowSideBar : 是否启用审批流程侧边栏;默认:false
    • workflowName:指定默认流程代码
    • workstatusField:查询筛选字段,默认audit_status
    • workflowSideBarInput : 筛选input选择器;默认null
    • businessNo : 默认businessid
    • authNo: 权限脚本id
    • sortNameDefault:排序额外标识字段(sortName排序分页重复时考虑使用)
    • tbtooltip:表格tooltip,默认:true
    • tipContent:表格head tip内容表达式[{name:'列名',exp:'表达式'},...]
      */
      function DefDataGrid(tableOptions){
      var emp = {};
      $.extend(emp, defPagiGridOpts, tableOptions);
      if(emp.delayLoad){
      this.dataUrl = emp.url;
      delete emp.url;
      }

      tOptions = emp;
      if(!emp.queryParams)
      emp.queryParams = {};
      if(emp.authNo)
      emp.authNo = emp.authNo.replace(/(^\s*)|(\s*$)/g,'');
      $.extend(emp.queryParams,{bussinessNo:emp.businessNo ? emp.businessNo : BOM.bussinessNo,authNo:emp.authNo,sortNameDefault:emp.sortNameDefault});
      this.defQueryParams = emp.queryParams;
      this.opts = emp;
      var dg = this;
      $(function() {
      dg.jq = $(dg.opts.selector);

      //刷新
      var refM = function(){dg.jq.datagrid("reload");};
      $(dg.opts.rb).click(refM);
      reloadMethod(refM);
      //显示搜索
      $(dg.opts.fb).click(function() {
          $(dg.opts.filterForm).parent('.toobar-search').toggle(300);
          setTimeout(function(){
              dg.jq.datagrid('resize');
          },350);
      });
      //搜索
      var searchM = function(currentInput){
          var temp=$(dg.opts.filterForm).serializeObject();
          for(o in temp){
              var temp2=temp[o];
              if(isArray(temp2)){
                  var temp3=temp2.join(',');
                  temp[o]=temp3;
              }
      
          }
          var qp = $.extend({},dg.defQueryParams,temp);
          qp = $.extend(qp, getCurrentComponentValue(currentInput));
          var p = {pageNumber: 1, queryParams:qp};
          if(dg.opts.delayLoad)
              p.url = dg.dataUrl;
          dg.jq.datagrid(p);
      };
      $(dg.opts.searchb).click(searchM);
      $(dg.opts.filterForm).submit(function(){return false;});
      $(dg.opts.filterForm).find('input').keyup(function(event){
          if(event.keyCode == 13) {
              searchM(this);
              var _t = this;
              setTimeout(function() { $(_t).focus().select(); }, 0);
          }
      });
      //重置
      var resetM = function() {
          clearFormWithoutHidden(dg.opts.filterForm);
          var qp = $.extend({},dg.defQueryParams,$(dg.opts.filterForm).serializeObject());
          dg.jq.datagrid({pageNumber: 1, queryParams:qp});
      };
      $(dg.opts.resetb).click(resetM);
      
      //侧边栏obj
      if(dg.opts.enableSideBar){
          if(dg.opts.enableSideBarTree)
              loadSidebar2(dg.opts.sideBarJsonURL,dg.opts.sideBar,dg.opts.sideBarInput,dg.opts.sideBarCallback ? 
                           dg.opts.sideBarCallback : searchM,dg.opts.defaultSideBarVal);
          else
              loadSidebar(dg.opts.sideBarJsonURL,dg.opts.toolbar,dg.opts.sideBarInput,dg.opts.sideBarCallback ? 
                           dg.opts.sideBarCallback : searchM,dg.opts.defaultSideBarVal,dg.opts.reportSideBar ? dg.opts.reportSideBar : false);
      }else if(dg.opts.workflowSideBar){
          //审批侧边栏
          var wurl = BOM.rootURL + 'workflow/nodeList.action';
          var wprop = {businessNo:BOM.bussinessNo}; 
          if(dg.opts.workflowName)
              wprop.workflowName = dg.opts.workflowName; 
          wurl = concatURL(wurl,wprop);
          var wcb = searchM;
          var wf = dg.opts.workstatusField; 
          if(!dg.opts.workflowSideBarInput){
              dg.opts.workflowSideBarInput = '#auditinput';
              setFormValue(dg.opts.filterForm,'filter.' + wf,[''],'auditinput');
              wcb = function(id){
                  setFormValue(dg.opts.filterForm,'filterClause.' + wf,[wf + '=?']);
                  setFormValue(dg.opts.filterForm,'filterType.' + wf,['java.lang.Long']);
                  searchM();
              };
          }
          loadSidebar(wurl,dg.opts.toolbar,dg.opts.workflowSideBarInput,dg.opts.sideBarCallback ? dg.opts.sideBarCallback : wcb,-1,dg.opts.reportSideBar ? dg.opts.reportSideBar : false);
      }
      
      //导出
      if(dg.opts.exportURL)
          $(dg.opts.exportb).click(function() {
              exportExcel(dg.opts.selector,dg.opts.filterForm,dg.opts.exportURL,dg.opts.authNo);
          });
      
      //明细
      if(dg.opts.viewURL && dg.opts.keyIdField){
          var openM = function(row){
              var query = {};
              query[dg.opts.queryIdName] = row[dg.opts.keyIdField];
              openWindow(dg.opts.viewURL, query);
          };
          $(dg.opts.viewb).click(function(){
              var row = dg.getSelectRow(true);
              if(!row)return;
              openM(row);
          });
          dg.opts.onDblClickRow = function(rowIndex){
              dg.jq.datagrid("clearSelections");
              dg.jq.datagrid("selectRow", rowIndex);
              var row = dg.getSelectRow();
              if(!row)return;
              openM(row);
          }
      
      }
      //编辑
      if (dg.opts.editURL && dg.opts.keyIdField) {
          var openE = function(row){
              var query = {};
              query[dg.opts.queryIdName] = row[dg.opts.keyIdField];
              openWindow(dg.opts.editURL,query);
          };
          $(dg.opts.editb).click(function(){
              var row = dg.getSelectRow(true);
              if(!row)return;
              openE(row);
          });
          if(dg.opts.db2edit){
              dg.opts.onDblClickRow = function(rowIndex){
                  dg.jq.datagrid("clearSelections");
                  dg.jq.datagrid("selectRow", rowIndex);
                  var row = dg.getSelectRow();
                  if(!row)return;
                  openE(row);
              }
          }
      }
      
      //删除
      if(dg.opts.delURL && dg.opts.keyIdField){
          $(dg.opts.delb).click(function(){
              var row = dg.getSelectRow(true);
              if(!row)return;
              if(confirm('您确定要删除选中单据?')){
                  var query = {};
                  query[dg.opts.queryIdName] = row[dg.opts.keyIdField];
                  getSyncJSON(dg.opts.delURL, query,refM);
              }
          });
      }
      
      //新建
      if(dg.opts.newURL){
          $(dg.opts.newb).click(function(){openWindow(dg.opts.newURL);});
      }
      //tooltip
      if(dg.opts.tbtooltip){
          var func = dg.opts.onLoadSuccess;
              dg.opts.onLoadSuccess = function(){
                  if(func){
                      func.apply(this,arguments);
                  }
                  initTip(dg.jq,dg.opts);
          }
      }
      //初始化列表
      dg.jq.datagrid(dg.opts);
      

      });
      }

    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • 谢谢楼上,我找到了,我要的是下面这种:
    $.extend($.fn.datagrid.methods, {
    addEditor : function(jq, param) {
    if (param instanceof Array) {
    $.each(param, function(index, item) {
    var e = $(jq).datagrid('getColumnOption', item.field);
    e.editor = item.editor;
    });
    } else {
    var e = $(jq).datagrid('getColumnOption', param.field);
    e.editor = param.editor;
    }
    },
    removeEditor : function(jq, param) {
    if (param instanceof Array) {
    $.each(param, function(index, item) {
    var e = $(jq).datagrid('getColumnOption', item);
    e.editor = {};
    });
    } else {
    var e = $(jq).datagrid('getColumnOption', param);
    e.editor = {};
    }
    }
    });

        function changeInputQtyFieldType(datagridId, field) {
            //editor="{type:'commonselector', options: {clickFn:splitInputQty, required:true}}"
            $("#dgPpmWaste").datagrid('addEditor', {
                field : field,
                editor : {
                    type : 'combobox',
                    options : {
                        clickFn: splitInputQty,
                        required: true
                    }
                }
            });
        }
    
        //双击打开单元格编辑
        function onDblClickCell(rowIndex, field, value){
            debugger
            ppmWaste.onDblClickCell(rowIndex, field, value);
            $.ajax({
                 url:'platform/mms/mds/mdsbomtype/mdsBomTypeController/operation/getMdsBomTypes',
                 data:  {data:'297ebf2650d59b090150d5cf810d049d'},
                 type : 'post',
                 dataType : 'json',
                 success : function(r){
                     if (r.flag == "success") {
                         debugger
                         var mdsBomTypeDTOs = r.mdsBomTypeDTOs;
                         //var mdsBomTypeDTOs = {"typeName":"1","id":"2"};
                            var d = [];
                            d.push(mdsBomTypeDTOs);
                             var edMdsBomType = $("#dgPpmWaste").datagrid('getEditor',{index:index,field: 'mdsBomTypeId'});
                             $(edMdsBomType.target).combobox('loadData', mdsBomTypeDTOs);
                             $('#zhangxl').combobox('loadData', mdsBomTypeDTOs);
    
                    }
                 }
             });
    
    
        }
               $(document)
        .ready(
                function() {
                    document.body.style.visibility = 'visible';
                    // 初始化键盘,鼠标事件基准列(可视列)数据
                    $("#dgPpmWaste").datagrid('initTabKeyColumns', this);
    
                    $("#dgPpmWaste").datagrid("options").onClickCell = function(index, field) {
    
                        if (field == "mdsBomTypeId" ) {
                            $.fn.datagrid.methods["defaultClickCellEvent"]
                                    ({
                                        gridId : "#dgPpmWaste",
                                        index : index,
                                        field : field
                                    });
    
                        }
                        // 注册“tab”及“上下左右”键事件
                        //ppmMrpWo.bindKeyBoardHander("#dgPpmWaste");
                    };
                    //}
                });
    
    评论
    解决 无用
    打赏 举报
  • 上面的copy错了,是下面的

    $.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); for ( var i = 0; i < fields.length; i++) { var col = $(this).datagrid('getColumnOption', fields[i]); col.editor = col.editor1; } }); } }); var editIndex = undefined; //结束编辑 function endEditing() { if (editIndex == undefined) { return true } if ($('#dg').datagrid('validateRow', editIndex)) { $('#dg').datagrid('endEdit', editIndex); editIndex = undefined; return true; } else { return false; } } //单击单元格 function onClickCell(index, field) { if (endEditing()) { $('#dg').datagrid('selectRow', index).datagrid('editCell', { index : index, field : field }); editIndex = index; } }
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题