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;
Related
ui-grid in my demo .here is the api document
http://ui-grid.info/docs/#/tutorial/101_intro
I want to remove hide option from the columns .In each columns there is "V" image and on click of that image it show hide column option.I want to remove that option .how it is possible
here is my code
self.gridOptions = {
enableFiltering: true,
enableRowSelection: true,
multiSelect: false,
enableRowHeaderSelection: false,
enableSorting: false
};
http://plnkr.co/edit/ye0HXTbKv6dDYTQ0DMyX?p=preview
The page you're after is here: http://ui-grid.info/docs/#/tutorial/303_customizing_column_menu
You can remove the menu on each column header completely using:
enableColumnMenus: false
In your grid options as so:
self.gridOptions = {
enableFiltering: true,
enableRowSelection: true,
multiSelect: false,
enableRowHeaderSelection: false,
enableSorting: false,
enableColumnMenus: false
};
You can hide column menu by using colDef."enableColumnMenu":false
self.gridOptions.columnDefs=[ { field: 'name' },
{ field: 'gender' },
{ field: 'company',
enableSorting: true,
enableColumnMenu :false }]
This is my grid
$scope.selectedUsers = {
enableRowSelection: true,
enableSelectAll: true,
enableHorizontalScrollbar: 0,
enableVerticalScrollbar: 0,
columnDefs: [
{ name:'userName', enableColumnMenu: false, sort: { direction: uiGridConstants.ASC, priority: 0 } },
{ name: 'mandatory',cellTemplate: '<input type="checkbox">', enableColumnMenu: false, enableSorting: false },
{ name: 'alternative user', cellTemplate: '<div class="ui-grid-cell-contents" ng-if="!$eval(row.entity.alternativeuserexists)" ><a ng-click="grid.appScope.showAlternativeUsers(row)">Alternative Users</a></div>', enableColumnMenu: false, enableSorting: false }]
};
In the above grid contains 3 columns, 3rd column contains hyperlink , when i click link i want to update new value in 3rd column for particular row link value only not entire row data by using angularjs ui-grid
Your question is quite hard to understand, so if my answer doesn't fit your needs, try to provide more details or a Plunkr please.
You required a refresh of row-data on button click? Then read on.
You can modify your data-set dynamically and it will autoupdate your grid without reloading the whole thing.
I made a Plunkr showcasing a possible setup.
As you already did, one needs to provide a cellTemplate with reference to appScopeProvider. There you can create a function that modifies your present grid.data.
appScopeProvider: {
showAlternativeUsers : function(row) {
myData[myData.indexOf(row.entity)] = altUsers[myData.indexOf(row.entity)];
},
}
Depending on how and where your new data arrives, you can adjust the update logic.
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;
});
When I try to remove selected row in ng-grid the removed rows are still shown in UI.
My grid option is
$scope.gridOptions = {
data: 'myData',
multiSelect: true,
displaySelectionCheckbox: true,
selectedItems: $scope.mySelections,
showFooter: true,
showColumnMenu:false,
showFilter :false,
enableCellEdit: false,
enableCellSelection: false,
enableRowSelection: false,
showSelectionCheckbox: false,
beforeSelectionChange: function() {
return $scope.option.enableRowSelection;
},
}
And I remove or splice the data using
$scope.removeItem=function(){
angular.forEach($scope.mySelections, function(row, index){
angular.forEach($scope.myData, function(data, index){
if(data.indexno == row.indexno){
$scope.myData.splice(index,1);
$scope.gridOptions.data = 'myData';
console.log("after",$scope.myData);
console.log("after data",$scope.gridOptions.data);
}
});
});
$(".badge").html($scope.mySelections.length);
}
Any suggestions or solutions please
The way I have removed a row is by only allowing the user to select 1 row at a time but you could make it so that everything the user selects gets removed. this is the line of code that I used to actually do the removing
$scope.GridOptions.data.splice($scope.GridApi.grid.renderContainers.body.visibleRowCache.indexOf($scope.selectedRow), 1);
But here is how I did it. Here is my grid options at the top of my controller with a var that holds the selected row. you could make this an array of selected rows however!
$scope.selectedRow = null;
$scope.GridOptions = {
enableSorting: true,
enableRowSelection: true,
enableRowHeaderSelection: false,
enableColumnResizing: true,
columnDefs: [
{ name:'User Name', field: 'username', width: '15%', minWidth: 100},
{ name:'Dependency Key', field: 'id', width: '20%', minWidth: 100},
{ name:'Dependency File', field: 'file', width: '30%', minWidth: 100},
{ name:'Modified By', field: 'modifiedBy', width: '15%', minWidth: 100},
{ name:'Modified On', field: 'modifiedOn', width: '10%', minWidth: 100, cellTemplate:'<div class="ui-grid-cell-contents">{{grid.appScope.convertDate(row.entity.modifiedOn)}}</div>'},
{ name:'Dep. File Checksum', field: 'checksumAmericas', width: '10%', minWidth: 100}
],
onRegisterApi : function(gridApi) {
$scope.GridApi = gridApi;
}
};
$scope.GridOptions.multiSelect = false;
then this method gets called each time the user clicks on a row. I get which row the user has selected and assigned it to $scope.selectedRow
$scope.GridOptions.onRegisterApi = function(gridApi){
//set gridApi on scope
$scope.GridApi = gridApi;
$scope.GridApi.selection.on.rowSelectionChanged($scope,function(row){
if($scope.selectedRow == null)//user has not selected a row
$scope.selectedRow = row;
else if(row.entity.username == $scope.selectedRow.entity.username && row.entity.id == $scope.selectedRow.entity.id)//user clicked the same row that was selected and now has unselected
$scope.selectedRow = null;
else //user selected new row
$scope.selectedRow = row;
});
};
Then to when the user clicks remove button it calls a method and inside that method i check to see if $scope.selectedRow is not null and then to remove from the table is just 1 line
if($scope.selectedRow != null)
$scope.GridOptions.data.splice($scope.GridApi.grid.renderContainers.body.visibleRowCache.indexOf($scope.selectedRow), 1);
This website really helps with ui-grid functionality:
http://ui-grid.info/
http://ui-grid.info/docs/#/tutorial/210_selection
Hope this helps
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>'}
]
};