JqGrid colModel dynamic formatter

吃可爱长大的小学妹 提交于 2020-01-04 09:55:09

问题


My jqGrid work dynamically.So that all options are loaded dynamically too. This options are generated with java Map<String,Object> All options work very well, but the function name within de map/opts not work. This is json map generated with java.

"colModel":[...{"formatter":"myFunction","index":"","name":""}]

I did not debug the jqgrid.src.js yet and i think that the problem are with the eval. the "myFunction" is not called and the undefined is returned.


回答1:


The formatter option can be a string if it is one of the predefined formatters, but according to the jqGrid docs for custom formatters:

You can define your own formatter for a particular column. Usually this is a function.

So jqGrid expects a function to be passed. This is why myFunction works but "myFunction" does not. Anyway, to solve your problem you need to output the code:

"formatter": myFunction


To go a bit deeper, you can see in the jqGrid source file grid.base.js that the formatter uses a function directly, but if a string is passed that string is passed to $.fn.fmatter:
    formatter = function (rowId, cellval , colpos, rwdat, _act){
        var cm = ts.p.colModel[colpos],v;
        if(typeof cm.formatter !== 'undefined') {
            var opts= {rowId: rowId, colModel:cm, gid:ts.p.id, pos:colpos };
            if($.isFunction( cm.formatter ) ) {
                v = cm.formatter.call(ts,cellval,opts,rwdat,_act);
            } else if($.fmatter){
                v = $.fn.fmatter(cm.formatter, cellval,opts, rwdat, _act);
            } else {
                v = cellVal(cellval);
            }
        } else {
            v = cellVal(cellval);
        }
        return v;
    },

If that function is not already present in jquery.fmatter.js then I would expect an error to occur when the grid attempts to use it.



来源:https://stackoverflow.com/questions/10709137/jqgrid-colmodel-dynamic-formatter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!