1

I have data of messages including {id,message,date}. I would like to display a grid for each Date with data{message} in AngularJs using ui-grid

I was thinking of something like this:

<div ng-repeat="(item in data | groupBy: 'Date'">
  <div>{{ item.Date }}</div>
  <div  id="grid1" ui-grid="gridOptions(item.Date) " class="grid"></div>
</div>

but it's not working!

  $scope.gridOptions = function (date) {
    return {
        enableSorting: true,
        enableFiltering: true,
        data: 'data',
        columnDefs: $scope.filterGrid(date),
        onRegisterApi: function (gridApi) {
            $scope.gridApi = gridApi;
        },
       };
};
 $scope.filterGrid= function (date){
    return [
           { field: 'id',  enableFiltering: False},
           {
               field: 'Date', enableFiltering: false, filter: {
                   noTerm: true,
                   condition: function (searchTerm, cellValue) {
                       return cellValue.match(date);
                   }
               }
           },
           { field: 'Message' , enableFiltering: false },
    ]
}
0

2 Answers 2

1

First of all - ui-grid has a grouping feature, but this is still in beta.

You can try to use this example to group the data and build grids accordingly.

 var dataSource = {};

$scope.gridOptions = {};

 var generalGridOptions = {
    enableSorting: true,
    columnDefs: [ 
      { name: 'prop1' },
      { name: 'prop2' },
    ],
    data: null
};

// group the data
for(var i = 0; i < data.length; i++){

  if(!dataSource[data[i].month]){
    dataSource[data[i].month] = [];
  }

  var obj = {};

  for(var prop in data[i]){
    if(prop!='month'){
      obj[prop] = data[i][prop];
    }
  }

  dataSource[data[i].month].push(obj);
}

// build the grid options
for (var item in dataSource) {
    $scope.gridOptions[item] = angular.copy(generalGridOptions);
    $scope.gridOptions[item].data = dataSource[item];
}
Sign up to request clarification or add additional context in comments.

Comments

1

ui-grid attribute recieves a gridOptions object containing several parameters. Two of them are:

  1. columnDefs - defining the columns and their data-binding.
  2. data - the message objects in your case.

Look at the the documentation for further study: http://ui-grid.info/docs/#/tutorial

Code Example

template:

<div ng-repeat='item in items track by item.id' ui-grid='getItemGridOptions($index)'></div>

Passing the item index to the controller allows you to process your data. Then you can return an object containing the data and columnDefs properties. Like this:

 private getItemGridOptions(index): IGridOptions {

        //get the data you need for your items array using the index...

       // then return the gridOptions object (I put random values)
        return {
            columnDefs: this.columns,
            rowHeight: 24,
            rowTemplate: rowtpl,
            enableHorizontalScrollbar: this.uiGridConstants.scrollbars.NEVER,
            enableColumnMenus: false,
            enableRowSelection: true,
            enableRowHeaderSelection: false,
            enableFiltering: true,
            modifierKeysToMultiSelect: true,
            multiSelect: true,
            data: null,
        };
    } 

7 Comments

but how can group by the 'date' and display more one then one grid?
each item in ng-repeat should create dynamically its gridOptions and pass it to the ui-grid attribute. You can call a function responsible for initializing the gridOptions, passing it the item values.
thank you! is that Angular 2? any way of doing this in angular1?
This is written in TypeScript and aimed for Angular1
do you have a way of doing this without TypeScript?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.