jQuery Easyui datagrid editor为combobox时指定数据源实例

 更新时间:2016年12月19日 14:46:48   作者:garfieldzf  
当在datagrid行内部应用添加编辑操作时,引入combobox是非常方便的操作,这篇文章主要介绍了jQuery Easyui datagrid editor为combobox时指定数据源实例,有兴趣的可以了解一下。

当在datagrid行内部应用添加编辑操作时,引入combobox是非常方便的操作,我在引入combobox时对数据源这快做个总结,在做demo的过程中遇到个问题,就是当你选择了下拉框的值后点击保存,此时显示的是value值,而不是text值,这时使用格式化函数解决此问题。

var Address = [{ "value": "1", "text": "CHINA" }, { "value": "2", "text": "USA" }, { "value": "3", "text": "Koren" }];

function unitformatter(value, rowData, rowIndex) {

  if (value == 0) {

    return;

  }

 

  for (var i = 0; i < Address.length; i++) {

    if (Address[i].value == value) {

      return Address[i].text;

    }

  }

}

function GetTable() {

  var editRow = undefined;

  $("#Student_Table").datagrid({

    height: 300,

    width: 450,

    title: '学生表',

    collapsible: true,

    singleSelect: true,

    url: '/Home/StuList',

    idField: 'ID',

    columns: [[

     { field: 'ID', title: 'ID', width: 100 },

      { field: 'Name', title: '姓名', width: 100, editor: { type: 'text', options: { required: true } } },

      { field: 'Age', title: '年龄', width: 100, align: 'center', editor: { type: 'text', options: { required: true } } },

      { field: 'Address', title: '地址', width: 100, formatter: unitformatter, align: 'center', editor: { type: 'combobox', options: { data: Address, valueField: "value", textField: "text" } } }

    ]],

    toolbar: [{

      text: '添加', iconCls: 'icon-add', handler: function () {

        if (editRow != undefined) {

          $("#Student_Table").datagrid('endEdit', editRow);

        }

        if (editRow == undefined) {

          $("#Student_Table").datagrid('insertRow', {

            index: 0,

            row: {}

          });

          $("#Student_Table").datagrid('beginEdit', 0);

          editRow = 0;

        }

      }

    }, '-', {

      text: '保存', iconCls: 'icon-save', handler: function () {

        $("#Student_Table").datagrid('endEdit', editRow);

        //如果调用acceptChanges(),使用getChanges()则获取不到编辑和新增的数据。

        //使用JSON序列化datarow对象,发送到后台。

        var rows = $("#Student_Table").datagrid('getChanges');

        var rowstr = JSON.stringify(rows);

        $.post('/Home/Create', rowstr, function (data) {

        });

      }

    }, '-', {

      text: '撤销', iconCls: 'icon-redo', handler: function () {

        editRow = undefined;

        $("#Student_Table").datagrid('rejectChanges');

        $("#Student_Table").datagrid('unselectAll');

      }

    }, '-', {

      text: '删除', iconCls: 'icon-remove', handler: function () {

        var row = $("#Student_Table").datagrid('getSelections');

      }

    }, '-', {

      text: '修改', iconCls: 'icon-edit', handler: function () {

        var row = $("#Student_Table").datagrid('getSelected');

        if (row != null) {

          if (editRow != undefined) {

            $("#Student_Table").datagrid('endEdit', editRow);

          }

          if (editRow == undefined) {

            var index = $("#Student_Table").datagrid('getRowIndex', row);

            $("#Student_Table").datagrid('beginEdit', index);

            editRow = index;

            $("#Student_Table").datagrid('unselectAll');

          }

        } else {

        }

      }

    }, '-', {

      text: '上移', iconCls: 'icon-up', handler: function () {

        MoveUp();

      }

    }, '-', {

      text: '下移', iconCls: 'icon-down', handler: function () {

        MoveDown();

      }

    }],

    onAfterEdit: function (rowIndex, rowData, changes) {

      editRow = undefined;

    },

    onDblClickRow: function (rowIndex, rowData) {

      if (editRow != undefined) {

        $("#Student_Table").datagrid('endEdit', editRow);

      }

      if (editRow == undefined) {

        $("#Student_Table").datagrid('beginEdit', rowIndex);

        editRow = rowIndex;

      }

    },

    onClickRow: function (rowIndex, rowData) {

      if (editRow != undefined) {

        $("#Student_Table").datagrid('endEdit', editRow);

      } 

    }

  });

} 

效果图:

 

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • jQuery简单操作cookie的插件实例

    jQuery简单操作cookie的插件实例

    这篇文章主要介绍了jQuery简单操作cookie的插件,以实例形式分析了jQuery操作cookie的插件功能定义与使用技巧,需要的朋友可以参考下
    2016-01-01
  • 整理8个很棒的 jQuery 倒计时插件和教程

    整理8个很棒的 jQuery 倒计时插件和教程

    jQuery 是最流行也是使用最广泛的 JavaScript 框架,它简化了 HTML 文档操作,事件处理,动画效果和 Ajax 交互。下面向大家分享8个优秀的 jQuery 倒计时插件和教程
    2011-12-12
  • jQuery.cookie.js使用方法及相关参数解释

    jQuery.cookie.js使用方法及相关参数解释

    一个轻量级的cookie 插件,可以读取、写入、删除 cookie。这篇文章主要介绍了jQuery.cookie.js使用方法及相关参数解释,需要的朋友可以参考下
    2017-03-03
  • 使用EVAL处理jqchart jquery 折线图返回数据无效的解决办法

    使用EVAL处理jqchart jquery 折线图返回数据无效的解决办法

    eval函数可以把一些处理过程序代码进行解析从而达到可以执行的一个状态,本篇文章给大家介绍使用eval处理jqchart jquery折线图返回数据无效的解决办法,对jqchart jquery相关内容感兴趣的朋友一起学习吧
    2015-11-11
  • jQuery弹出层插件Lightbox_me使用指南

    jQuery弹出层插件Lightbox_me使用指南

    在使用discuzx中有一个Message以及Dialog方法来显示信息对话框。今天写项目的时候,需要一个信息对话框,所以就着手利用lightbox_me插件来写一个做备用。
    2015-04-04
  • jquery控制左右箭头滚动图片列表的实例

    jquery控制左右箭头滚动图片列表的实例

    jquery控制左右箭头滚动图片列表的实例,需要的朋友可以参考一下
    2013-05-05
  • jQuery中Ajax的load方法详解

    jQuery中Ajax的load方法详解

    本文重点个大家讲解了几个jQuery中ajax的load()方法的使用实例,load()方法是jQuery中最为简单和常用的Ajax方法,能远程载入HTML代码并插入DOM中
    2015-01-01
  • jquery $.each()使用探讨

    jquery $.each()使用探讨

    想必大家对jquery $.each()并不陌生吧,使用它可以进行元素的遍历,下面有个不错的示例,感兴趣的朋友可以参考下
    2013-09-09
  • jQuery Div中加载其他页面的实现代码

    jQuery Div中加载其他页面的实现代码

    在做一个表单签核系统时,需在要签核页面中将表单内容(事先做好的PHP页面)显示出来,于就是想能不能利用Ajax技术把这个事先做好的页面嵌入到签核页面中呢?
    2009-02-02
  • jQuery 1.5 源码解读 面向中高阶JSER

    jQuery 1.5 源码解读 面向中高阶JSER

    jQuery 1.5 源码有8068行。很多想读 jQuery 源码的童鞋在读了一半不到就不敢往下读了,jQuery是一个 封装良好、代码紧凑 的框架。
    2011-04-04

最新评论