ng-grid - editing then updating remote. How? - angularjs

I am using ng-grid and I am trying to automatically update a single model when data in the grid-table changes. For that I am using the edit module of ng-grid.
This is the code of my controller:
# Define app
app = angular.module("app", ["ng-rails-csrf", "rails", 'ngTagsInput', 'ui.bootstrap', 'ui.grid', 'ui.grid.edit'])
app = angular.module("app")
app.controller "UsersController", ["$scope", "User", ($scope, User)->
$scope.init = ->
User.query({}).then((results)->
console.log results
$scope.gridOptions.data = results
)
$scope.gridOptions =
enableFiltering: true
columnDefs: [
{field: 'id'}
{field: 'email', enableCellEdit: true, type: 'string'}
{field: 'firstName', enableCellEdit: true, type: 'string'}
{field: 'lastName', enableCellEdit: true, type: 'string'}
{field: 'lastName', enableCellEdit: true, type: 'string'}
]
$scope.init()
$scope.$watch 'gridOptions.data', (newValue, oldValue)->
console.log "new value"
console.log newValue
console.log "old value"
console.log oldValue
# FIXME this causes a lot of updates to the remote
, true
]
html:
.users-list
.my-grid ui-grid="gridOptions" ui-grid-edit="ui-grid-edit"
Is there a better way to do it? Like calling an update function whenever one of the model changes?

I'm not sure where you can use angular debounce or how it applies to the model on your grid.
Maybe something like?
<input ng-model="gridOptions.data" ng-model-options="{debounce: {'default': 700} }"/>
With lodash / underscore it looks like.
$scope.$watch('gridOptions.data', _.debounce(function (value) {
$scope.$apply(function(){
...
})
}, 700));

Related

ui-grid textarea doesn't close after edit

When I'm finished editing a textarea in the grid the edit mode is not closing.
In this Plunker, if you double-click one of the cells under Title, the textarea opens correctly, but does not close after I click out of the cell.
http://plnkr.co/edit/9jrzziWOP6hWWQ1Gab39?p=preview
<div ui-grid="{ data: data, columnDefs: columnDefs }" ui-grid-edit></div>
var app = angular.module('app', ['ui.grid', 'ui.grid.edit']);
app.controller('MainCtrl', ['$scope', function ($scope) {
$scope.data = [
{ name: 'John', title: 'CEO' },
{ name: 'Jane', title: 'Developer' }
];
$scope.columnDefs = [
{name: 'name', enableCellEdit: false},
{ name: 'title',
cellEditableCondition: true,
enableCellEdit: true,
editType: 'textarea',
editableCellTemplate: '<textarea rows="4" cols="140" ng-model="MODEL_COL_FIELD"></textarea>'
}
];
}]);
Your problem has to do with the line:
editableCellTemplate: '<textarea rows="4" cols="140" ng-model="MODEL_COL_FIELD"></textarea>'
In the API for uiGrid-edit, it states that you must have valid html, templateCache Id, or url that returns html content to be compiled when edit mode is invoked. In your case, you haven't included a means for the textarea to exit edit mode. To remedy this, include ui-grid-editor in the tag as follows.
editableCellTemplate: '<textarea ui-grid-editor rows="4" cols="140" ng-model="MODEL_COL_FIELD"></textarea>'

AngularJS : angular-ui-grid showing rows based on the condition

I would like my grid to show rows that only matches a creteria. For example i want my grid to show only the rows where name is Brian.
var app = angular.module('app', ['ngAnimate', 'ui.grid']);
app.controller('MainCtrl', ['$scope', '$http', function ($scope, $http) {
$scope.myData = [{name: "Brian", code: 50,count:20},
{name: "Jason", code: 43,userid:1},
{name: "Brian", code: 27,userid:10},
{name: "Devon", code: 29,userid:7},
{name: "Kale", code: 34,userid:2}];
$scope.gridOptions = {
enableSorting: true,
data:'myData',
columnDefs: [
{ field: 'name', displayName: 'Name'},
{field: 'code', displayName: 'Code'},
{ field: 'userid', displayName: 'UserId'
}
}
]
};
}]);
how can i accomplish that? Thank you in advance...
One of the work around would be, Instead of passing myData do filter you criteria and then assign that filtered data to new scope variable and assign that inside your gridOptions data field
Code
$scope.filteredData = myData; //make filter your data manually here
$scope.gridOptions = {
enableSorting: true,
data:'filteredData', //passed filtered data here.
columnDefs: [
{ field: 'name', displayName: 'Name'},
{field: 'code', displayName: 'Code'},
{ field: 'userid', displayName: 'UserId'
}
}
]
};
You need to use a filter. You can read about the standard angular filter here. You can read about how to make custom filters here.

Invoking a function inside controller from ng-click in ng-grid is not working

I got stuck while calling a function inside controller(listCtrl) from ng-click inside ng-grid
asset.js.coffee
app = angular.module('asset',[]).namespace(true)
app.controller 'linkCtrl', ['$scope','Restangular','asset.ProcServiceTools',($scope,Restangular,ProcServiceTools) ->
Restangular.all('asset').customGETLIST("linktogroup",{}).then (data) ->
$scope.myData = data
$scope.dynamicColumnDefs =ProcServiceTools.getIndexDynamicColumnDefs(data)
$scope.customoptions = ProcServiceTools.getCustomOptions()
$scope.linking = () ->
console.log("checkout")
]
app.service 'ProcServiceTools', () ->
class Tools
getIndexDynamicColumnDefs: (data) ->
return [
{field: 'id', displayName: 'ID' },
{field: 'asset_group_id', displayName: 'Group_ID' },
{field:'asset_catalog_id', displayName:'Catalog_id' },
{field: 'condition', displayName: 'Condition' },
{field: 'notes', displayName: 'Notes' },
{field: 'status', displayName: 'Status' },
{field: 'current_user_id', displayName: 'Current_user' },
{field: '' , cellTemplate: "<button ng-click='linking()'>Link-to </button>"}]
getCustomOptions: () ->
return {
showGroupPanel: true
jqueryUIDraggable: true
showSelectionCheckbox: true
showFooter: true
selectWithCheckboxOnly: true
enableColumnResize: true
}
new Tools
This is my asset.js file...I have injected the ProcServiceTools inside my linkCtrl and called the getIndexDynamicColumnDefs function inside this service with data as paramter and it get assigned to $scope.myData and i have also added linking() to the $scope
And this is my
index.html.erb
<div ng-controller='asset.linkCtrl'>
<customgrid columndefs='dynamicColumnDefs' data='myData' dirclass='gridStyleTable' include-search='true' options='customoptions'></customgrid>
In this file i set the data to be myData.I am getting all the datas displayed and button is also getting displayed..My problem is ng-click in the getIndexDynamicCoulmnDefs is not getting triggered when i click the link-to button...
But when i insert a button inside index.html.erb it is triggering the function.See the below code.The inserted button link is triggering the linking().
<div ng-controller='asset.linkCtrl'>
<customgrid columndefs='dynamicColumnDefs' data='myData' dirclass='gridStyleTable' include-search='true' options='customoptions'></customgrid>
<button ng-click="linking()">link</button>
</div>
I dont know the reason why i cant call the function inside the controller from ng-clik in ng-grid.
The problem is because of my user defined customgrid directive which i have included in index.html.erb that has its own private scope..So i used ng-grid itself and got that function triggered...
app = angular.module('asset', []).namespace(true);
app.controller('linkCtrl', ['$scope', 'Restangular', function($scope, Restangular) {
$scope.linking = function() {
console.log("check me out");
};
Restangular.all('asset').customGETLIST("linktogroup", {}).then(function(data) {
$scope.myData = data;
});
$scope.gridOptions = {
data: 'myData',
columnDefs: [{field: 'id', displayName: 'ID' },
{field: 'asset_group_id', displayName: 'Group_ID' },
{field:'asset_catalog_id', displayName:'Catalog_id' },
{field: 'condition', displayName: 'Condition' },
{field: 'notes', displayName: 'Notes' },
{field: 'status', displayName: 'Status' },
{field: 'current_user_id', displayName: 'Current_user' },
{cellTemplate:'<button id="editBtn" type="button" class="btn btn-primary" ng-click="linking()" >Edit</button> '}]};
}
]);
And my index.html.erb
<div ng-controller='asset.linkCtrl'>
<div class="gridStyle" ng-grid="gridOptions"></div>
</div>
And got my thing working :)

how to use ng-grid to update cells with $http?

I am new to angular and am trying to make a CRUD app with the ng-grid plugin. I found an example from the web that gets me the info I need, but not a good explanation on how to update the info using a REST route. Here is the code:
var cellEditableTemplate = "";
// Configure ng-grid
$scope.gridOptions = {
data: 'myData',
enableCellEdit: true,
multiSelect: false,
columnDefs: [
{ field: 'Id', displayName: 'Id' },
{ field: 'Name', displayName: 'Name', enableCellEdit: true, editableCellTemplate: cellEditableTemplate },
{ field: 'Description', displayName: 'Description', enableCellEdit: true, editableCellTemplate: cellEditableTemplate }
]
};
// Update Entity on the server side
$scope.updateEntity = function (column, row) {
console.log(row.entity);
console.log(column.field);
// code for saving data to the server...
// row.entity.$update() ... <- the simple case
}
How do I use the following from the example to update my model?
row.entity.$update()
Inject $http into your controller.
Then in your $scope.updateEntity:
$scope.updateEntity = function() {
$http.get('getDataFromServerUrl').success(function(data) {
//update data
$scope.gridOptions.data = data;
}).error(function(err) {
console.log('Error getting data', err);
});
}
and then your data will be updated, as $http activate the $digest cycle.

How can I update my server with changed data from ng-grid?

I have coded the following which I copied from an example:
var cellEditableTemplate = "<input style=\"width: 90%\" step=\"any\" type=\"text\" data-ng-class=\"'colt' + col.index\" data-ng-input=\"COL_FIELD\" data-ng-blur=\"updateEntity(col, row)\"/>";
// Configure ng-grid
$scope.gridOptions = {
data: 'myData',
enableCellEdit: true,
multiSelect: false,
columnDefs: [
{ field: 'Id', displayName: 'Id' },
{ field: 'Name', displayName: 'Name', enableCellEdit: true, editableCellTemplate: cellEditableTemplate },
{ field: 'Description', displayName: 'Description', enableCellEdit: true, editableCellTemplate: cellEditableTemplate }
]
};
// Update Entity on the server side
$scope.updateEntity = function (column, row) {
console.log(row.entity);
console.log(column.field);
// code for saving data to the server...
// row.entity.$update() ... <- the simple case
}
Everything works and the correct data is logged to the console.
Now I need to understand how I can update data on my server. The creator of the example is suggesting
row.entity.$update()
Can someone help me out. Is this a function that's part of the ng-grid and if not then how could I implement the $update to change data on my server through http?
Now that you know the column, the row, and the new value, you can use $http or $resource or even jQuery.ajax to update your model.

Resources