Angular ui-select multiple dropdown filter - angularjs

I have the following list from which I need to create a multiple multi-select dropdown using ui-select
[
{
"Id":1,
"name":"Return on Equity (IFRS)",
"type":"profitability",
},
{
"Id":2,
"name":"Return on Assets (IFRS)",
"type":"profitability",
},
{
"Id":3,
"name":"EBITDA Margin (IFRS)",
"type":"profitability",
},
{
"Id":4,
"name":"EBIT Margin (IFRS)",
"type":"profitability",
},
{
"Id":5,
"name":"Net Profit Margin (IFRS)",
"type":"profitability",
},
{
"Id":8,
"name":"Cash Ratio",
"type":"liquidity",
},
{
"Id":9,
"name":"Quick Ratio",
"type":"liquidity",
},
{
"Id":10,
"name":"Current Ratio",
"type":"liquidity",
},
{
"Id":11,
"name":"Net Financial Liabilities",
"type":"debt",
}
];
I need to be able to select multiple "types" in the first dropdown based on which the corresponding "names" should be shown in the second dropdown. I seem to have hit a roadblock with this. Any help would be appreciated.
This is what I have tried so far. Plunker

let's use 2 custom filters:
app.filter('unique', function() {
return function(collection, keyname) {
return Object.keys(collection.reduce((res, curr) => {
res[curr.type] = curr;
return res;
}, {}));
};
});
app.filter('matchType', function() {
return function(collection, keynames) {
return collection.filter(item => ~keynames.indexOf(item.type));
};
});
unique filter returns an array of strings with unique types and matchType filters array by selected types.
HTML:
<ui-select multiple
ng-model="ctrl.multipleDemo.type"
theme="bootstrap"
style="width: 800px;">
<ui-select-match placeholder="Select type...">{{$item}}</ui-select-match>
<ui-select-choices repeat="type in ctrl.people | unique : 'type'">
<div ng-bind-html="type | highlight: $select.search"></div>
</ui-select-choices>
</ui-select>
<pre>{{ ctrl.multipleDemo.type | json }}</pre>
<br><br>
<ui-select multiple
ng-model="ctrl.multipleDemo.name"
ng-disabled="!ctrl.multipleDemo.type.length"
theme="bootstrap"
style="width: 800px;">
<ui-select-match placeholder="Select name...">{{$item.name}}</ui-select-match>
<ui-select-choices repeat="person in ctrl.people | matchType : ctrl.multipleDemo.type">
<div ng-bind-html="person.name | highlight: $select.search"></div>
</ui-select-choices>
</ui-select>
<pre>{{ ctrl.multipleDemo.name | json }}</pre>
plunker: http://plnkr.co/edit/6aqPwuRwoJdHUmvFOquI?p=preview
TODO: when type is removed from the first select, make sure to remove persons with removed type from the second ng-model

Related

How to set a default value in ui-select in angular?

I wrote a little program in Angular using ui-select elements. Here is the html code :
<ui-select ng-model="test.selectedContract">
<ui-select-match >
{{$select.selected.name}} - {{$select.selected.value}} ---- {{$select.selected.id.id}} *** {{$select.selected.policy.info.name }}
</ui-select-match>
<ui-select-choices group-by="groupByLetter"
repeat="contract in (contracts |
orSearchFilter: {id.id: $select.search, policy.info.name : $select.search} |
orderBy: 'name') track by contract.name">
{{contract.name}} - {{contract.value}} ---- {{contract.id.id}} *** {{contract.policy.info.name }}
</ui-select-choices>
</ui-select>
And here is the structure of my contracts object :
$scope.contracts = [{
name: "contract1.00",
value: 10,
id: {
id: 8000,
code: 2
},
policy: {
info: {
name: "test1",
country: "test"
}
}
}, //other contract objects
Finally, there is my plunker code : http://plnkr.co/edit/PigjGCj5ukxscJC7Wl97?p=preview
The problem is that I want to display "All" as a default value (if there is no selection) while my default value in my plunker is : " - ---- *** "
Can you please help me to set this default value please?
Thanks !
You could define a sort of toString function in your controller and put the result of that in the ui-select tag, instead of hard-code dashes and stars. If value is null, it should return "All"
Just set the
$scope.test.selectedContract = {
name: "all",
value: 10,
id: {
id: 8000,
code: 2
},
policy: {
info: {
name: "test1",
country: "test"
}
}
}
DEMO

Date format not changed with angular-formly when locales changes

I have a form generated by angular-formly.
I format the date using angular date filter
this.formlyFields = [
{
key: 'fromDate',
type: 'datepicker',
templateOptions: {
label: this.i18n.text('searchCase_FromDate'),
type: 'text',
placeholder: this.i18n.text('searchCase_FromDatePlaceholder'),
required: true,
datepickerOptions: {
format: 'shortDate',
'show-weeks': false,
}
},
expressionProperties: {
'templateOptions.label': '"searchCase_FromDate" | translate',
'templateOptions.placeholder': '"searchCase_FromDatePlaceholder" | translate'
}
}
]
Here is the template I'm using:
<input type="text"
id="{{::id}}"
name="{{::id}}"
ng-model="model[options.key]"
class="form-control"
uib-datepicker-popup="{{to.datepickerOptions.format}}"
is-open="datepicker.opened"
datepicker-options="to.datepickerOptions"
current-text="{{'datepickerCurrent' | translate}}" clear-text="{{'datepickerClear' | translate}}" close-text="{{'datepickerClose' | translate}}" />
When I'm on this view and change the locale the date won't be reformated.
It will if I change the locale before arriving on this page.
I'm changing the locale with angular-dynamic-locale.
It works if in a view I display like this:
<div>{{vm.formlyModel.fromDate | date:'shortDate'}}</div>
But how to have the same rresult with angular-formly ?
Thnaks for your help.

KendoUI Grid does not invoke update function in popup mode with editable template

I'm using a Kendo UI Grid with AngularJS. The grid has a 'popup' mode editable kendo template. The grid invokes the create, destroy & delete functions; however the update function won't get called. Strangely when I change the edit mode to 'inline', the update function is called. Below are the code snippets from my application :
Main UI Grid:
<div class="container-fluid">
<kendo-grid style="margin-top: 2em" k-options="ctrl.fundGridOptions" k-scope-field="kgrid" id="myGrid"></kendo-grid>
</div>
Edit Template:
<script id="edit-template" type="text/x-kendo-template">
<div class="container">
<div class="well">
Fund :
<select kendo-drop-down-list k-options="ctrl.fundOptions" style="width: 130px;" ng-model="dataItem.GenevaId"></select>
<!--<select kendo-drop-down-list k-data-source="ctrl.funds" style="width: 130px;" ng-model="dataItem.GenevaId"></select>-->
NAV Change Threshold
<input kendo-numeric-text-box k-min="0" k-max="100" k-ng-model="value" style="width: 60px;" ng-model="dataItem.NAVThreshold" />
NAV Source
<select k-data-source="ctrl.navSources" kendo-drop-down-list k-option-label="'-Select-'" style="width: 130px;" ng-model="dataItem.NAVSource"></select>
Frequency
<select k-data-source="ctrl.frequencyList" kendo-drop-down-list k-option-label="'-Select-'" style="width: 130px;" ng-model="dataItem.Frequency"></select>
Type
<select k-data-source="ctrl.typeList" kendo-drop-down-list k-option-label="'-Select-'" style="width: 130px;" ng-model="dataItem.Type"></select>
</div>
<div kendo-grid="ctrl.currencyKendoGrid" style="margin-top: 2em" k-options="ctrl.currencyGridOptions"></div>
</div>
</script>
Grid Options:
ctrl.fundGridOptions = {
dataSource: {
transport: {
update: function (options) {
DataSvc.updateFund(e.data).then(function (response) {
e.success(e.data);
});
},
},
schema: {
model: {
id: "FundId",
fields: {
FundId: { type: "number", editable: false, nullable: true },
GenevaId: { type: "string", editable: true },
NAVThreshold: { type: "number", editable: true },
NAVSource: { type: "string", editable: true },
Frequency: { type: "string", editable: true },
Type: { type: "string", editable: true },
}
}
},
},
sortable: true,
columns: [
{ field: "GenevaId", title: "Fund Name" },
{ field: "NAVThreshold*100", title: "NAV Threshold", template: '#=kendo.format("{0:p}", NAVThreshold)#' },
{ field: "NAVSource", title: "NAV Source" },
{ field: "Frequency", title: "Frequency" },
{ field: "Type", title: "Type" },
{ command: ["edit", "destroy"], title: " " }
],
detailTemplate: kendo.template($("#detail-template").html()),
detailInit: function (e) {
kendo.bind(e.detailRow, e.data);
},
dataBound: function (e) {
var grid = e.sender;
if (grid.dataSource.total() == 0) {
var colCount = grid.columns.length;
$(e.sender.wrapper)
.find('tbody')
.append('<tr class="kendo-data-row"><td colspan="' + colCount + '" class="no-data">Sorry, no data :(</td></tr>');
}
},
editable: {
mode: "popup",
template: kendo.template($("#edit-template").html()),
window: {
title: "Edit Fund Details",
animation: false,
height: "600",
width: "1200"
}
},
edit: function (e) {
if (e.model.Currencies)
ctrl.currencyKendoGrid.dataSource.data(e.model.Currencies);
},
toolbar: [
{
name: 'create',
text: 'Add Fund',
}],
};
Could anyone help me understand the reason why the 'update' function won't be called in 'popup' mode, but gets called in 'inline' mode ? Appreciate any responses in advance.
I know this is question old, but I came across it searching for an answer. I think the issue might be that your (and my) edit template uses ng-model binding to edit the dataItem ie we're using angularjs. This doesn't actually seem to change the dirty property on the dataItem. I solved the issue by using ng-change on each control. It's a pain, but seems to work.
<input id="Title" type="text" class="k-textbox" ng-model="dataItem.Title" ng-change="dataItem.dirty=true"/>

normal select list to angular-ui-select is not working

How to convert normal select list code into angular-ui-select directive code.
My code html:
<select class="input-small tight-form-input" ng-model="panel.valueName" ng-options="f.value as f.text for f in bigValueOptions" ng-change="render()"></select>
My controller code:
$scope.bigValueOptions= [{
text: 'min',
value: 'min'
}, {
text: 'max',
value: 'max'
}, {
text: 'avg',
value: 'avg'
}, {
text: 'current',
value: 'current'
}, {
text: 'total',
value: 'total'
}];
What I have tried:
<ui-select id="viewSelector"
class="viewSelector input-small tight-form-input"
ng-model="panel.valueName"
theme="selectize"
ng-change="render()">
<ui-select-match placeholder="Select">{{$select.selected.text}}</ui-select-match>
<ui-select-choices repeat="f in bigValueOptions">
<div ng-bind-html="f.text"></div>
</ui-select-choices>
</ui-select>
panel.valueName is not having correct value. How to fix this or how to convert normal select into ui-select directuve code.
Please guide.
It works for me: Plunker
Did you defined $scope.panel = {};?
JS:
app.controller('DemoCtrl', function($scope, $http) {
$scope.bigValueOptions= [{
text: 'min',
value: 'min'
}, {
text: 'max',
value: 'max'
}, {
text: 'avg',
value: 'avg'
}, {
text: 'current',
value: 'current'
}, {
text: 'total',
value: 'total'
}];
$scope.panel = {};
});
HTML:
<body ng-controller="DemoCtrl">
Selected object: {{panel.valueName}}
<ui-select id="viewSelector"
class="viewSelector input-small tight-form-input"
ng-model="panel.valueName"
theme="selectize"
ng-change="render()">
<ui-select-match placeholder="Select">{{$select.selected.text}}</ui-select-match>
<ui-select-choices repeat="f in bigValueOptions">
<div ng-bind-html="f.text"></div>
</ui-select-choices>
</ui-select>
</body>

How to use ng-click on ng-option( or other way to assign value)

How to use ng-options.
$scope.fieldTable = [
{
filed:"text",
title:"Global"
},
{
field: "relatedentity",
title: "Entity"
},
{
field:"title",
title:"Title"
},
{
field: "content",
title: "Content"
}
]
I want to build a which use the title as displayed and when select something, popout a alert window which display the according field. The initial selection is
{
filed:"text",
title:"Global"
}
Can anyone help?
var app = angular.module('stack', []);
app.controller('MainCtrl', function($scope) {
$scope.fieldTable = [{
field: "text",
title: "Global"
}, {
field: "relatedentity",
title: "Entity"
}, {
field: "title",
title: "Title"
}, {
field: "content",
title: "Content"
}];
$scope.selected = $scope.fieldTable[0];
$scope.hasChanged = function() {
alert($scope.selected.field);
}
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<body ng-app="stack" ng-controller="MainCtrl">
<select ng-options="item.title for item in fieldTable" ng-model="selected" ng-change="hasChanged()">
</select>
</body>
You can use ng-change to do that
<select ng-options="item.title as item.title for item in fieldTable track by item.title" ng-model="selected" ng-change="onChanged()">
then in your onChange() method in the controller you can do whatever you want it to do :) In your case , show an alert with the value
edit:
https://plnkr.co/edit/4csDtFVH5mKd7Xr39WtG?p=preview

Resources