在彈出的模態框中使用表格。
在某些情況下,特別是 bootstrap modal,可能會出現表格渲染寬度過小或有時顯示不完全。會誤認為是由於 bootstrap modal 的動畫渲染導致表格渲染時的可用空間不如預期。可以通過調用handleWindowResize來糾正。動畫渲染的時間不好確定,所以一般推薦使用$interval,在模態框打開後的5秒內每隔500ms循環調用。
從某種意義上說,這類似於自動調整大小的功能,但它只在模態框開啟後的短時間內完成。
代碼:
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 id="showButton" class="btn btn-success" ng-click="showModal()">Show Modal</button> </div> </body> </html>
mian.css
.grid { width: 300px; height: 250px; }
app.js
var app = angular.module('app', ['ngTouch', 'ui.grid']); app.controller('MainCtrl', ['$rootScope', '$scope', '$http', 'modal', '$interval', function ($rootScope, $scope, $http, modal, $interval) { var myModal = new modal(); $scope.hideGrid = true; $rootScope.gridOptions = { onRegisterApi: function (gridApi) { $scope.gridApi = gridApi; // call resize every 500 ms for 5 s after modal finishes opening - usually only necessary on a bootstrap modal $interval( function() { $scope.gridApi.core.handleWindowResize(); }, 500, 10); } }; $http.get('/data/100.json') .success(function(data) { $rootScope.gridOptions.data = data; }); $scope.showModal = function() { myModal.open(); }; }]); app.factory('modal', ['$compile', '$rootScope', function ($compile, $rootScope) { return function() { var elm; var modal = { open: function() { var html = '<div class="modal" ng-style="modalStyle">{{modalStyle}}<div class="modal-dialog"><div class="modal-content"><div class="modal-header"></div><div class="modal-body"><div id="grid1" ui-grid="gridOptions" class="grid"></div></div><div class="modal-footer"><button id="buttonClose" class="btn btn-primary" ng-click="close()">Close</button></div></div></div></div>'; elm = angular.element(html); angular.element(document.body).prepend(elm); $rootScope.close = function() { modal.close(); }; $rootScope.modalStyle = {"display": "block"}; $compile(elm)($rootScope); }, close: function() { if (elm) { elm.remove(); } } }; return modal; }; }]);
Demo
以上所述是小編給大家介紹的jQuery UI Grid 模態框中的表格,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對網站的支持!