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';
} };
Related
Here's the screenshot of my ui-grid.
Here's the code that creates that filter.
{ field: 'channel_type', displayName: "Type", filter: {
type: uiGridConstants.filter.SELECT,
selectOptions: [
{ value: 'HD', label: 'HD' },
{ value: 'SD', label: 'SD' }
]
}},
{ field: 'price', displayName: "Price", enableFiltering: false, enableSorting: false},
My data is loaded in uigrid from rest api call.
I really can't figure it out from where is it adding that blank option. I haven't found any solution to this issue. Please help me in solving this problem.
Its a known issue, see ui-grid issues
The solution (workaround) is to add some custom style:
#grid1 div div select option:first-child[value=""] {
display: none;
}
Demo Plunker
You have to use filterHeaderTemplate and create your dropdown in it. Here is the example:
field: 'yourField',
filterHeaderTemplate: '<div class="ui-grid-filter-container" ng-repeat="colFilter in col.filters"><select class="grid-filter-control" ng-model="colFilter.term" ng-options="option.value as option.label for option in colFilter.selectOptions"></select></div>',
filter: {
disableCancelFilterButton: true,
term: '',
type: uiGridConstants.filter.SELECT,
selectOptions: [{ value: '', label: 'All' },
{ value: '1', label: 'Something' },
{ value: '2', label: 'Something Else' }]
}
Defining term will select your first option and the empty one is not going to show.
I have a grid populating records from view model (Ajax proxy data from Java restful web services). When I select a record in the grid form open and the fields are displayed. The records are editable using the form.
I have one tag field which is binded to a store and I should be able to populate the data associated to the record whenever the user selects a record.
"data" : [ {
"createdOn" : 1475678859000,
"updatedOn" : 1475679885000,
"updatedBy" : null,
"id" : 174,
"userName" : "ffff,
"firstName" : "gg",
"lastName" : "ggg",
"salesDepartment" : [ {
"id" : 3,
"code" : "FC",
"name" : "Fiat-Chrysler",
"departmentHead" : "xxx",
"applicationCode" : null} ],}
I need to bind the value of id from sales department object . how can I achieve this. Please help me.
{xtype: 'tagfield',
anchor: '100%',
reference: 'salesDept',
fieldLabel: 'Sales Dept\'s',
name: 'salesDepartment',
allowBlank: false,
displayField: 'name',
valueField: 'id',
bind: {
value: '{record.salesDepartmentIds}',
store: '{SalesDepartmentStore}'},
}
Tag fields are really only designed to work with arrays or comma separated lists;
It looks like you are trying to use with associations, which it would be nice if there was more support for this out of the box.
I've had a go, and in terms of displaying got it working however saving values will really depend on how you plan to sync values to the server, if you are going to use in built proxies etc. then this will present a whole different challenge. I would like a similar component for my own apps, I will think on this further and may even create a UX for this.
I think you have several issues here, one is getting your associations working properly, then you need to make your tagfield work as expected.
Here is the fiddle
And the key parts are:
An extended tagfield:
Ext.define('RelatedTagField', {
extend: 'Ext.form.field.Tag',
xtype: 'rtagfield',
config: {
relatedStore: null
},
setValue: function (val) {
this.setRelatedStore(val);
var value;
if (val && val.isStore) {
value = val.collect('id');
} else {
value = '';
}
this.callParent([value]);
},
getValue: function () {
return this.relatedStore;
},
onBindStore: function () {
this.callParent(arguments);
this.on('select', function (tagfield, selection) {
var selectedIds = [];
//add any new selections
Ext.each(selection, function (rec) {
var rrec = this.relatedStore.getById(rec.id);
if (!rrec) {
this.relatedStore.add(rec.data)
}
selectedIds.push(rec.id);
}, this);
//remove any not selected anymore
this.relatedStore.each(function (rrec) {
if (selectedIds.indexOf(rrec.id) == -1) {
this.relatedStore.remove(rrec);
}
}, this);
}, this);
},
})
Binding the value:
{
xtype: 'rtagfield',
fieldLabel: 'Sales Department',
name: 'id',
displayField: 'name',
valueField: 'id',
bind: {
store: '{salesDepartment}',
value: '{record.salesDepartment}'
}
},
And adding a definition for the association:
Ext.define('MyApp.model.User', {
extend: 'Ext.data.Model',
alias: 'model.user',
hasMany: [{
model: 'MyApp.model.SalesDepartmentModel',
associationKey: 'salesDepartment',
role: 'salesDepartment'
}],
requires: [
'Ext.data.field.String'
],
fields: [{
type: 'string',
name: 'userName'
}, {
type: 'string',
name: 'firstName'
}, {
type: 'string',
name: 'lastName'
}, {
name: 'salesDepartment'
}, {
name: 'roles'
}, {
type: 'string',
name: 'email'
}, {
name: 'notificationFrequencyId'
}]
});
Further reading
I suggest you read more on associations and the tagfield source code
In terms of saving records back to the server, I would recommend looking at sessions
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>
I added two extra columns in in kendo grid which have not data in datasource. I add two textboxes in these two columns using template.Now I want to push the values of these two columns into an angular array oncheckbox checked. I search alot about this in google and in stackoverflow. But did not find any relevant answer for my problem Here is the code for adding template in kendo grid
$scope.qualifySubGridColumns = [
{ template: "<input type='checkbox' class='subCheck checkbox' ng-click='getSelectedRow(dataItem)' />" },
{ field: "subList", template: "<input type='number' ng-minlength='0' ng-init='prefferedUser.subList=0'>", title: "SubList" },
{ field: "level", template: '<input type="number" ng-model="prefferedUser.level" ng-minlength="0" ng-init="prefferedUser.level=0">', title: "Level" },
{ field: "lastName", title: "Last Name" },
{ field: "firstName", title: "First Name" },
{ field: "email", title: "Email" },
{ field: "address1", title: "Address 1" },
{ field: "address2", title: "Address 2" },
{ field: "phone", title: "Phone" }
];
and here I want to fetch these columns. But I have no Idea How can i fetch sublist and level field values in selectedRow Array. Please Experts Help me
$scope.selectedRow = [];
$scope.getSelectedRow = function (data) {
$scope.selectedRow.push({ userId: data.substituteId});
// $scope.selectedRow.push({ userId: data.substituteId, sublist: sublist, level: level });
console.log("mydata", $scope.selectedRow);
};
I'm not sure familiar with anuglar, but I've done this in the past using just kendo.
Here is a simple grid I created with a checkbox on one column, and a text input on another column. The dataSource schema only has an id and a name property. But by adding the data bindings to the template input fields, those properties will be added to the dataSource data item.
<div id="grid"
data-role="grid"
data-columns="[{ field: 'id', title: 'Select', template: '<input type=\'checkbox\' data-bind=\'checked: selected\' />' },
{ field: 'name', title: 'Name' },
{ title: 'Age', template: '<input type=\'number\' data-bind=\'value: age\' />' }]"
data-bind="source: itemsDataSource">
</div>
Then if you dump the dataSource the grid is bound to (itemsDataSource in my instance), the items that you have changed the checkbox, or textbox will have those properties.
See sample running at JSBin
From there, you should be able to pull out the fields you want if the selected field is true etc.
I am not sure but it worked for me...
Html:
<body>
<div kendo-grid k-options="gridOptions" k-ng-delay="gridOptions" k-on-change="selected=data"></div>
</body>
<script id="RowId" type="text/x-kendo-template">
<tr>
<td><input ng-model="dataItem.Name"></td>
</tr>
</script>
Js:
$scope.Array= new kendo.data.ObservableArray([Object,Object]);
$scope.gridOptions = {
dataSource: new kendo.data.DataSource({
pageSize: 20,
data: $scope.Array,
autoSync: true,
schema: {
model: {
id: "Id",
}
}
}),
sortable: true,
resizable: true,
autoSync: true,
scrollable: true,
columns: [
{
field: "Name",
title: "Name",
editable: True,
},
],
rowTemplate: kendo.template($("#RowId").html()),
};
How to add conditional when showing data in ui-grid cellTemplate below:
$scope.status = ['Active', 'Non Active', 'Deleted'];
$scope.gridOptions = {
columnDefs: [{
field: 'code'
}, {
field: 'name'
}, {
field: 'status',
cellTemplate: '<div>{{status[row.entity.status]}}</div>'
}]
};
The expected result should be row status show Active/NonActive/Deleted.
Here is the plunker
Thanks in advance.
You have to use externalScopes.
In your markup define the gridholder like this.
<div ui-grid="gridOptions" external-scopes="states" class="grid"></div>
And in your controller use this code:
var statusTxt = ['Active', 'Non Active', 'Deleted'];
$scope.states = {
showMe: function(val) {
return statusTxt[val];
}
};
var statusTemplate = '<div>{{getExternalScopes().showMe(row.entity.status)}}</div>';
$scope.gridOptions = {
columnDefs: [{
field: 'code'
}, {
field: 'name'
}, {
field: 'status',
cellTemplate: statusTemplate
}]
};
Or use an angular filter.
Note that this only renders text. The best approach would be to transform myData to have real text states before using it in ui-grid. Just in case you want to do some text based filtering later on.
Here is a Plunker
I would suggest to use ng-if solve this problem.
$scope.gridOptions = {
columnDefs: [{
field: 'code'
}, {
field: 'name'
}, {
field: 'status',
cellTemplate: '<div ng-if="row.entity.status == 0">Active</div><div ng-if="row.entity.status == 1">Non Active</div>'
}]
};
I have got another solution for you without using external scopes:
The Template looks like this:
var statusTemplate = '<div>{{COL_FIELD == 0 ? "Active" : (COL_FIELD == 1 ? "Non Active" : "Deleted")}}</div>';
Here is the plunker:
http://plnkr.co/edit/OZtU7GrOskdqwHW5FIVz?p=preview
Use a cellFilter.
columnDefs: [{
field: 'code'
}, {
field: 'name'
}, {
field: 'status',
cellFilter: 'mapStatus'
}]
app.filter('mapStatus', function() {
var statusMap = ['Active', 'Non Active', 'Deleted'];
return function(code) {
if (!angular.isDefined(code) || code < 0 || code > 2) {
return '';
} else {
return statusMap[code];
}
};
});
plunker
You must change your template. When you are referring to external scopes in angular-ui-grid you may use grid.appScope.
var statusTemplate = '<div>{{grid.appScope.status[row.entity.status]}}</div>';
Try below script. It is working for me.
app.controller('MainCtrl', ['$scope',
function($scope) {
var statusTxt = ['Active', 'Non Active', 'Deleted'];
$scope.showMe= function(val) {
return statusTxt[val];
};
var statusTemplate = '<div>{{grid.appScope.showMe(row.entity.status)}}</div>';
$scope.gridOptions = {
columnDefs: [{
field: 'code'
}, {
field: 'name'
}, {
field: 'status',
cellTemplate: statusTemplate
}]
};
$scope.gridOptions.data = [{
"code": "Cox",
"name": "Carney",
"status": 0
}, {
"code": "Lorraine",
"name": "Wise",
"status": 1
}, {
"code": "Nancy",
"name": "Waters",
"status": 2
}];
}
]);