简体   繁体   English

Angular JQXGrid单元格渲染器将“值”设置为字符串,但是如果我传入对象,则看起来不对

[英]Angular JQXGrid cellsrender sets “value” as a string, but looks wrong if i pass in an object

The Angular JqxGrid has a cellsrenderer function call on rows i am leveraging. Angular JqxGrid在我利用的行上有一个cellsrenderer函数调用。 It has the arguments: index, datafield, value, defaultvalue, column, rowdata 它具有以下参数: 索引,数据字段,值,默认值,列,行数据

I wanted to do some magic in the cellsrender, but it seems when the column accesses a value which is an object it will stringify it to "[object]" and thats not ideal. 我想在cellsrender中做一些魔术,但是似乎当列访问的值是一个对象时,它将其字符串化为“ [object]”,那并不理想。 I also looked at it in terms of "rowdata" but it is already stringified. 我也从“行数据”的角度看了它,但是它已经被字符串化了。

How do i access the actual object to use its properties for rendering? 如何访问实际对象以使用其属性进行渲染? I tried to access the underlying data list, but when clicking and refreshing the data, it will throw errors due to index not being accessible, so I am a bit town on how to unstring this information. 我尝试访问基础数据列表,但是当单击并刷新数据时,由于无法访问索引,它将引发错误,因此我对如何对这些信息进行解串有点麻烦。

Im tempted to process the raw data to a string before the grid uses it, but i dont think that would be the best thing. 我很想在网格使用原始数据之前将其处理为字符串,但是我认为那不是最好的选择。 I would think that the cellsrenderer should do that. 我认为cellrenderer应该这样做。

My coworker wanted me to assign all properties of the object to the datafield, but i dont like that option. 我的同事希望我将对象的所有属性分配给数据字段,但我不喜欢该选项。 If looking at it as a dynamic solution so im not sure i like adding the values into the datafield for each item i want to leverage in a row's value 如果将其视为一种动态解决方案,那么我不确定我是否喜欢将值添加到我想利用一行值的每个项目的数据字段中

I solved this in a fairly elegant way. 我以一种相当优雅的方式解决了这个问题。

All data which is going to be a part of the dataAdapter, I check for complex objects and stringify them. 所有将成为dataAdapter一部分的数据,我都会检查复杂的对象并将其字符串化。

var data = []; //dataset
data.map( row => {
  let x = {};
  if (obj.hasOwnProperty(v) && obj[v] && obj[v].constructor === {}.constructor){
    x[v] = JSON.stringify(obj[v]);
  }else {
    x[v] = obj[v];
  }
  return x;
});

Now that the data is primed, and the columns are set we go into the cellsrenderer: 现在已经准备好数据,并设置了列,我们进入cellsrenderer:

col.cellsrenderer = (index: number, datafield: string, value: any, defaultvalue: any, column: any, rowdata: any): string => {
  let decode = value;
  try {
    decoded = JSON.parse(value);
  }catch (e){ 
    decoded = value;
  }

  //Now you can do custom stuff with a formatter:
  let formatter = getFormatter(XXXXX);
  let cell = `<div>${formatter.format(decoded)}</div>`; 
  //decoded would be an object OR a fundamental type string, number, etc.

  // When doing cellrenderer, you will only be able to use globalclasses, so custom component based class defintions will not work.  If needed, you would use inline styles, or expose global css/scss for a cell.
  return cell;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM