I am trying to learn Ui-grid from this link
http://ui-grid.info/docs/#/tutorial/101_intro.
I make a simple example of ui-grid in plunker..Actually the table header takes first object property name .I need to give other name instead of property name.Example I need my first columns name should "First" and "second" without changing the array of objects here is my code
https://plnkr.co/edit/s0NUaL15W4Q95WXGeQK5?p=preview
angular.module('app',['ngTouch', 'ui.grid']).controller('MainCtrl',function($scope){
$scope.data=[{
name:'abc',
lastname:'hrt'
},{
name:'pqr',
lastname:'oiu'
},{
name:'lqm',
lastname:'ytu'
}]
})
see your solution here
https://plnkr.co/edit/cDaeiNmWIvQ0NLoWxYKX?p=preview
you need to set Options to ui-grid, so you can set displayName in columnDefs
$scope.gridOptions = {
columnDefs: [
{field: 'name', displayName:'First'},
{field: 'lastname', displayName:'Second'}
]
};
use grid option 'columnDefs'
angular.module('app',['ngTouch', 'ui.grid']).controller('MainCtrl',function($scope){
$scope.datas=[{
name:'abc',
lastname:'hrt'
},{
name:'pqr',
lastname:'oiu'
},{
name:'lqm',
lastname:'ytu'
}];
$scope.gridOptions = {
columnDefs: [
{ name:'First', field: 'name' },
{ name:'Second', field: 'lastname' }],
data : $scope.datas
};
})
html
<div ng-controller="MainCtrl">
{{3+5}}
<div id="grid1" ui-grid="gridOptions" class="grid" ></div>
</div>
Related
I am trying to get dropdown values in header for one of the field in ui-grid. For the same field I have the dropdown for every row working fine. Based on the header dropdown selection every dropdown values in rows should be selected. Here is my plunker link. Can someone help please?
Here's a link!
var jsonDef = { name: 'Status', field: 'Status', width: 150,
editType: 'dropdown',
editableCellTemplate: 'ui-grid/dropdownEditor',
headerCellTemplate: '<div class="ui-grid-cell-contents header-tcsi"><select
ng-required = "true" ng-options="options.id as options.type for tcsiOption
in grid.appScope.MainCtrl" ng-model="grid.appScope.selectedTCSI"></select>
</div>',
editDropdownIdLabel: 'id',
editDropdownValueLabel: 'type',
filter: {
type: uiGridConstants.filter.SELECT,
condition: uiGridConstants.filter.EXACT }
};
var options = [{
id: 1,
type: 'Closed'
}, {
id: 2,
type: 'Pending'
}, {
id: 3,
type: 'Open'
}];
You were using the ng-options incorrectly. If you want to access something from the appScope you have to the attach it to the controller $scope. Then you can change your template where you use the options like so:
headerCellTemplate: '<div class="ui-grid-cell-contents header-Status"><select ng-required = "true" ng-options="options.id as options.type for options in grid.appScope.dropdownoptions" ng-change= "grid.appScope.selectionChanged()"ng-model="grid.appScope.selectedStatus"></select> </div>'
Pay attention to ng-options:
ng-options="options.id as options.type for options in grid.appScope.dropdownoptions"
The way you've written your code, you need to listen to when the dropdown changes and then update the values in the grid. Check the
Updated Plnkr for the working example.
The ui-grid documentation tells us to define columns in the controller, like this:
$scope.gridOptions = {
columnDefs: [
{ field: 'id', displayName: 'Ref', width: "60" },
{ field: 'title', displayName: 'Title', width: "200" }
]
}
While this works fine, I feel that this really belongs in my template. Is there some way to setup the columns like this:
<div ui-grid="gridOptions" class="myGrid" style="width:800px; height: 600px">
<ui-column field="id" width="60">Ref</ui-column>
<ui-column field="title" width="200">Title</ui-column>
</div>
apologies if this is really simple but....
I want to display a grid of data but need a look up for some columns. This will become an editable grid but I am suffering with the basics so god help me.
I have two sets of data:
$gridOptions1.data = [{"group_id":"1","location_id":"-1","group_name":"Cars","active":"1"},{"group_id":"2","location_id":"1","group_name":"Trains","active":"1"},{"group_id":"3","location_id":"2","group_name":"Buses","active":"0"}]
and
$scope.locations=[{value: "-1", text: 'All Locations'},
{value: "0", text: 'Location 1'},
{value: "1", text: 'Location 2'},
{value: "2", text: 'Location 3'},
{value: "3", text: 'Location 4'}];
And want to display in the grid.
$scope.gridOptions1 = {
enableSorting: true,
columnDefs: [
{ field: 'location_id' },
{ field: 'group_name' },
{ field: 'active' },
{ name: 'Location', field:'location_id', cellFilter: 'maplocation:this'}
],
onRegisterApi: function( gridApi ) {
$scope.grid1Api = gridApi;
}
};
What I need to do is map the location id
I thought I could use a filter but cannot seem to get access to the scope data.
If anyone could give me a simple example of how to do this I would be very grateful as I am struggling to find any examples of what I want to do.
From what I can see the 'this' parameter is a pointer to the record and not the scope in which the grid options is defined.
I don't want to define the data in the filter because it is coming from the database.
Hope that makes sense.
If you want to use part of the data that's in your application scope to transform the data displayed in UI grid it's best to go with a custom template and calling in the function from your template.
Something like this should work:
columnDefs: [
{ field: 'location_id' },
{ field: 'group_name' },
{ field: 'active' },
{ name: 'Location', field:'location_id', cellTemplate: 'location-template'}
],
And then HTML:
<script type="text/ng-template" id="location-template">
<div class="ui-grid-cell-contents" title="TOOLTIP">{{grid.appScope.formatLocation(row)}}</div>
</script>
Now all you have to do is to define function formatLocation in your controller's scope and do the magic there.
When calling functions from within the cell template, make sure you use grid.appScope to get access to your controller's scope, like in the example I've provided.
Thanks to #Ethnar, here is a workable solution that keeps the template in the source:
columnDefs: [ { field: 'location_id' },
{ field: 'group_name' },
{ field: active' },
{ name: 'Location', field:'location_id',
cellTemplate: '<div class="ui-grid-cell-contents" title="TOOLTIP">{{grid.appScope.formatLocation(row)}}</div>'
}],
Then all is needed is the formatLocation function:
$scope.formatLocation=function(row)
{
locationid=row.entity.location_id;
if(locationid && $scope.locations.length) {
var selected = $filter('filter')($scope.locations, {value: locationid});
return selected.length ? selected[0].text : 'Not set';
} else {
return 'Not set';
} };
I am considering using ng-grid for a new angular application. The grid that we are creating requires html markup in each individual cell.
A very simplified example is in this plunkr. The ui-grid widget has some cells that want to use the <strong> and <em> tags. However, the markup is displayed as a string, not as DOM.
So, this is what I have:
HTML:
<body ng-app="mygrid">
<div ng-controller="GridCtrl">
<div ui-grid="{ data: myData }"></div>
</div>
</body>
JS:
angular.module('mygrid', ['ui.grid'])
.controller('GridCtrl', function($scope) {
$scope.name = 'Andrew';
$scope.data = [{
first: '<em>Andrew</em>',
last: '<strong>Eisenberg</strong>'
}];
});
I would like this to be displayed as DOM, instead of text. How can I do this?
Assuming you have control over your data (i.e. you can change your data from:
$scope.data = [{
first: '<em>Andrew</em>',
last: '<strong>Eisenberg</strong>'
}];
to:
$scope.data = [{
first: 'Andrew',
last: 'Eisenberg'
}];
you can define columnDefs with cellTemplates (that contain the HTML template) as follows:
$scope.gridOptions = {
columnDefs: [
{
field: 'first',
cellTemplate: '<em>{{COL_FIELD}}</em>'
},
{
field: 'last',
cellTemplate: '<strong>{{COL_FIELD}}</strong>'
},
],
See here for an example when using ui-grid. If you happen to be using an ng-grid you can do something similar, but instead of COL_FIELD use row.getProperty(col.field). See here for an ng-grid example.
Edit: If you have no control over the data and it must contain HTML
If you haven't got control of your data (i.e. your data must contain the markup) e.g:
$scope.data = [{
first: '<em>Andrew</em>',
last: '<strong>Eisenberg</strong>'
}];
then you can do as follows. Create a cell template that can render HTML:
<script type="text/ng-template" id="emailTemplate">
<div class="ngCellText" ng-class="col.colIndex()"><span ng-bind-html="COL_FIELD"></span></div>
</script>
Define columnDefs that reference the template as follows:
$scope.gridOptions = {
columnDefs: [
{
field: 'first',
cellTemplate: 'htmlTemplate'
},
{
field: 'last',
cellTemplate: 'htmlTemplate'
},
],
Note: You'll need to include the ngSanitize library to render and sanitize the HTML:
var app = angular.module('app', ['ngSanitize', 'ui.grid']);
See here for a demo.
I'm trying to translate Ui-grid in angular but i can't . i just want to translate columnDefs .
here is my controller :
$scope.gridOptions = {
enableSorting: true,
columnDefs: [
{ name: 'نمایش', cellClass: "editCell", cellTemplate: '<i id="editBtn" tooltip-placement="left" tooltip="نمایش درخواست" class="fa fa-eye" ng-click="getExternalScopes().editUser(row.entity.RequestCode)" ></i>', headerClass: 'JobHeader' },
{
name: 'کد شهر', headerClass: 'cityHeader', field: 'CityCode', editableCellTemplate: self.editableCellTempate,
enableCellEdit: true
},
{ name: 'کد امور', field: 'RgnCode' },
{ name: 'شماره درخواست', field: 'RequestCode' },
],
};
i want to translate name in columnDefs
Any idea ?
Use
cellFilter:'translate' for cell,
headerCellFilter:'translate' for header
footerCellFilter: 'translate' for footer
in colummDefs
I used {field:'id', displayName:'ID_TRANSLATION_KEY', headerFilter:'translate'}. It works like usual template translation. Only problem with your solution you may be losing default sorting functionality offered by the component (have to create your own) when you use headerCellTemplate.
I think this may help someone.
If you want to manually translate it, use name as the fieldname in your data, and then set displayName to whatever you want.
If you want to do on-the-fly translation using angular-translate, then as #YOU said.
Use displayName and $translate.instant('...'):
{
name: 'CityCode',
displayName: $translate.instant('CityCode'),
headerClass: 'cityHeader',
editableCellTemplate: self.editableCellTempate,
enableCellEdit: true
}