Skip to content

Fix(importer): changes to handling of data watch #1826

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 15, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion misc/tutorial/207_importing_data.ngdoc
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ csv needing to match either the column.name or column.displayName. Optionally y
provide a custom function that maps headings to column.name, and this will be used instead.

To use the importer you need to include the ui-grid-importer directive on
your grid.
your grid, and you must provide a `gridOptions.importerDataAddCallback` function that adds
the created objects into your data array.

The options and API for importer can be found at {@link api/ui.grid.importer ui.grid.importer}.

Expand All @@ -38,8 +39,13 @@ illustrates that doing so is not mandatory).
var app = angular.module('app', ['ngAnimate', 'ui.grid', 'ui.grid.importer']);

app.controller('MainCtrl', ['$scope', '$http', '$interval', function ($scope, $http, $interval) {
$scope.data = [];
$scope.gridOptions = {
enableGridMenu: true,
data: 'data',
importerDataAddCallback: function ( grid, newObjects ) {
$scope.data = $scope.data.concat( newObjects );
},
onRegisterApi: function(gridApi){
$scope.gridApi = gridApi;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
@ngdoc overview
@name Tutorial: 310 Importing Data With Row Edit
@name Tutorial: 311 Importing Data With Row Edit
@description The importer feature allows data to be imported into the grid in
csv or json format. The importer can use the native grid menu, or can accept a
file from a custom file picker implemented by the user.
Expand All @@ -23,12 +23,17 @@ and can be edited to save without error.
var app = angular.module('app', ['ngAnimate', 'ui.grid', 'ui.grid.importer', 'ui.grid.rowEdit', 'ui.grid.edit']);

app.controller('MainCtrl', ['$scope', '$http', '$interval', '$q', function ($scope, $http, $interval, $q) {
$scope.data = [];
$scope.gridOptions = {
enableGridMenu: true,
importerDataAddCallback: function( grid, newObjects ) {
$scope.data = $scope.data.concat( newObjects );
},
onRegisterApi: function(gridApi){
$scope.gridApi = gridApi;
gridApi.rowEdit.on.saveRow($scope, $scope.saveRow);
}
},
data: 'data'
};

$scope.saveRow = function( rowEntity ) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
@ngdoc overview
@name Tutorial: 306 Exporting Data With Custom UI
@name Tutorial: 312 Exporting Data With Custom UI
@description The exporter feature allows data to be exported from the grid in
csv or pdf format. The exporter can export all data, visible data or selected data.

Expand Down
83 changes: 42 additions & 41 deletions src/features/importer/js/importer.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,15 +98,17 @@

grid.api.registerMethodsFromObject(publicApi.methods);

if (grid.api.core.addToGridMenu){
service.addToMenu( grid );
} else {
// order of registration is not guaranteed, register in a little while
$interval( function() {
if (grid.api.core.addToGridMenu){
service.addToMenu( grid );
}
}, 100, 1);
if ( grid.options.enableImporter && grid.options.importerShowMenu ){
if ( grid.api.core.addToGridMenu ){
service.addToMenu( grid );
} else {
// order of registration is not guaranteed, register in a little while
$interval( function() {
if (grid.api.core.addToGridMenu){
service.addToMenu( grid );
}
}, 100, 1);
}
}
},

Expand Down Expand Up @@ -205,7 +207,29 @@
if ( !gridOptions.importerErrorCallback || typeof(gridOptions.importerErrorCallback) !== 'function' ){
delete gridOptions.importerErrorCallback;
}


/**
* @ngdoc method
* @name importerDataAddCallback
* @methodOf ui.grid.importer.api:GridOptions
* @description A mandatory callback function that adds data to the source data array. The grid
* generally doesn't add rows to the source data array, it is tidier to handle this through a user
* callback.
*
* <pre>
* gridOptions.importerDataAddCallback: function( grid, newObjects ) {
* $scope.myData = $scope.myData.concat( newObjects );
* })
* </pre>
* @param {Grid} grid the grid we're importing into, may be useful in some way
* @param {array} newObjects an array of new objects that you should add to your data
*
*/
if ( gridOptions.enableImporter === true && !gridOptions.importerDataAddCallback ) {
$log.error("You have not set an importerDataAddCallback, importer is disabled");
gridOptions.enableImporter = false;
}

/**
* @ngdoc object
* @name importerNewObject
Expand All @@ -227,17 +251,10 @@
* @ngdoc property
* @propertyOf ui.grid.importer.api:GridOptions
* @name importerShowMenu
* @description Whether or not to show an item in the grid menu. If an item
* is to be shown in the grid menu then an `importerInputElement` must be
* provided as well. If there is no `importerInputElement` then the menu
* will be automatically suppressed, otherwise defaults to true.
* @description Whether or not to show an item in the grid menu. Defaults to true.
*
*/
if ( gridOptions.enableImporter && ( !gridOptions.importerInputElement || !gridOptions.importerInputElement.append ) ) {
gridOptions.importerShowMenu = false;
} else {
gridOptions.importerShowMenu = !!gridOptions.importerShowMenu;
}
gridOptions.importerShowMenu = gridOptions.importerShowMenu !== false;
},


Expand Down Expand Up @@ -545,30 +562,14 @@
* @returns {object} the new object
*/
addObjects: function( grid, newObjects ){
grid.options.data = grid.options.data.concat( newObjects );

// This block replicates the data watch, but shouldn't be necessary in an ideal world
var promises = [];
if (grid.columns.length === ( grid.rowHeaderColumns ? grid.rowHeaderColumns.length : 0 ) ) {
if (grid.options.columnDefs.length === 0) {
grid.buildColumnDefsFromData(grid.options.data);
}
promises.push(grid.buildColumns()
.then(function() {
grid.preCompileCellTemplates();}
));
if ( grid.api.rowEdit ){
var callbackId = grid.registerDataChangeCallback( function() {
grid.api.rowEdit.setRowsDirty( grid, newObjects );
grid.deregisterDataChangeCallback( callbackId );
});
}

$q.all(promises).then(function() {
grid.modifyRows(grid.options.data)
.then(function () {
grid.redrawInPlace();
grid.refreshCanvas(true);
if ( grid.api.rowEdit ){
grid.api.rowEdit.setRowsDirty( grid, newObjects );
}
});
});
grid.options.importerDataAddCallback( grid, newObjects );
},


Expand Down
Loading