原文:202 Cell Navigation
此示例使用 ui-grid-cellNav指令添加单元格导航。要启用, 必须包括 “ui.grid.cellNav” 模块, 并且必须在表格元素上包括ui-grid-cellNav指令。
CellNav 功能的文档在 api 文档中提供, 特别是:
columnDefgridOptionspublicApiCellNav 允许您使用箭头键、pg-down 和 pg-up 在网格周围导航、输入 (下移)、shift 输入 (向上移动)、tab (向右移动) 和 shift 键 (向左移动)。当与可编辑功能组合在一起时, 左向右箭头键将被并入 “深层编辑” 模式, 允许您在编辑的文本中移动。tab 键和 shift 键选项卡按键仍起作用。
使用 gridOptions. onRegisterApi 回调以在导航单元格时注册 on_cellNav 事件和日志。
提供一个示例, 请求滚动到特定的行或列以编程方式-对于记住页的状态并在用户返回时滚动回该位置非常有用。
提供一个滚动到特定单元格并使用 scrollToFocus api 设置焦点的示例。
提供了一个利用 modifierKeysToMultiSelectCells 选项和 getCurrentSelection api 函数来提取选定单元格的值的示例。
提供了一个示例, 说明使用 keyDownOverrides 添加自定义的 ctrl + Right 焦点到一行的最后一列, 同时仍然允许由表格处理右箭头。
代码: index.html
<!doctype html> <html ng-app="app"> <head> <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.5.0/angular.js"></script> <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.5.0/angular-touch.js"></script> <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.5.0/angular-animate.js"></script> <script src="http://ui-grid.info/docs/grunt-scripts/csv.js"></script> <script src="http://ui-grid.info/docs/grunt-scripts/pdfmake.js"></script> <script src="http://ui-grid.info/docs/grunt-scripts/vfs_fonts.js"></script> <script src="/release/ui-grid.js"></script> <script src="/release/ui-grid.css"></script> <script src="app.js"></script> </head> <body> <div ng-controller="MainCtrl"> <button type="button" class="btn btn-success" ng-click="scrollTo(20,0)">Scroll To Row 20</button> <button type="button" class="btn btn-success" ng-click="scrollTo(0,7)">Scroll To Balance</button> <button type="button" class="btn btn-success" ng-click="scrollTo(50,7)">Scroll To Row 50, Balance</button> <button type="button" class="btn btn-success" ng-click="scrollToFocus(50,7)">Focus Row 50, Balance</button> <button type="button" class="btn btn-success" ng-click="getCurrentFocus()">Get Current focused cell</button> <span ng-bind="currentFocused"></span> <button type="button" class="btn btn-success" ng-click="getCurrentSelection()">Get Current focused cell values</button> <input type="text" ng-model="printSelection" placeholder="Click the 'Get current focused cell values' button to print cell values of selection here" class="printSelection"> <br> <br> <div ui-grid="gridOptions" ui-grid-cellNav ui-grid-pinning class="grid"></div> </div> </body> </html>main.css
.grid { width: 500px; height: 400px; } .printSelection { width: 600px; margin-top: 10px; }app.js
var app = angular.module('app', ['ngTouch', 'ui.grid', 'ui.grid.cellNav', 'ui.grid.pinning']); app.controller('MainCtrl', ['$scope', '$http', '$log', function ($scope, $http, $log) { $scope.gridOptions = { modifierKeysToMultiSelectCells: true, keyDownOverrides: [ { keyCode: 39, ctrlKey: true }], showGridFooter: true }; $scope.gridOptions.columnDefs = [ { name: 'id', width:'150' }, { name: 'name', width:'200' }, { name: 'age', displayName: 'Age (not focusable)', allowCellFocus : false, width:'100' }, { name: 'address.city', width:'200' }, { name: 'phone', width:'150' }, { name: 'company', width:'150' }, { name: 'email', width:'150' }, { name: 'balance', width:'100' }, { name: 'guid', width:'100' } ]; $http.get('/data/500_complex.json') .success(function(data) { $scope.gridOptions.data = data; }); $scope.info = {}; $scope.currentFocused = ""; $scope.getCurrentFocus = function(){ var rowCol = $scope.gridApi.cellNav.getFocusedCell(); if(rowCol !== null) { $scope.currentFocused = 'Row Id:' + rowCol.row.entity.id + ' col:' + rowCol.col.colDef.name; } }; $scope.getCurrentSelection = function() { var values = []; var currentSelection = $scope.gridApi.cellNav.getCurrentSelection(); for (var i = 0; i < currentSelection.length; i++) { values.push(currentSelection[i].row.entity[currentSelection[i].col.name]) } $scope.printSelection = values.toString(); }; $scope.scrollTo = function( rowIndex, colIndex ) { $scope.gridApi.core.scrollTo( $scope.gridOptions.data[rowIndex], $scope.gridOptions.columnDefs[colIndex]); }; $scope.scrollToFocus = function( rowIndex, colIndex ) { $scope.gridApi.cellNav.scrollToFocus( $scope.gridOptions.data[rowIndex], $scope.gridOptions.columnDefs[colIndex]); }; $scope.gridOptions.onRegisterApi = function(gridApi){ $scope.gridApi = gridApi; gridApi.cellNav.on.navigate($scope,function(newRowCol, oldRowCol){ // var rowCol = {row: newRowCol.row.index, col:newRowCol.col.colDef.name}; // var msg = 'New RowCol is ' + angular.toJson(rowCol); // if(oldRowCol){ // rowCol = {row: oldRowCol.row.index, col:oldRowCol.col.colDef.name}; // msg += ' Old RowCol is ' + angular.toJson(rowCol); // } $log.log('navigation event'); }); gridApi.cellNav.on.viewPortKeyDown($scope,function(event, newRowCol){ var row = newRowCol.row; var col = newRowCol.col if (event.keyCode === 39) { $scope.gridApi.cellNav.scrollToFocus(row.entity, $scope.gridApi.grid.columns[$scope.gridApi.grid.columns.length - 1]); } }); }; }]);Demo
作者水平有限,不当之处敬请指正。
感谢您的阅读,如果觉得文章对您有帮助,请支持一下。