In ui-grid, I want to use a function for the colDef's field property. How can I pass in another function as a parameter to it?

徘徊边缘 提交于 2019-12-06 22:50:28

This is due to the gridUtil.preEval function wich prepares the string you put in your field attribute.

It tranlsates:

  • foo.bar to foo['bar']
  • foo.bar(arg) to foo'bar'
  • foo.bar(function() {...}) to foo['bar(function() {}']

The last one is your case, but the resulting string seems not evaluable to me (using angular $parse method).

I was looking throug it and it looks like a string like the last one:

foo.bar(function() {...})

is just not compatible with $parse method.

I'd suggest you to go with a custom cell template, you still would not be able to use nested function inside an angular expression (the ones with the curly brackets {{ ... }}) but you would have access to more variables like:

  • grid.appScope: the scope wherein the grid resides.
  • row: the row of the cell with custom template, row.entity gives access to entity values.
  • col: the column of the cell with custom template, col.colDef gives access to column definition

You can see more here and I edited your plunker to use this, here.

So, I was looking at the plunkr in the post linked from the OP and a couple script links were broken, so here's a direct link with those fixed: http://plnkr.co/edit/jAyqrKZ6gGa4Xrp7NRsq?p=preview

I'll say I like that it works, using the function string in the field property. I don't like however that I have to add that function to each row of data, especially if there are 1000's of rows.

    angular.forEach($scope.myData,function(row){
      row.getNameAndAge = function(){
        return this.name + '-' + this.age;
      }
    });

I haven't found how to do this with an anonymous function, but that would look cleaner to me.

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