The question as I understand it: clicking a row in an Angular UI Grid should result in navigation to a related page (i.e. the row should behave like a link). For example, a list of contacts are displayed in the grid, and clicking on a row takes you to a contact details page.
Kudos to the OP for answering his own question in an innovative way. However, I prefer this answer as it does not require the creation of a custom row template. I have fleshed it out a bit more, as the OP did not seem satisfied with Kathir's example.
Firstly, understand that the following code sets up a listener function for whenever the row.isSelected property changes:
gridApi.selection.on.rowSelectionChanged($scope,function(row){
//Do something when a row is selected
});
I.e. whenever a row is clicked, this function will be called. Note the row
parameter passed to the function which can be used to access the entity that the row represents. For example, if a row represented a contact entity, then you could access the contactId
property of the row/entity that was selected. The following example uses UI Router's $state
service to navigate to a contact details page passing the contactId
obtained from the row.entity
property:
this.gridOptions.onRegisterApi = function (gridApi) {
//set gridApi to controller property
this.gridApi = gridApi;
gridApi.selection.on.rowSelectionChanged($scope, function (row) {
$state.go("contact.details.view", {contactId: row.entity.contactId});
});
}
Note that the $scope
object is required to be passed in, even if you are using Controller as
syntax. Refer to this article about Controller as
syntax.
For a full example using Typescript (file references omitted for brevity):
"use strict"
export class ContactsCtrl {
title: string;
contacts: interfaces.IContact[] = [];
gridAPI: any;
gridOptions: any = {
enableFullRowSelection: true,
enableRowSelection: true,
multiSelect: false,
enableRowHeaderSelection: false,
data: "vm.contacts",
columnDefs: [
{ field: "contactId", displayName: "Contact ID", width: 100 },
{ field: "name", displayName: "Contact Name" } ]
}
static $inject = ["$scope", "$state"];
constructor(private $scope : ng.IScope, private $state : ng.ui.IStateService) {
this.gridOptions.onRegisterApi = function (gridApi) {
//set gridApi on scope
this.gridApi = gridApi;
gridApi.selection.on.rowSelectionChanged($scope, function (row) {
$state.go("contact.details.view", {contactId: row.entity.contactId});
});
}
}
}
angular.module("app.contacts").controller("ContactsCtrl", contacts.controllers.ContactsCtrl);
}