EasyUI数据表格中嵌入下拉框

效果

代码

javascript 复制代码
$(function () {
    // 标记当前正在编辑的行
    var editorIndex = -1;
    var data = [
        {
            code: 1,
            name: '1',
            price: 1,
            status: 0
        },
        {
            code: 2,
            name: '2',
            price: 2,
            status: 1
        }
    ]
    
    $('#dg').datagrid({
        data: data,
        onDblClickCell:function (index, field, value) {
            var dg = $(this);
            if(field !== 'status' && editorIndex !== -1) {
                dg.datagrid("endEdit",editorIndex)
                return;
            }else if(field === 'status' && editorIndex !== -1 && editorIndex !== index) {
                dg.datagrid("endEdit",editorIndex)
                return;
            }

            if(field === 'status' && editorIndex === -1) {
                editorIndex = index;
                dg.datagrid("beginEdit",index)
            }
            

        },
        onSelect: function(index, row) {
            var dg = $(this);
            if(editorIndex !== index) {
                dg.datagrid("endEdit",index)
            }
        },
        onClickCell: function(index, field, value) {
            var dg = $(this);
            if(editorIndex !== -1 && field !== "status") {
                dg.datagrid("endEdit",editorIndex)
            } else if(field === 'status' && editorIndex !== -1 && editorIndex !== index) {
                dg.datagrid("endEdit",editorIndex)
            }
        },
        onEndEdit:function (index, row, changes) {
            if(editorIndex === index) {
                editorIndex = -1;
                var dg = $(this);
                dg.datagrid("updateRow",index,row)
            }
        },
        columns:[[
            {field:'code',title:'代码',width:100,align: 'center'},
            {field:'name',title:'名称',width:100,align: 'center'},
            {field:'price',title:'价格',width:100,align:'center'},
            {field:'status',title:'状态',width:100,align:'center',formatter:function(value,row,index){
                return value && (value === 1 || value === '1')  ? "禁用" : "启用"
            },
                editor: {
                type: 'combobox',
                options: {
                    valueField: 'value',
                    textField: 'text',
                    data: [{value:1,text:'禁用'},{value:0,text:'启用'}]
                }
                }

            }
        ]]
    });

})

解析

列渲染为下拉框

javascript 复制代码
{field:'status',title:'状态',width:100,align:'center',formatter:function(value,row,index){
    return value && (value === 1 || value === '1')  ? "禁用" : "启用"
},
    editor: {
    type: 'combobox',
    options: {
        valueField: 'value',
        textField: 'text',
        data: [{value:1,text:'禁用'},{value:0,text:'启用'}]
    }
    }

}

双击触发编辑行

javascript 复制代码
onDblClickCell:function (index, field, value) {
    var dg = $(this);
    // 关闭其他行的行编辑
    if(field !== 'status' && editorIndex !== -1) {
        dg.datagrid("endEdit",editorIndex)
        return;
    }else if(field === 'status' && editorIndex !== -1 
    && editorIndex !== index) {
        dg.datagrid("endEdit",editorIndex)
        return;
    }
	
	// 监听指定列,触发行编辑
    if(field === 'status' && editorIndex === -1) {
        editorIndex = index;
        dg.datagrid("beginEdit",index)
    }
},

监听关闭行编辑

javascript 复制代码
onSelect: function(index, row) {
  var dg = $(this);
  if(editorIndex !== index) {
      dg.datagrid("endEdit",index)
  }
},
onClickCell: function(index, field, value) {
  var dg = $(this);
  if(editorIndex !== -1 && field !== "status") {
      dg.datagrid("endEdit",editorIndex)
  } else if(field === 'status' && editorIndex !== -1 
  && editorIndex !== index) {
      dg.datagrid("endEdit",editorIndex)
  }
},

监听编辑结束事件更新行数据

javascript 复制代码
  onEndEdit:function (index, row, changes) {
      if(editorIndex === index) {
          editorIndex = -1;
          var dg = $(this);
          dg.datagrid("updateRow",index,row)
      }
  },
相关推荐
Senar2 小时前
如何判断浏览器是否开启硬件加速
前端·javascript·数据可视化
HtwHUAT2 小时前
实验四 Java图形界面与事件处理
开发语言·前端·python
利刃之灵2 小时前
01-初识前端
前端
codingandsleeping2 小时前
一个简易版无缝轮播图的实现思路
前端·javascript·css
天天扭码2 小时前
一分钟解决 | 高频面试算法题——最大子数组之和
前端·算法·面试
全宝3 小时前
🌏【cesium系列】01.vue3+vite集成Cesium
前端·gis·cesium
拉不动的猪3 小时前
简单回顾下插槽透传
前端·javascript·面试
烛阴3 小时前
Fragment Shader--一行代码让屏幕瞬间变黄
前端·webgl
爱吃鱼的锅包肉4 小时前
Flutter路由模块化管理方案
前端·javascript·flutter
风清扬雨4 小时前
Vue3具名插槽用法全解——从零到一的详细指南
前端·javascript·vue.js