Skip to content

关于表格列属性 templet 的函数参数说明 #3

Open
@cshaptx4869

Description

问题描述:

  • 渲染 table 的自定义模板 templet 属性的时候,比如渲染图片。在easyadmin 中可以使用 {templet: ea.table.image} 配置渲染 , 他会自动传入两个参数 data, option 。而在 layui 的 {templet: function(d){}} 配置的回调函数中只会传入第一个参数 d。请问 easyadmin 中他是怎么处理的,会传入2个参数...

问题解答:

  • 从结果上看

easyadmin 中修改了 layui.all.js 源文件 return "function" == typeof e.templet ? e.templet(l) : i(t(e.templet).html() || String(a)).render(l) 的 e.templet(l) 为 e.templet(l, e)

  • 具体分析

easyadmin 总计有两处 layui 2.5.6 源码修改:

  • src/lay/modules/table.js
parseTempData = function(item3, content, tplData, text){ //表头数据、原始内容、表体数据、是否只返回文本
    var str = item3.templet ? function(){
      return typeof item3.templet === 'function' 
        ? item3.templet(tplData, item3) // 改动处:此处需额外传入 item3 参数
      : laytpl($(item3.templet).html() || String(content)).render(tplData) 
    }() : content;
    return text ? $('<div>'+ str +'</div>').text() : str;
  }
  • src/layui.js
search: function(){
        var obj = {}
        ,search = (href 
          ? function () { var path = (href.match(/\?.+/) || [])[0] || ''; return path.replace(/\#.+/, '')} () // 改动处:此处增加#后面的字符串去除
          : location.search
        ).replace(/^\?+/, '').split('&'); //去除 ?,按 & 分割参数
        
       //代码省略...
      }()

Tips:

  • 修改源码后记得重新 gulp 打包
  • node 版本不能太高,否则会报 fs.js:42 } = primordials; 错误,因为 node 12.0 的版本之后(包括12.0版本)不支持 gulp4.0 之前的版本,实测本地使用 node11 没有报错, node16 就有问题
  • 这样重新打包后获得的 layui.all.js 文件就和 easyadmin 中的完全一样了

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions