Appearance

$grid.newGrid

$grid.newGrid方法是在easyUI的dataGrid上进行了二次封装,主要封装了gird的工具栏,加部分默认参数设置, 详细参数设置请在查看此文档的时候也查看easyUI的API文档

运行效果

progress

示例代码

$grid.newGrid("#gridBox",{
  tools:[//工具栏按钮
    [//按钮组
      {
        iconCls:'plus',//图标
        text:'新增',//文字
        url:'userForm.html',//请求链接
        popMax:true,//默认采用popIframe弹窗模式,popMax为ture是全屏展示
        title:'用户信息-新增'//弹窗标题
      },
      {
        iconCls:'pencil',text:'修改',
        onlyOne:true,//只能操作一条记录
        notNull:'请选择你要修改的记录!',//为true必须选择记录行,内容为没有选择任何一条记录的提示
        popHeight:400,popWidth:560,//弹窗pop宽高
        url:'userForm.html?id={id}',//请求url地址,{}内为选择行的参数值,如果多选,值用逗号隔开
        title:'用户信息-修改'
      },
      {
        iconCls:'trash',text:'删除',
        check:true,//true为行check模式,默认false为行select模式
        url:'json/true.js',
        post:'userId=id&userName=name',//url请求为post模式,
        notNull:'请 <strong class="red">勾选</strong> 需要删除的一项或多项!',
        ajax:true,//请求为ajax模式,直接向服务器发送ajax请求
        ajaxMsg:'你确定提交此删除操作吗?'//ajax提示信息
        }
      ]//按钮组结束
    ,[//按钮组
      {
        iconCls:'refresh',
        text:'重置密码',
        click: function(){
            var rows = $('#gridBox').datagrid('getChecked'); //获取勾选行
            // var row = $('#gridBox').datagrid('getSelected'); //获取选中行
            var ids = rows.map((row) => row.id);
            ids = ids.join(',');
            $ajax.post({ //发送请求
                url: 'restPass.jhtml',
                data: {ids: ids},
                ...
            });
        }
      },
      {
        html: function($par,$grid,opt){//工具栏 插入html
          return '<input class="txt txt-number" name="num" data-options="min:0,precision:2"><button class="btn btn-small btn-primary" type="button">搜索</button>'
        },
        render : function($par,$grid,opt){//对插入的html代码进行事件初始化 $par:工具栏盒对象,$grid:grid对象,opt:当前工具行参数,比如 opt.someOpt 得到 cc
          var num = $par.find('.txt-number').val();
          $par.find('.btn').click(function(){
            $grid.datagrid('reload',{num:num});
          });
        },
        someOpt : 'cc'
      }
    ]//按钮组结束
  ],
  rownumbers : false,//是否显示rownumber,参考easyui原api
  fitColumns : false,//列是否自动展开,参考easyui原api
  singleSelect : false,//是否单选,参考easyui原api
  checkOnSelect : false,//check的时候同时select,参考easyui原api
  selectOnCheck : false,//select的时候同时check,参考easyui原api
  columns:[[//数据行
    {title:'id',field:'id',checkbox:true},
    {title:'用户名',field:'userName',sortable:true,width:100},//sortable打开单击表头排序功能
    {title:'姓名',field:'realName',sortable:true,width:100},
    {title:'性别',field:'sex',sortable:true,width:100,formatter:function(value,rows,index){//formatter自定义返回内容,参考easyui原api
          return ['','男','女'][value];
        }
      },
    {title:'创建日期',field:'createTime',width:150,format:'yyyy-MM-dd hh:mm:ss'}//format只支持时间format
    ,{title:'最后修改时间',field:'lastModifyTime',width:150}
  ]],
//   loader: function(params,success,error){//自定义请求数据函数 params为请求参数
//     return success([{userName:'张三',sex:'1'}]);
//   },
  onClickRow: function(index, row){}, //单击行
  onDblClickRow: function(index, row){}, //双击行
  onClickCell: function(index, row){}, //单击单元格
  onCheck: function(index, row){}, //当勾选行
  onUncheck: function(index, row){}, //当取消勾选行
  onBeforeLoad: function(params){ //请求前拦截函数
    if(!params.deptId){return false;} //r不满足条件,return false 中断请求
  },
  onLoadSuccess: function(data){
    console.log(data.rows,data.total);
  },
  url:'json/users.js'//grid请求地址
  ,offset : -50//高度偏移量,目的使grid表格高度正好适应窗口
});
<div class="gridWrap">
    <div id="gridBox"></div>
</div>

newGrid 新增属性

  • newGrid 在表格高度上做了自适应,默认高度是 页面高度 - 搜索栏高度, 如果页面中除了表格和工具栏,你还有其他元素需要占用高度,你可以通过 offset 属性增减 匹配让高度自适应全屏。
  • 除此之外,为了让grid 高度可以尽量灵活适应页面布局,我们还增加了 fitHeightfitParent属性,具体用途如下表。
  • 如果你自定义 grid 高度,可以 将 height 属性设置为 auto 或具体的 高度数值。
属性名属性值类型默认值描述
fitHeightbooleantrue随视窗缩放,自适应窗口,需定义固定高度时,设为false或者将 height设置为 auto均可,相关属性 offset height
fitParentbooleanfalsegrid自适应父级元素宽高,设定此属性后,表格会自动适应父级盒子高度,一般用在需要自适应的布局中, 父级盒子会采用绝对定位来自适应页面宽高。

columns 新增属性

属性名属性值类型默认值描述
formatstring''用日期函数 $.fmtDate来格式化列值
pricebooleanfalse为true,格式化为2位小数,右对齐
titletipbooleantrue以title的方式显示单元格内容
sumbooleanfalse为需要合计的列

datagrid 新增方法

totalNumber

$(grid).datagrid('totalNumber',{
  type : 'footer',//默认为footer
  extendData : {},//其他需要显示的数据,对应列field显示
  precision:2,//合计保留2为小数
  showSum : true,//合计
  totalTxt : '<b class="\'b-totalT\'" style="\'color:#000;\'">合计:</b>',//合计标题文案,没有特殊需求不需要修改
  totalTxtField:opt[0][0].field//合计标题显示所在 field
});

tools按钮参数列表

属性名属性值类型默认值描述
htmlstring / ($par,$grid,opt) => stringhtml模式插入页面,可以是字符串或者函数返回字符串,结合 render函数处理html代码
renderfunctionfunction($par,$grid,opt){}只在html模式下有效,用于处理插入后的html事件,3个参数分别为 $par:工具栏对象,$grid:grid对象,opt:当前工具行参数
iconClsstringplus按钮图标
btnTitlestringnull按钮标题
textstring新增按钮文本
urlstringnull请求地址,如 :userForm.html?id={id}{}里为行返回的field值,如选择的是多行,返回的field为,隔开的字符串
popMaxbooleanfalseiframe弹窗模式,弹窗是否最大化
popWidthnumber560弹窗宽度
popHeightnumber300弹窗高度
ajaxMsgstring你确定提交此操作吗?弹窗宽度
popHeightnumber300弹窗高度
titlestring信息窗口iframe弹窗模式,弹窗标题
checkbooleanfalse是否返回是check的值,即勾选行,默认返回select的值,即选择行
notNullboolean/stringfalse为true时,grid必须选择行,为提示文字(string)时,没有选择行弹出提示文字
onlyOnebooleanfalse为true时,grid操作行只能选择一行
newWinbooleanfalse为true时,url对应的页面在新窗口打开
ajaxbooleanfalse为true时,url对应的为ajax请求
poststringfalse此参数为ajax请求的延伸参数, 当ajax为true时,默认url请求传递参数的方法为:userForm.html?id={id}{}里为行返回的field值, 当post为true时,传参方式通过post值给出,返回,如:url:'userForm.html',post:'userId=id&name=name',则返回给服务器的url请求为(举例): userForm.html?userId=122&name=zhanghua,122为field id的值 注意,这里post模式返回和默认方式不同的是,post模式回传的是参数的序列,和表单同步提交的方式相同
endBackfunctionfunction () {}iframe弹窗模式,关闭时返回事件
ajaxBackfunctionfunction (rst) {}ajax模式,请求后自自定义事件
clickfunctionfunction () {}自定义click事件,之前所有的参数设置皆为浮云