Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript wijgrid中是否可能有object类型的单元格?_Javascript_Knockout.js_Wijmo_Wijgrid - Fatal编程技术网

Javascript wijgrid中是否可能有object类型的单元格?

Javascript wijgrid中是否可能有object类型的单元格?,javascript,knockout.js,wijmo,wijgrid,Javascript,Knockout.js,Wijmo,Wijgrid,我使用敲除可观察数组填充wijgrid。在wijgrid中,我想使用JavaScript对象作为某些单元格的值。不幸的是,wijmo似乎在自己的模型中将对象转换为字符串 请喝一杯。我希望在表中显示车主姓名,但我还需要保留id(和车型数据结构) 淘汰视图模型 var someData =[ { AssetCode: "Truck 5", Owner: { id: 1, name: 'Pete'},

我使用敲除可观察数组填充wijgrid。在wijgrid中,我想使用JavaScript对象作为某些单元格的值。不幸的是,wijmo似乎在自己的模型中将对象转换为字符串

请喝一杯。我希望在表中显示车主姓名,但我还需要保留id(和车型数据结构)

淘汰视图模型

var someData =[ { AssetCode: "Truck 5",
              Owner: {
                 id: 1,
                 name: 'Pete'},
              VIN: "T3SN2ADN",
              Odo: 232109,
              TimeStamp: "2012-07-21T09:13:12Z"},
            { AssetCode: "Car 8",
              Owner: {
                 id: 3,
                 name: 'Brian'},
              VIN: "COFAQ211",
              Odo: 433299,
              TimeStamp: "2012-07-17T15:34:54Z"}];

function ViewModel() {
    var self = this;
    self.gridData = ko.observableArray(someData);
}

ko.applyBindings(new ViewModel());
wijgrid

<table id="t1" data-bind="wijgrid: {
    data: gridData,
    columns: [
    { headerText: 'Asset Code', dataKey: 'AssetCode', dataType: 'string'},
    { headerText: 'Owner name', dataKey: 'Owner'},         <!-- PROBLEM LINE -->
    { headerText: 'VIN', dataKey: 'VIN', dataType: 'string' },
    { headerText: 'Odometer', dateKey: 'Odo', dataType: 'number' },
    { headerText: 'Time', dataKey: 'TimeStamp', dataType: 'datetime', dataFormatString: timePattern }

]}"></table>

您可以通过如下方式处理cellStyleFormatter,将自定义值设置为任何单元格(或在您的案例中显示车主姓名):

cellStyleFormatter: function (args) {
//check for specific column and header row
if (args.column.headerText == "Owner name" && args.row.dataRowIndex >= 0) {
     //set the custom value to cell i.e. vehicle owner name
     args.$cell.text(args.row.data.Owner.name);
 }
}

有关CellStyleFormatter的更多信息,您可以参考:

好的,您可以使用
cellFormatter
获取单元格值作为对象。。。要使其正常工作,不要指定
dataKey
属性。以下是经修订的守则:

<table id="t1" data-bind="wijgrid: {
    data: gridData,
    columns: [
        { headerText: 'Asset Code', dataKey: 'AssetCode', dataType: 'string'},
        { headerText: 'Owner name', cellFormatter: MY_FORMATTER},         <!-- FIXED LINE -->
        { headerText: 'VIN', dataKey: 'VIN', dataType: 'string' },
        { headerText: 'Odometer', dateKey: 'Odo', dataType: 'number' },
        { headerText: 'Time', dataKey: 'TimeStamp', dataType: 'datetime', dataFormatString: timePattern }
]}"></table>

不幸的是,
cellStyleFormatter
cellFormatter
存在相同的问题。
args.row.data.Owner的值是:“[object]”。它是一个字符串而不是所有者对象。这对我不起作用(不过我忘了说谢谢你的回答。如果你还有其他想法,我很想试试!
var MY_FORMATTER = function(args) {
  if (args.row.data && args.row.dataRowIndex >= 0) {
    args.formattedValue = args.row.data.Owner.name;
  } 
};