Jas*_*ett 15 angularjs angular-ui-grid
我有一个Angular UI Grid中的项目列表.当我点击一行时,我想要被带到另一页.(换句话说,网格中的每一行都是一个链接.)
我想这一定是一个非常普遍的愿望,虽然我还没有看到任何关于如何做的文档.有什么好办法来实现这个目标?
Jas*_*ett 20
我自己想出了答案.这是我的控制器(ES6):
'use strict';
class TrackingRecordsCtrl {
constructor($scope) {
// The content of this template is included
// separately
$scope.gridOptions = {
rowTemplate: 'app/components/tracking-record/grid-row.html',
};
// This function is referenced from the row's template.
// I'm just console.logging the row but you can of
// course do anything you want with it.
$scope.gridRowClick = row => {
console.log(row);
// or maybe $location.path(row.url)?
};
$scope.gridOptions.data = {
// This of course can't just be an empty object.
// Chances are you already have something defined
// for gridOptions.data.
};
}
}
TrackingRecordsCtrl.$inject = ['$scope'];
export default TrackingRecordsCtrl;
Run Code Online (Sandbox Code Playgroud)
这是我的行模板(Jade):
.ui-grid-cell(
ng-repeat='(colRenderIndex, col) in colContainer.renderedColumns track by col.colDef.name'
ng-class="{ 'ui-grid-row-header-cell': col.isRowHeader }"
ui-grid-cell=''
ng-click='grid.appScope.gridRowClick(row)'
)
Run Code Online (Sandbox Code Playgroud)
作为奖励,这是我的样式表(SCSS).我认为突出光标下的行并使用pointer光标更清楚地表明行是可点击的是有意义的.
.ui-grid-row {
cursor: pointer;
&:hover .ui-grid-cell {
background-color: #CCC;
}
}
Run Code Online (Sandbox Code Playgroud)
KuN*_*KuN 15
这是我使用的解决方案. /sf/answers/2297082091/
yourCtrl.gridOptions = {
enableFiltering: true,
enableHiding : false,
enableSorting: true,
appScopeProvider : yourCtrl,
rowTemplate: '<div ng-click="grid.appScope.doSomething(row)" ng-repeat="(colRenderIndex, col) in colContainer.renderedColumns track by col.uid" class="ui-grid-cell" ng-class="col.colIndex()" ui-grid-cell></div>',
};
yourCtrl.doSomething = function(row) {
alert("lala");
}
Run Code Online (Sandbox Code Playgroud)
Bla*_*ord 12
我理解的问题是:单击Angular UI Grid中的一行应导致导航到相关页面(即该行应该像链接一样).例如,联系人列表显示在网格中,单击一行可以转到联系人详细信息页面.
感谢OP以创新的方式回答他自己的问题.但是,我更喜欢这个答案,因为它不需要创建自定义行模板.我已经充实了它,因为OP似乎对Kathir的例子不满意.
首先,要了解以下代码在row.isSelected属性更改时设置侦听器函数:
gridApi.selection.on.rowSelectionChanged($scope,function(row){
//Do something when a row is selected
});
Run Code Online (Sandbox Code Playgroud)
即,只要单击一行,就会调用此函数.请注意row传递给函数的参数,该参数可用于访问行所代表的实体.例如,如果行表示联系人实体,则可以访问contactId所选行/实体的属性.以下示例使用UI路由器的$state服务导航到传递contactId从row.entity属性获取的联系人详细信息页面:
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});
});
}
Run Code Online (Sandbox Code Playgroud)
请注意$scope,即使您使用Controller as语法,也需要传入该对象.请参阅有关语法的本文Controller as.
有关使用Typescript的完整示例(为简洁起见省略了文件引用):
"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);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
34707 次 |
| 最近记录: |