简体   繁体   中英

AngularJs Ng-Grid : Multiproperty binding with filter

I have Json object:

[
   {name: "Moroni", age: 50, dob: 30051998, tob: 1005},
   {name: "Tiancum", age: 43, dob: 30051987, tob: 2205},
   {name: "Jacob", age: 27, dob: 30051956, tob: 0605},
   {name: "Nephi", age: 29, dob: 30051978, tob: 1605},
   {name: "Enos", age: 34, dob: 30051965, tob: 1305}
]

Now I want to create one ng-grid, in which there will be one column representing the date and time of birth for each person. Since the data is not properly formatted. So, we want a filter.

var gridOptions1 = {
        data: 'myData',
        columnDefs: [
            { field:"name", displayName: "NAME"},
            { field:"age", displayName: "AGE"},
            { field:"dob+tob", displayName: "Date & Time"}
        ],
        selectedItems: $scope.selected
    };

Please help. Plnkr .

I would separate your initial JSON server data from the data that you are going to push into the ng-grid.

So, before setting your data source for grid2, I would create your view model:

$scope.grid2Data = [];
for(var i = 0; i < $scope.myData.length; i++) {

  $scope.grid2Data.push({

    name: $scope.myData[i].name,
    age: $scope.myData[i].age,
    fullDate: $scope.myData[i].dob.toString() + $scope.myData[i].tob.toString()
  })
}

And then configure the columns for grid2 so that you are using the cellFilter property:

var gridOptions2 = {
        data: 'grid2Data',
        columnDefs: [
            { field:"name", displayName: "Name"},
            { field:"age", displayName: "Age"},
            { field:"fullDate", displayName: "Date & Time", cellFilter:"formatDate:'ddMMyyyyHHmm':'HH:mm MM/dd'"}],
        multiSelect: false,
        selectedItems: $scope.selected
    };

Here is a plunker showing the techniqu e in action.

This works for all but one date (the one at position 3). That date doesn't seem to format correctly with your filter.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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