[转]jQuery EasyUI自定义DataGrid的Editor

原文地址:http://www.jeasyuicn.com/post-3.html

官网datagrid的api:http://jquery-easyui.wikidot.com/document:datagrid

首先,先看看官方的editor的介绍:

可以看到如果我们要自定义一个editor,需要实现四个方法(init,getValue,setValue,resize)。

下面是我自己扩展的一个datetimebox类型

01 $.extend($.fn.datagrid.defaults.editors, {
02      datetimebox: {//datetimebox就是你要自定义editor的名称
03          init: function(container, options){
04              var input = $(‘<input class="easyuidatetimebox">‘).appendTo(container);
05              return input.datetimebox({
06                  formatter:function(date){
07                      return new Date(date).format("yyyy-MM-dd hh:mm:ss");
08                  }
09              });
10          },
11          getValue: function(target){
12              return $(target).parent().find(‘input.combo-value‘).val();
13         },
14          setValue: function(target, value){
15              $(target).datetimebox("setValue",value);
16          },
17          resize: function(target, width){
18             var input = $(target);
19             if ($.boxModel == true){
20                  input.width(width - (input.outerWidth() - input.width()));
21             } else {
22                  input.width(width);
23              }
24          }
25      }
26 });

这是最终出来的效果:

使用方法:

<th field="datetime" width="150" editor="datetimebox">datetime</th>

或者:

在配置里面

{

field:"dataTime",

editor:"datetimebox"

}

一般我们只许要注意init,getValue和setValue这三个方法,最主要的还是init的方法的实现。需要注意的是,这里的set和getValue方法都是针对于editor的,是给editor设值和获取值的。

上面例子中涉及到的format方法:

 1 //时间格式化
 2 Date.prototype.format = function(format){
 3     /*
 4      * eg:format="yyyy-MM-dd hh:mm:ss";
 5      */
 6     if(!format){
 7         format = "yyyy-MM-dd hh:mm:ss";
 8     }
 9
10     var o = {
11             "M+": this.getMonth() + 1, // month
12             "d+": this.getDate(), // day
13             "h+": this.getHours(), // hour
14             "m+": this.getMinutes(), // minute
15             "s+": this.getSeconds(), // second
16            "q+": Math.floor((this.getMonth() + 3) / 3), // quarter
17            "S": this.getMilliseconds()
18             // millisecond
19    };
20
21    if (/(y+)/.test(format)) {
22         format = format.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
23     }
24
25     for (var k in o) {
26         if (new RegExp("(" + k + ")").test(format)) {
27             format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" +o[k]).length));
28        }
29     }
30     return format;
31 };
时间: 2024-12-22 10:48:37

[转]jQuery EasyUI自定义DataGrid的Editor的相关文章

JQuery EasyUI的datagrid的使用方式总结

JQuery EasyUI的datagrid的使用方式总结第一步:添加样式和js脚本在前台添加展示数据表格的table元素 例如: <div> <table id="tt" style="width: 700px;" title="标题,可以使用代码进行初始化,也可以使用这种属性的方式" iconcls="icon-edit"> </table> </div> 注:表格的属性可以

给Jquery easyui 的datagrid 每行增加操作链接

我们都知道Jquery的Easy-UI的datagrid可以添加并且自定义Toolbar,这样我们选择一行然后选择toolbar的相应按钮就可以对这行的数据进行操作.但实际项目里我们可能需要在每行后面加一些操作链接,最常见的就是比如"修改"."删除"."查看"之类.如下图: 凡事都怕但是!Easy-UI的Datagrid没有直接添加link的属性.查看Easy-UI的帮助文档,看到一个formater:格式化函数,可以对某一行进行格式化,然后通过

JQuery EasyUI之DataGrid列名和数据列分别设置不同对齐方式(转)

需求如下 现有数据列三列 Name,Age,CreateDate 数据 张三,18,2000-12-09 :12:34:56 李四,28,2000-12-09 :12:34:56 王麻子,38,2000-12-09 :12:34:56 Jquery Easyui DataGrid中列设置 { field: 'Name', title: '名称', width: 120 ,align:left},{field: 'Age', title: '年龄', width: 120 ,align:right

jQuery EasyUI 在datagrid上使用combotree 进行多选

datagrid本身有编辑功能,根据官方说明,在需要编辑的列上,加上editor 属性即可.具体的类型有以下几种: text,textarea,checkbox,numberbox,validatebox,datebox,combobox,combotree. 最近想利用combotree实现一个可以多选的树,途中遇到一些问题,放到这里分享一下: 1. 基本写法: editor="{type:'combotree',options:{url:'datagrid_data.json',multip

jQuery EasyUI之DataGrid使用示例

jQuery EasyUI是一个轻量级的Web前端开发框架,提供了很多的现成组件帮助程序员减轻前端代码开发量,之前有个项目中就用到了其中的DataGrid. jQuery EasyUI框架的官方主页:http://www.jeasyui.com/demo/main/index.php.可以下载完整开发包,里面有示例代码可以参考. 由于我使用的是ASP.NET webform技术,下面我就贴出主要的代码以供参考. 在页面中首先要引用相关的css以及js文件,这样才能使用该组件. css部分:  <

jQuery easyUI 使用 datagrid 表格

获取后台数据依旧是使用一般处理程序(ashx) ,分页上添加一个函数(pagerFilter(data)) 前端代码: <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="dataGrid.aspx.cs" Inherits="Web.Management.dataGrid" %> <!DOCTYPE html> <html xmln

JQuery EasyUI 之 DataGrid

1.动态创建datagrid 在页面上添加一个div或table标签,然后用jquery获取这个标签,并初始化一个datagrid.代码如下: (1)页面上添加div标签 <div id="dataGrid"></div> (2)用js动态初始化: $(function () { $('#dataGrid').datagrid({ height: 340, url: '<%=path%>/AdminCenter/SysFunction/GetData

jQuery easyUI的datagrid,如何在翻页以后仍能记录被选中的行

1.先给出问题解决后的代码 1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 2 <%@ taglib prefix="c" uri="http://java.sun.com/jstl/core_rt"%> 3 <% 4 String path = request.getContextP

jquery easyui的datagrid在初始化的时候会请求两次URL?

html代码中利用class声明了datagrid,导致easyUI解析class代码的时候先解析class声明中的datagrid,这样组件就请求了一次url:然后又调用js初始化代码请求一次url.这样导致了重复加载,解决的方法就是只用一种初始化方法来声明easyUI组件以避免重复的提交请求,即删除html中的class声明(class="easyui-datagrid") 即:<table id="DataGrid" class="easyui