Angular JS - UI Grid EnableFullRowSelection rowSelectionChanged event [duplicate] - angularjs

This question already has an answer here:
UI-Grid with button in custom cell template - How to cancel row select event?
(1 answer)
Closed 7 years ago.
I am using ui-grid for grid. I want to create a grid with some columns from the database and an extra column for a button.
$scope.studentgrid = {
data: 'students',
enableFiltering: false,
onRegisterApi: function(gridApi){
$scope.gridApi = gridApi;
$scope.gridApi.grid.registerRowsProcessor( $scope.singleFilter, 200 );
},
enableRowSelection: true,
multiSelect: false,
enableColumnResizing: true,
enableSelectAll:true,
enableCellEdit: false,
enableFullRowSelection: true,
enableCellEditOnFocus: false,
columnDefs: [
{ field: 'ID'},
{ field: 'name'},
{ field: 'age' },
{ field: 'email', displayName: 'Email(Sorting Disabled)', enableSorting: false },
{ field: 'Change', cellTemplate: '<div><button ng-click="grid.appScope.genalert()">Click Here</button></div>'}
]
};
In ui-grid, I have register a callback function on the event rowSelectionChanged
$scope.studentgrid.onRegisterApi = function (gridApi) {
$scope.gridApi = gridApi;
gridApi.selection.on.rowSelectionChanged($scope, function (row) {
alert(row.isSelected);
});
};
Each time when I click on the button the rowSelectionChanged event is firing. How can I stop rowSelectionChanged event to be fired when I click on button? I need enableFullRowSelection to be true.

I have not found a way to swallow the rowSelectionChanged event. I did find a way to work around it though, which may or may not be useful in your specific case. In my case I had a specific action that happened on rowSelectionChange that I wanted to prevent happening when the button was clicked. If that works for you here's how I'd do it.
Alter your cellTemplate to include a buttonClicked boolean:
cellTemplate: '<div><button ng-click="grid.appScope.buttonClicked = true;grid.appScope.genalert()">Click Here</button></div>'
Then in your rowSelectionChanged declaration:
gridApi.selection.on.rowSelectionChanged($scope, function (row) {
if (!this.grid.appScope.buttonClicked) {
alert(row.isSelected);
}
this.grid.appScope.buttonClicked = false;
});

Related

Angular ui-grid disable cell focus for row selection checkboxes

I have a ui-grid with several columns. Two of those columns are editable. I have also enabled row selection which makes the checkboxes appear in the left most column of the row.
I have disabled cell focus for non-editable columns. What I want to achieve is when I edit one of the editable column cell and hit tab, it should navigate only between the editable columns. The problem with the current setup is that the focus shifts from the editable cells in a row to the selection checkbox in the next row instead of going to next editable cell. I want to disable focus for the selection checkboxes.
vm.tableOptions = {
appScopeProvider: vm,
data: [],
enableSorting: true,
enableFiltering: true,
enableRowSelection: true,
enableColumnResizing: true,
enableSelectAll: true,
multiSelect: true,
enableGridMenu: true,
virtualizationThreshold: 18,
fastWatch: true,
scrollDebounce: 500,
wheelScrollThrottle: 500,
rowHeight: 40,
headerRowHeight: 40,
minRowsToShow: 15,
paginationPageSize: 25,
paginationPageSizes: [],
treeRowHeaderAlwaysVisible: false,
saveScroll: true,
saveGroupingExpandedStates: true,
enableCellEditOnFocus: true,
onRegisterApi: function (gridApi) {
vm.gridApi = gridApi;
/**
* Checks if any rows are selected in the tasks grid.
* #returns True if any tasks are selected; otherwise, false.
*/
function anyRowsSelected() {
var selectedRows = vm.gridApi.selection.getSelectedRows();
return selectedRows ? selectedRows.length > 0 : false;
};
// Check if any tasks are selected when the selection changes
gridApi.selection.on.rowSelectionChanged(null, function (row) {
vm.isRowsSelected = anyRowsSelected();
});
// Check if any tasks are selected when batch selection changes
gridApi.selection.on.rowSelectionChangedBatch(null, function (rows) {
vm.isRowsSelected = anyRowsSelected();
});
// This is a hack to manually move the focus to next editable cell
// I want to avoid this since this is causing some issues.
gridApi.cellNav.on.navigate($scope, function (newRowCol, oldRowCol) {
var assetColIndex = 4;
if (newRowCol.col.name === "selectionRowHeaderCol" &&
oldRowCol.col.name === "SerialNumber") {
vm.gridApi.cellNav.scrollToFocus(newRowCol.row.entity, vm.tableOptions.columnDefs[assetColIndex]);
}
});
$timeout(vm.restoreGridState, 50);
},
columnDefs: [
{ name: "DiscreteSkuId", enableCellEdit: false, allowCellFocus: false },
{
name: "SlotBin",
cellTemplate: slotBinCellTemplate,
enableCellEdit: false,
allowCellFocus: false,
sort: {
direction: 'desc',
priority: 0
}
},
{ name: "Model", enableCellEdit: false, allowCellFocus: false },
{ name: "AssetType", enableCellEdit: false, allowCellFocus: false },
{ name: "AssetTag" , cellTemplate: editableCellTemplate, cellEditableCondition: allowTableEdit },
{ name: "SerialNumber" , cellTemplate: editableCellTemplate, cellEditableCondition: allowTableEdit },
{ name: "ValidationFailureReasons", cellTemplate: errorMessageCellTemplate, enableCellEdit: false, allowCellFocus: false }
]
};
This is in the html page.
<div id="table" ui-grid="rackItemDetailsCtrl.tableOptions" ng-disabled="rackItemDetailsCtrl.disableUserInteraction" class="grid tasks-table ui-grid-row-hover" ui-grid-save-state ui-grid-resize-columns ui-grid-selection ui-grid-grouping ui-grid-move-columns ui-grid-auto-resize ui-grid-scrolling-fix ui-grid-edit ui-grid-cellNav></div>
Angular ui-grid version: 3.1.0.1
Angularjs version: 1.4.9
You can disable cell focus on a column with the allowCellFocus flag. However, the row selection column is created automatically and you have no way to set this flag. The solution to to manually access the column after it is created and set allowCellFocus to false. Here is how you can do that:
gridApi.grid.getColumn('selectionRowHeaderCol').colDef.allowCellFocus = false;

UI-Grid with button in custom cell template - How to cancel row select event?

I am using ui-Grid v 3.0.1.
I have a custom cell template for a particular column which displays a button in each row. I have attached a ng-click attribute which calls the appScope to trigger some action.
All works beautifully.
However, the clicking of the custom template button also causes the selection of the row in question to be toggled.
I need to prevent this from happening.
i.e. a click to the custom template button should leave the current row's selection status as it was.
I suspect I need to cancel the row selection event somehow.
Can anyone point me in the right direction?
vm.gridOptions = {
data: "vm.myGridData",
columnDefs: [
{ field: "Col1", displayName: "Col1", width: 100 },
{ field: "Col2", displayName: "Col2", width: 200 },
{ name: " ", cellTemplate: "<div><button ng-click='grid.appScope.displayRecord(row.entity)'><i class='fa fa-search'></i></button></div>" }
],
enableRowSelection: true,
enableSelectAll: true,
showGridFooter: true,
multiSelect: true,
enableSorting: true,
enableFiltering: true,
enableFullRowSelection: true,
enableRowHeaderSelection: true
};
Just add
$event.stopPropagation();
to the ng-click attribute as you can see in this plunker.
You can chain javascript calls inside your ng-click attribute just by writing them one next to another with a ; as a separator like this:
ng-click = "instructionOne(); instructionTwo(argument); $event.stopPropagation();"

Add Action Buttons to each row in ng-grid

I have a ng-grid on my page which is used to display details.
Is there a way to add action buttons like edit or delete to my ng-grid?
Or any property in gridOpts that needs to be set so as to enable the edit and delete button.
Also, On click of the button how will I get the details of the row selected.
Here is the code for my ng-grid.
$scope.gridOptions = {
paginationPageSizes: [25, 50, 75],
paginationPageSize: 25,
multiSelect: false,
enableCellEdit: true,
enableRowSelection: true,
enableColumnResize: true,
enableCellSelection: true,
columnDefs: [
{ name: 'Name' },
{ name: 'Description' },
{ name: 'FinalModuleWisePrivileges' },
{ name: 'FinalFunctionWisePrivileges' },
{ name: 'Active' },
]
};
HTML:
I tried options like enableCellEdit and enableRowSelection but they dont seem to work.
Would this have to be done by using a loop when the grid is loaded?
I also tried to look at the following reference but it didn't help much.
ng-grid how to enable Edit and Delete buttons
Edit: I added the following line of code to the gridOptions. This solves the temporary purpose but is there a neat way to do this?
cellTemplate: '<button ng-click="grid.appScope.editClicked(row)" ng-if="row.entity.Active == true">Edit</button>'
you would need to add a column in ColumnDefs with a custom cell template..
columnDefs: [{ field: 'name', displayName: 'Name'},
{ field: 'description', displayName: 'Description'},
{ displayName: 'Actions', cellTemplate:
'<div class="grid-action-cell">'+
'<a ng-click="deleteThisRow(row.entity);" >Delete</a></div>'}
]
};
this example shows how to add custom Delete button
the example code is taken from here

Angular-ui-grid : Disable on-click sort for the header cells

I want to disable the sort for on-click event of the header cell and the sort option should only be available in the header drop down menu. I have tried using enableSorting property to disable to sort for on-click event. But, it is also removing the sort option from the header drop down too.
Here is the plunker link: http://plnkr.co/edit/J6XnIAb7E3MZe62Ixax3?p=preview
HTML:
<div id="grid1" ui-grid="gridOptions1" class="grid"></div>
JS:
$scope.gridOptions1 = {
enableSorting: true,
columnDefs: [
{ field: 'name' },
{ field: 'gender' },
{ field: 'company', enableSorting: false }
],
onRegisterApi: function( gridApi ) {
$scope.grid1Api = gridApi;
}
};

ng grid how to change style of individual cell in nggrid?

Just starting out with the ng-grid , how can I change the css of an individual cell in a grid.
Things like backgroundcolor but also disable and enabling of just 1 cell in the current selected row?
plunkr:http://plnkr.co/edit/zQTRd7hOR6vpVZlgfHq0?p=preview
If you define your columns with columnDefs, then you have much more flexibility. You can then add a property to the definition called cellClass that you can define in your stylesheet.
All of the column definition properties are displayed on the ng-grid wiki. If you want you can redefine the cell or header template and write custom code for anything you want.
Here is an updated plunker.
And the changed code:
$scope.gridOptions = {
data: 'myData',
selectedItems: $scope.mySelections,
enableCellEditOnFocus: true,
multiSelect: true,
afterSelectionChange: function() {
$scope.selectedIDs = [];
angular.forEach($scope.mySelections, function(item) {
$scope.selectedIDs.push(item.name)
});
},
columnDefs: [
{field:'id', displayName:'Id', cellClass: 'red'},
{field:'name', displayName:'Name', cellClass: 'blue'}
]
};
If you want you can also put a template:
$scope.gridOptions = {
data: 'myData',
selectedItems: $scope.mySelections,
enableCellEditOnFocus: true,
multiSelect: true,
afterSelectionChange: function() {
$scope.selectedIDs = [];
angular.forEach($scope.mySelections, function(item) {
$scope.selectedIDs.push(item.name)
});
},
columnDefs: [
{field:'id', displayName:'Id', cellTemplate: '<div class="ui-grid-cell-contents v-align-middle padding-20"><p>{{COL_FIELD}}</p></div>'},
{field:'name', displayName:'Name', cellTemplate: '<div class="ui-grid-cell-contents v-align-middle padding-20"><p>{{COL_FIELD}}</p></div>'}
]
};

Resources