zoukankan      html  css  js  c++  java
  • 在EasyUI的DataGrid中嵌入Combobox



           在做项目时,须要在EasyUI的DataGrid中嵌入Combobox,花了好几天功夫,在大家的帮助下,最终看到了它的庐山真面:


            

             核心代码例如以下:

          

    <html>
    <head>
        @*加入Jquery EasyUI的样式*@
        <link href="@Url.Content("../../Content/JqueryEasyUI/themes/default/easyui.css")" rel="stylesheet" />
        <link href="@Url.Content("../../Content/JqueryEasyUI/themes/icon.css")" rel="stylesheet" />
    
        @*加入Jquery。EasyUI和easyUI的语言包的JS文件*@
        <script src="@Url.Content("../../Content/JqueryEasyUI/jquery-1.8.0.min.js")"></script>
        <script src="@Url.Content("../../Content/JqueryEasyUI/jquery.easyui.min.js")"></script>
        <script src="@Url.Content("../../Content/JqueryEasyUI/locale/easyui-lang-zh_CN.js")"></script>
    
        @*实现对EasyUI的DataGird控件操作的JS代码*@
        <script type="text/javascript">
    	
            $(function () {
                //当页面首次刷新的时候运行的事件
                initTable();    
            });
    
            //实现新闻DataGird控件的绑定操作
            function initTable(queryData) {
                $('#test').datagrid({            //定位到Table标签,Table标签的ID是test
                    fitColumns: true,
                    url: '/News/QueryAllNews',   //指向后台的Action来获取当前用户的信息的Json格式的数据
                    title: '新闻公告',           //表格标题
                    striped: true,               //斑马线效果
                    pagination: true,            //分页工具栏
                    rownumbers: true,            //显示行号
                    onClickCell: onClickCell,    //点击单元格触发的事件(重要)
                    onAfterEdit:onAfterEdit,     //编辑单元格之后触发的事件(重要)
                    idField: 'NewsID',           //标识字段
                    queryParams: queryData,      //异步查询的參数
                    columns: [[
    	                  { field: 'ck', checkbox: true },
    	                  { title: '主键', field: 'NewsID', sortable: true, hidden: true, },
                        <span style="white-space:pre">	</span>  { title: '内容标题', field: 'NewsTitle',  50, sortable: true },
                      <span style="white-space:pre">	</span>  { title: '详细内容', field: 'NewsContent', sortable: true, hidden: true, },
                      <span style="white-space:pre">	</span>  { title: '创建时间', field: 'TimeStamp', sortable: true, },
                      <span style="white-space:pre">	</span>  { title: '所属类别', field: 'CategoryName', sortable: true, },
                      <span style="white-space:pre">	</span>  { title: '创建人', field: 'UserName', sortable: true },
                       <span style="white-space:pre">	</span>  {
                                title: '是否在首页显示', field: 'IsShow', sortable: true,                      
                                editor: {
                                          type: 'combobox',
                                          options: {                             
                                                     valueField: 'text',
                                                     textField: 'text',
                                                     method: 'get',
                                                     url: '/News/ReturnIsShowData',
                                                     required: true
                                                    }
                                         }
                              }                 
                            ]],
                    toolbar: [{
                        id: 'btnadd',
                        text: '加入',
                        iconCls: 'icon-add',
                        handler: function () {
                            //实现弹出注冊信息的页面
                            AddNews();
                        }
                    }, '-', {
                        id: 'btncut',
                        text: '改动',
                        iconCls: 'icon-cut',
                        handler: function () {
                            //实现改动的方法
                            UpdateLzjs();
                        }
                    }, '-', {
                        id: 'btnCancle',
                        text: '删除',
                        iconCls: 'icon-remove',
                        handler: function () {
                            //实现直接删除全部数据的方法
                            DeleteLzjs();
                        }
                    }]
                });
            }
    
    		
            $.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 ($('#test').datagrid('validateRow', editIndex)) {
                    $('#test').datagrid('endEdit', editIndex);
                    editIndex = undefined;
                    return true;
                } else {
                    return false;
                }
            }
    		
    	//点击单元格触发的事件
            function onClickCell(index, field) {
                if (endEditing()) {
                    $('#test').datagrid('selectRow', index)
    						.datagrid('editCell', { index: index, field: field });
                    editIndex = index;
                }
            }
    		
    	//编辑完单元格之后触发的事件
            function onAfterEdit(index, row, changes) {
                
                //…运行编辑单元格后须要运行的操作…
    	    //…运行编辑单元格后须要运行的操作…
            }     
            }
        </script>
    </head>
    <body>
            <table id="test" style=" 955px; margin: 20px 0 10px 8px" title="新闻中心" iconcls="icon-edit">
            </table>
    </body>
    </html>


             在Combobox类型的editor的数据源url: '/News/ReturnIsEnabledData'在相应Controller中相应的方法为(事实上就是在后台拼了个特别简单的json串):

             

    public string ReturnIsShowData()
            {
                string strJson = "[{"id":"yes","text":"是"},{"id":"no","text":"否"}]";          
                return strJson;
            }

            当选择了Combobox中的值时,仅仅须要把将要运行的操作写在onAfterEdit(index, row, changes)函数中就可以,index为编辑的行号,默认从0開始;row为被编辑单元格所在的整个行,row.列名能够获得此行此列的数据,如row.IsShow能够获得被编辑单元格所在行的IsShow列的数据。


          (此文内容基于Asp.net MVC)


  • 相关阅读:
    ORA-12170 TNS 连接超时
    判断字符串里面是否包含汉字
    关于Try/Catch 代码块
    SOLID 原则
    整洁代码原则
    装箱 拆箱
    存储过程优缺点
    ANTLR Tool version 4.4中使用ANTLR 4.7.1
    The Apache Tomcat installation at this directory is version 8.5.40. A Tomcat 8.0 installation is expected.
    实现功能:构建一个JSON到XML的翻译器。
  • 原文地址:https://www.cnblogs.com/gcczhongduan/p/5254581.html
Copyright © 2011-2022 走看看