I am using ui-grid in my project, I need to show the selected row index value in pagination part.
For example, there are total 100 items in my grid, it is divided into 4 pages, each page has 25 items. If I select 10th row from a page, I need show 10 of 25 items
I am using :
HTML
<div ui-grid="gridOptions" ui-grid-selection > </div>
controller:
$scope.gridOptions ={
data:'data',
coulmnDef: $scope.coulmnDef,
paginationPageSizes: [25, 50, 75,100],
paginationPageSize: 25,
};
could you please help me any one, Thanks in advance
You can get the selected rows by listening for rowSelectionChanged event, after which you can use the getSelectedRows method on gridApi's selection object. To do so, add the following to your gridOptions object:
$scope.selection = [];
$scope.gridOptions = {
enableRowSelection: true,
enableRowHeaderSelection: false,
modifierKeysToMultiSelect: true,
multiSelect: true,
onRegisterApi: function (gridApi) {
$scope.gridApi = gridApi;
gridApi.selection.on.rowSelectionChanged($scope, function () {
$scope.selection = gridApi.selection.getSelectedRows();
});
}
};
Now everytime the selection changes, the selection array in your scope gets updated of which you can take the length property which gives you the selection count. Hope that helps, good luck!
Reference: http://ui-grid.info/docs/#/tutorial/210_selection
Related
I have a ui-grid table which has been implemented with Server-side filtering, now i have a scenario that i am passing the filtering term in my api from another page and coming to the ui-grid page with the filtered data for the grid but the selection of the filter for that column is not applied.
Code:
$scope.gridOptions = {
enableSelectAll: false,
exporterCsvFilename: fileNm,
exporterMenuPdf: false,
enableFiltering: true,
showGridFooter: true,
paginationPageSizes: [25, 50, 75],
paginationPageSize: 25,
useExternalPagination: true,
enableGridMenu: true,
..
..
.
Sample columnDef
var sparableColumn = {
name: 'sparable', displayName: 'Sparable', headerCellClass: $scope.highlightFilteredHeader,
filter: {
type: uiGridConstants.filter.SELECT,
selectOptions: $scope.sparableFilter
},
cellTemplate:'<div ng-if="row.entity.sparable" class="tcenter">{{row.entity.sparable}}</div><div ng-if="!row.entity.sparable"></div>',
cellClass: function (grid, row, col) {
if (grid.getCellValue(row, col) === 'NO') {
return 'red tcenter';
}
if(grid.getCellValue(row, col) === 'YES') {
return 'green tcenter';
}
return 'tcenter';
}
};
Server side filter inside onRegisterApi:
onRegisterApi: function (gridApi) {
$scope.gridApi = gridApi;
$scope.gridApi.core.on.filterChanged( $scope, function() {
var grid = this.grid;
$scope.serversideFilters = [];
angular.forEach(grid.columns, function(value, key) {
if(value.filters[0].term) {
var dummy = {};
console.log('FILTER TERM FOR ' + value.colDef.name + ' = ' + value.filters[0].term);
dummy['k'] = value.colDef.name;
dummy['v'] = value.filters[0].term;
$scope.serversideFilters.push(dummy);
}
});
getPage();
});
I know we need to populate or select it via grid.columns.filters but how and where to put the code to activate the filtered column selection is the question.
Thanks in advance!
I've put together a plnkr to outline what I think you are looking for. It is based off of the ui-grid tutorial on Saving State, but shrinks the amount of data you are storing down to the columns array (where each column object contains the filters). To test, add some filter content to the filter headers, and save state. 'X' the filters out, hit restore, and the filters are back.
Down below, I've outlined some ideas on "what to do" with this array.
JS
This code comes from the above mentioned tutorial, with slight modifications to avoid storing the extra data.
Note that $scope.gridOptions.saveFilter = true. The save* options have been set to false to avoid any extra data handling.
// ensure
var app = angular.module('app', ['ui.grid', 'ui.grid.saveState']);
// left out extra code for brevity
var columns = [];
$scope.saveState = function() {
var saved = $scope.gridApi.saveState.save();
columns = saved.columns;
console.log('columns', columns);
};
$scope.restoreState = function() {
$scope.gridApi.saveState.restore($scope, {
columns: columns
});
};
HTML
Directives are "listed" to ensure they stand out.
<div class="grid" ui-grid="gridOptions" ui-grid-save-state>
<button id="save" type="button" class="btn btn-success"
ng-click="saveState()">Save</button>
<button id="restore" type="button" class="btn btn-success"
ng-click="restoreState()">Restore</button>
Stashing the Array
Depending on how you are handling navigation, you could watch for $routeChangeStart, and call saveState():
$scope.$on('$routeChangeStart', function(next, current) {
// Serialize, post/put, etc here
});
When loading the the second page, you can retrieve the data doing something like this:
// app.config
$routeProvider.
when('/page2', {
templateUrl: 'page_2.tpl.htm',
controller: 'Page2Controller',
resolve: {
gridFilters: function($http){
// We must return something with a promise. $http already does that, so does $resource.
// See: https://docs.angularjs.org/api/ng/service/$q
return $http({
method: 'GET',
url: '/api/resource/with/grid/settings'
})
}
}
})
Local Storage
A second approach would be keeping the data locally using a service. The answer provided in passing-data-between-controllers-in-angular-js sums this pattern up perfectly, so it need not be replicated here.
I am using UI grid v3.0.0 in my project. Recently I changed my code to filter grid using drop down list value. After this change, UI grid getting blank when I filter grid multiple times. This is intermittent issue and I am not getting any javascript error. when UI grid get blanked after applying filter, UI grid will remain blank till user move scroll bar of UI grid.
I am using below Grid options.
$scope.options = {
enableColumnMenus: false,
enableSelectAll: true,
enableFiltering: true,
enableHorizontalScrollbar:0,
enableRowHeaderSelection: false,
enableRowSelection: true,
data : [],
showSelectionCheckbox: true,
selectionRowHeaderWidth: 25,
rowHeight: 25,
columnDefs : $scope.columnDefs
};
$scope.options.onRegisterApi = function (gridApi) {
$scope.options = gridApi;
gridApi.selection.on.rowSelectionChanged($rootScope , function(gridApi) {
$scope.options.selected = gridApi;
});
I am also calling below grid api for refresh after filering ui grid array .
$scope.options.core.refresh();
But still it is not working. If anyone can give me the hint about how this problem can be solved, it would me much appreciated. Thanks.
I have never been able to get the core.refresh to work for me. I actually use a workaround. You can try it to see if it helps with this issue.
create Function:
$scope.refresh = false;
function refresh () {
$scope.refresh = true;
$timeout(function () {
$scope.refresh = false;
}, 0);
};
Put an ng-if on your grid div:
<div ng-if="!refresh" ui-grid="gridOptions" class="grid"></div>
Then call refresh() in your controller where/when you want to refresh the grid.
I hope this helps!
You need to set scroll position after refresh like below :
$scope.options.core.refresh();
$scope.options.core.scrollTo($scope.data[0], $scope.columnDefs[0]);
You can use $filter of angular to filter your kendo grid dataSource.
You can take the reference from following code, let me know anything else you required.
var ds = $filter('filter')($scope.grdDataSource.dataSource.data, $scope.searchText);
$scope.kendoGridOptions.dataSource.data = ds;
I have a grid displaying data. I want to be able to route to a new view with a click of a grid row. Right now I am unable to even register a row click/selection. I have tried in my grid definition:
onRegisterApi: function (gridApi ) {
$scope.gridApi = gridApi;
gridApi.selection.on.onRowSelectionChanged($scope, function (rows) {
$scope.mySelections = gridApi.selection.getSelectedRows();
});
},
with a call to mySelections in the html:
<p>{{mySelections}}</p>
this results in error : Cannot read property 'on' of undefined. However, I can't tell what is 'undefined'.
I have also tried a separate function:
$scope.gridOptions.onRegisterApi = function (gridApi) {
$scope.gridApi = gridApi;
gridApi.selection.on.rowSelectionChanged($scope, function (row) {
alert(row.isSelected);
//$scope.mySelections = gridApi.selection.getSelectedRows();
});
};
but it returns the same error.
I have added ui.grid and ui.grid.selection to my angular.module. I would love to find an actual example of using rowselect to link to a new page, but I have to find anything.
Cannot read property 'on' of undefined.
Suggests that the selection property is not available on your gridApi object, i'de double check if you really added the dependencies the right way:
angular.module('app', [
'ui.grid',
'ui.grid.selection'
]);
Next, calling gridApi.selection.on.onRowSelectionChanged won't work because the method is called rowSelectionChanged not onRowSelectionChanged. You've got that right in your second example. For the rest your code seems ok, perhaps there's something wrong in the rest of your grid definition. Here's one that works with an example using ui.router for view change:
$scope.gridOptions = {
enableRowSelection: true,
enableRowHeaderSelection: false,
multiSelect: false,
onRegisterApi: function (gridApi) {
$scope.gridApi = gridApi;
gridApi.selection.on.rowSelectionChanged($scope, function (rows) {
var selection = gridApi.selection.getSelectedRows();
$state.go('item', {item: selection[0]});
});
}
};
http://plnkr.co/edit/Rs9M6pJd83vK8syr3W9g?p=preview
Don't forget to add the "ui-grid-selection" attribut to your DIV :
<div class="gridStyle" ui-grid="gridOptions" ui-grid-selection ... ></div>
In my Angular apps I am using UI Grid (http://ui-grid.info/docs/#/tutorial)
How do I get the data of the selected row ?
Plunker : http://plnkr.co/edit/GpsfCHewYjhiPcwAQheA?p=preview
HTML:
<div ui-grid="gridOptions" ui-grid-selection class="grid"></div>
The grid selection module has a method to get the selected rows: getSelectedRows().
You can access this method through the gridApi if you define your options like so:
$scope.gridOptions = {
columnDefs: $scope.columns,
onRegisterApi: function(gridApi) {
$scope.gridApi = gridApi;
}
};
Please find the ng grid example in plunker
http://plnkr.co/edit/CncDWCktXTuBQdDVfuVv?p=preview
It will allow user to select only one row but there will be one selected row at all time. I want to deselect all rows.
ng-grid has keepLastSelected option.
Try:
keepLastSelected: false in gridOptions. This will toggle selection.
Example
$scope.gridOptions = {
data: 'myData',
selectedItems: $scope.mySelections,
multiSelect: false,
keepLastSelected: false
};
The following works for me:
Plunker
First you need store gridApi:
$scope.gridOptions.onRegisterApi = function (gridApi) {
$scope.gridApi = gridApi;
};
Then you can use:
$scope.unSelectAll = function(){
$scope.gridApi.selection.clearSelectedRows();
}
simply :
$scope.gridApi.selection.clearSelectedRows()
For Selecting ALL Rows We Use ::
$scope.GridOptions.api.selectAll()
Here $scope.GridOptions is your own grid data
And For Deselecting All Rows We use::
$scope.gridOptions.api.deselectAll();
Here (.api) is file and you get this file from
(ag-grid.js) file from internet
Url:https://cdnjs.com/libraries/ag-grid