How to add multiple input box dynamically with dropdown in angularjs - angularjs

I am having this problem which I can't find the solution to.
1) I have a dropdown list which the data is pull in from a json file
2) When the user selects a item I want to dynamically add a input box without a button.
Got a jQuery code but wanted to do it the angular way, but read that ng-Change is not the same as jquery .change?
Can anyone help or point me into the right direction of how to do this.
HTML
<div class="main-content__right" ng-controller="QuestionController">
<div ng-repeat="element in questionList">
<fieldset>
<div id="add-more" class="well">
<div class="field">
<div style="width:100%;" class="dropdown">
<select name="{{options.name}}" id="select" data-ng-model="formData"
data-ng-options="options as options.label for options in element.inputElement | orderBy:'label'"
ng-change="onCategoryChange(formData)">
<option value="" data-id="null" disabled="" selected="">Select an item...</option>
</select>
</div>
</div>
{{formData}}
</div>
</fieldset>
</div>
app.js
var app = angular.module("cab", []);
app.controller('QuestionController', function($scope) {
var $scope.formData = {};
$scope.questionList = [{
"sectionTitle": "Travel",
"subTitle": "How much do you spend on these items for your car?",
"inputType": "select",
"inputElement": [{
"label": "Public transport",
"name": "travelOutgoing",
"helpInfo": "include train journeys",
"type": "select"
}, {
"label": "Taxis",
"name": "travelOutgoing",
"type": "select"
}, {
"label": "Bicycle",
"name": "travelOutgoing",
"helpInfo": "general running costs such as repair or rental",
"type": "select"
}, {
"label": "Car rental",
"name": "travelOutgoing",
"helpInfo": "include fuel, parking charges and tolls",
"type": "select"
}
]
}];
$scope.onCategoryChange = function () {};
});
can be found on http://plnkr.co/edit/PPDYKjztPF528yli9FbN?p=preview

Your controller function needs to add each selection to an array on scope:
Controller
app.controller('QuestionController', function($scope) {
$scope.formData = [];
$scope.selectedValue = {};
$scope.questionList = [{
"sectionTitle": "Travel",
"subTitle": "How much do you spend on these items for your car?",
"inputType": "select",
"inputElement": [{
"label": "Public transport",
"name": "travelOutgoing",
"helpInfo": "include train journeys",
"type": "select"
}, {
"label": "Taxis",
"name": "travelOutgoing",
"type": "select"
}, {
"label": "Bicycle",
"name": "travelOutgoing",
"helpInfo": "general running costs such as repair or rental",
"type": "select"
}, {
"label": "Car rental",
"name": "travelOutgoing",
"helpInfo": "include fuel, parking charges and tolls",
"type": "select"
}]
}];
$scope.onCategoryChange = function(selectedItem) {
$scope.formData.push(selectedItem)
};
});
Then you can use an ng-repeat to render all of the items in formData.
HTML
<fieldset>
<div id="add-more" class="well">
<div class="field">
<div style="width:100%;" class="dropdown">
<select name="{{options.name}}" id="select" data-ng-model="selectedValue" data-ng-options="options as options.label for options in element.inputElement | orderBy:'label'" ng-change="onCategoryChange(selectedValue)">
<option value="" data-id="null" disabled="" selected="">Select an item...</option>
</select>
</div>
</div>
<div ng-repeat="item in formData track by $index">
<input ng-model="item.label" />
</div>
</div>
</fieldset>

Related

Get month from date in angularjs ng-repeat

I have an AngularJS 1.X app with a date field retrieved from a .json file. The date is being converted from a string to a date object to order the list but I need to capture the "month" part of the date to group by and for separate display but can't figure it out.
Here's my controller and HTML:
Angular Controller:
(function () {
"use strict";
angular.module("xxyyzz")
.controller("displayEventCtrl", displayEventCtrl);
function displayEventCtrl($http) {
var model = this;
model.header = "Upcoming Events";
model.events = [];
$http.get("/App/Data/eventCalendar.json")
.then(function (response) {
model.events = response.data;
console.log(model.events);
});
model.sortDate = function (event) {
var date = new Date(event.date);
return date;
}
}
})();
HTML:
<div ng-controller="displayEventCtrl as model">
<h3><i class="fa fa-calendar"></i> {{model.header}}</h3>
<div class="list-group">
<a href="#" class="list-group-item" ng-repeat="event in model.events | orderBy : -model.sortDate : true track by $index">
<div class="list-group-item-heading">
<h4>{{event.title}}</h4>
</div>
<p class="list-group-item-text">
Tuctus placerat scelerisque.
</p>
<div class="row">
<div class="col-xs-6">
<span class="small">{{event.location}}</span>
</div>
<div class="col-xs-6">
<span class="small pull-right">
{{event.startTime}} - {{event.endTime}}
</span>
</div>
</div>
<div class="row">
<div class="col-xs-9">
<span class="small">{{event.city}}, {{event.state}}</span>
</div>
<div class="col-xs-3">
<span class="small pull-right">{{event.date}}</span>
<div class="clearfix"></div>
</div>
</div>
</a>
</div>
</div>
JSON example:`
[
{
"id": 1,
"title": "Christmas Parade",
"date": "12/25/2017",
"city": "Dallas",
"state": "TX",
"location": "Galleria Mall",
"startTime": "11:00",
"endTime": "15:00"
},
{
"id": 2,
"title": "Thanksgiving Parade",
"date": "11/23/2017",
"city": "Denver",
"state": "CO",
"location": "Mile High Stadium",
"startTime": "11:00",
"endTime": "15:00"
},
{
"id": 3,
"title": "Halloween Parade",
"date": "10/31/2017",
"city": "Sleepy Hollow",
"state": "NY",
"location": "Ichabod Crane House",
"startTime": "19:00",
"endTime": "21:00"
},
{
"id": 4,
"title": "Independence Day Fireworks",
"date": "07/04/2017",
"city": "Washington",
"state": "DC",
"location": "National Mall",
"startTime": "11:00",
"endTime": "15:00"
}
]
Any help would be appreciated.
You can use js api named moment. Inject moment as dependency. You can use moment to get date in any formate you want.
moment ().get ('month');
To get the month, use the getMonth() function
var Xmas95 = new Date('December 25, 1995 23:15:30');
var month = Xmas95.getMonth();
console.log(month); // 11
My issue is that I am trying to pull the date from the array in the ng-repeat. So I can't declare the variable like above as it is part of a returned array {{event.date}}. What I need to know is how to get that date part from the nested item.
Use a custom filter to convert the text data to a Date object:
angular.module("app",[])
.filter("toDate", function() {
return function(data) {
return new Date(data);
};
})
.run(function($rootScope) {
var vm = $rootScope;
vm.Xmas95 = 'December 25, 1995 23:15:30';
})
<script src="//unpkg.com/angular/angular.js"></script>
<div ng-app="app">
Month number = {{Xmas95 | toDate | date: 'MM'}}
</div>
For more information, see
AngularJS Developer Guide - Creating Custom Filters
AngularJS date Filter API Reference

filter on selected item in dropdown list

i'm trying to make my {{client.tenant}} be the source to filter out my table later on. the table should only view customer 1 or customer 2 based on the selection in my dropdown list. I have the feeling the input in my dropdown list isn't stored anywhere. do you have some tips what im doing wrong? down is a sample from my code. i tested around alot so sorry for the code.:)
<body>
<div class="container" ng-controller="menuController">
<select>
<option ng-model="dropdownString" ng-repeat="client in products | unique:'tenant'">{{client.tenant}}</option>
</select>
<div class="tab-content">
<ul class="media-list tab-pane fade in active">
<div class="row row-content">
<div class="col-xs-12">
<ul class="media-list">
<li class="media">
<div class="media-left media-middle">
</div>
<div class="media-body">
<table>
<tr>
<th class="table-1">Product description</th>
<th>SKU</th>
<th>Tenant</th>
<th>Select</th>
</tr>
<tr ng-repeat="product in products | searchFor:searchString">
<td>{{product.description}}</td>
<td>{{product.sku}}</td>
<td>{{product.tenant}}</td>
<td><input type="checkbox"></td>
</tr>
</table>
</div>
</li>
</ul>
</ul>
</div>
</div>
</div>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.7/angular.min.js"></script>
<script>
var app = angular.module('productBackend', []);
app.controller('menuController', function ($scope, $http) {
$scope.search=[];
$http.get('./scripts/products.json')
.then(function (res) {
$scope.products = res.data;
});
});
app.filter('searchFor', function () {
return function (arr, searchString) {
if (!searchString) {
return arr;
}
var result = [];
searchString = searchString.toLowerCase();
angular.forEach(arr, function (item) {
if (item.tenant.toLowerCase().indexOf(searchString) !== -1) {
result.push(item);
}
});
return result;
};
});
app.filter('unique', function() {
return function(collection, keyname) {
var output = [],
keys = [];
angular.forEach(collection, function(item) {
var key = item[keyname];
if(keys.indexOf(key) === -1) {
keys.push(key);
output.push(item);
}
});
return output;
};
});
app.controller('ExampleController', ['$scope', function($scope) {
$scope.data = {
singleSelect: null,
multipleSelect: [],
option1: 'option-1'
};
$scope.forceUnknownOption = function() {
$scope.data.singleSelect = 'nonsense';
};
}]);
</script>
</body>
Json File:
[{
"sku": "S-MLX-SEC-002",
"description": "Intrusion Prevention",
"tenant": "Customer 1"
}, {
"sku": "S-MLX-MCA-007",
"description": "Microsoft Lync Enterprise - Private Cloud",
"tenant": "Customer 1"
},{
"sku": "S-MLX-SEC-002",
"description": "Intrusion Prevention",
"tenant": "Customer 1"
}, {
"sku": "S-MLX-SEC-004",
"description": "Local privacy compliance",
"tenant": "Customer 1"
}, {
"sku": "S-MLX-S02-100",
"description": "Location based Access",
"tenant": "Customer 1"
},{
"sku": "S-MLX-SEC-002",
"description": "Intrusion Prevention",
"tenant": "Customer 1"
}, {
"sku": "S-MLX-S02-510",
"description": "Two factor authentication - Hard Token Security",
"tenant": "Customer 1"
}, {
"sku": "S-MLX-MCA-007",
"description": "Microsoft Lync Enterprise - Private Cloud",
"tenant": "Customer 1"
},{
"sku": "S-MLX-CHG-001",
"description": "Changes level Business - for Global Desktop Bundel",
"tenant": "Customer 1"
}, {
"sku": "S-MLX-GLD-003",
"description": "Global Desktop Business Bundel - Performance - Private Cloud",
"tenant": "Customer 1"
}, {
"sku": "S-MLX-GLD-012",
"description": "Global Desktop Foundation - Standard - Private Cloud",
"tenant": "Customer 1"
},{
"sku": "S-MLX-OFF-001",
"description": "Microsoft Office Standard - Private Cloud",
"tenant": "Customer 1"
}, {
"sku": "S-MLX-LOB-824",
"description": "Exact Financials (CL) - Private Cloud",
"tenant": "Customer 1"
}, {
"sku": "S-MLX-MCA-007",
"description": "Microsoft Lync Enterprise - Private Cloud",
"tenant": "Customer 2"
},{
"sku": "S-MLX-SEC-002",
"description": "Intrusion Prevention",
"tenant": "Customer 2"
}, {
"sku": "S-MLX-SEC-004",
"description": "Local privacy compliance",
"tenant": "Customer 2"
}, {
"sku": "S-MLX-S02-100",
"description": "Location based Access",
"tenant": "Customer 2"
},{
"sku": "S-MLX-SEC-002",
"description": "Intrusion Prevention",
"tenant": "Customer 2"
}, {
"sku": "S-MLX-S02-510",
"description": "Two factor authentication - Hard Token Security",
"tenant": "Customer 2"
}, {
"sku": "S-MLX-MCA-007",
"description": "Microsoft Lync Enterprise - Private Cloud",
"tenant": "Customer 2"
},{
"sku": "S-MLX-CHG-001",
"description": "Changes level Business - for Global Desktop Bundel",
"tenant": "Customer 2"
}, {
"sku": "S-MLX-GLD-003",
"description": "Global Desktop Business Bundel - Performance - Private Cloud",
"tenant": "Customer 2"
}, {
"sku": "S-MLX-GLD-012",
"description": "Global Desktop Foundation - Standard - Private Cloud",
"tenant": "Customer 2"
},{
"sku": "S-MLX-OFF-001",
"description": "Microsoft Office Standard - Private Cloud",
"tenant": "Customer 2"
}, {
"sku": "S-MLX-LOB-824",
"description": "Exact Financials (CL) - Private Cloud",
"tenant": "Customer 2"
}]
Plunkr
https://embed.plnkr.co/hfB75e6w9sYZzDkwyh24/
you just change the following code
<select>
<option ng-model="dropdownString" ng-repeat="client in products | unique:'tenant'">{{client.tenant}}</option>
</select>
to
<select ng-model="dropdownString" >
<option ng-repeat="client in products | unique:'tenant'">{{client.tenant}}</option>
</select>
I'm not sure it will work or not.
this is how I fix you problem. My code is given below :
HTML
<input type="text" class="search" ng-model="searchx.sku" placeholder="Enter your search terms" />
<select ng-model="searchx.tenant">
<option ng-repeat="client in products | unique:'tenant'">{{client.tenant}}</option>
</select>
<div class="tab-content">
<ul class="media-list tab-pane fade in active">
<div class="row row-content">
<div class="col-xs-12">
<ul class="media-list">
<li class="media">
<div class="media-left media-middle">
</div>
<div class="media-body">
<table>
<tr>
<th class="table-1">Product description</th>
<th>SKU</th>
<th>Tenant</th>
<th>Select</th>
</tr>
<tr ng-repeat="product in products | filter: searchx">
<td>{{product.description}}</td>
<td>{{product.sku}}</td>
<td>{{product.tenant}}</td>
<td><input type="checkbox"></td>
</tr>
</table>
</div>
</li>
</ul>
</ul>
</div>
Angular.js
var app = angular.module('productBackend', []);
app.controller('menuController', function ($scope, $http) {
$scope.searchx = {};
$scope.search=[];
$http.get('./products.json')
.then(function (res) {
$scope.products = res.data;
});
});
app.filter('unique', function() {
// same code
});
app.controller('ExampleController', ['$scope', function($scope) {
// same code
}]);

In Angular how can you dynamically add an input to be applied once

I have searched the web and found no example. I wonder if someone could help.
I have two instances of a dropdown or maybe more and I want to apply the add input dynamically to only that form but it applies to everyone with that ng-model everywhere on the web page.
The code is here https://plnkr.co/edit/PPDYKjztPF528yli9FbN
HTML:
<div class="main-content__right" ng-controller="QuestionController">
<div ng-repeat="element in questionList">
<fieldset>
<div ng-repeat="choice in formOptionData track by $index">
<a class="remove-field remove u-pull-right" ng-click="remove()">Remove</a>
<input id="input{{choice.name}}" placeholder="{{choice.label}}" type="number" name="{{choice.name}}">
</div>
<div id="add-more" class="well">
<div class="field">
<div style="width:100%;" class="dropdown">
<select name="{{options.name}}" id="select" data-ng-model="selectedValue" data-ng-options="options as options.label for options in element.inputElement | orderBy:'label'" ng-change="onCategoryChange(selectedValue)">
<option value="" data-id="null" disabled="" selected="">Select an item...</option>
</select>
</div>
</div>
{{formData}}
</div>
</fieldset>
</div>
</div>
APP.js
var app = angular.module("cab", []);
app.controller('QuestionController', function($scope) {
$scope.formOptionData = [];
$scope.selectedValue = {};
$scope.questionList = [{
"sectionTitle": "Travel",
"inputType": "select",
"inputElement": [{
"label": "Public transport",
"name": "travelOutgoing",
"helpInfo": "include train journeys",
"type": "select"
}, {
"label": "Taxis",
"name": "travelOutgoing",
"type": "select"
}
]
},
{
"sectionTitle": "Leisure",
"title": "Enter the amount you spend on entertainment and leisure. Leave them blank if they don\"t apply to you.",
"inputType": "select",
"inputElement": [
{
"label": "Eating out",
"name": "leisureOutgoing",
"helpInfo": "Include coffees, teas and snacks",
"type": "select"
},
{
"label": "Going out",
"name": "leisureOutgoing",
"helpInfo": "Include drinks, taxis, admission charges",
"type": "select"
}
] }
];
$scope.onCategoryChange = function(selectedItem) {
this.formOptionData.push(selectedItem);
};
$scope.remove = function(element) {
this.formData.splice(element,1);
};
});

Add Multiple templates in one cell in kendo UI

I am new in kendo-ui.I am integrating kendo-ui with angularjs.I want to show the two buttons in single cell. I dont know how can i achieve this.
Here is my code:
<div id="mygrid" kendo-grid k-data-source="products"
k-pageable='{ "refresh": true, "pageSizes": true }'
k-columns='[
{ "field": "ProductName", "title": "Name"},
{ "field": "Supplier.SupplierName", "title": "Supplier" },
{ "field": "Category.CategoryName", "title": "Category" },
{ "template": "<button class=\"k-button\" ng-click=\"clickMe(#=ProductID#)\">Delete this item</button>", "title": "Delete" }
]'
k-sortable="true", k-groupable="true", k-filterable="true"
>
</div>
<div kendo-window="modal" k-title="'Title'" k-visible="false" k-modal="true">
Product Id clicked: {{ productId }}
</div>

$scope doesn't reflect ng-model

The ng-model defined in a select-tag does not seem to affect the controller $scope-variable with the same name.
Controller:
.controller('AccountCtrl', function($scope, $state, User, apiUrls) {
$scope.enviroments = apiUrls;
$scope.updateEnviroment = function() {
console.log($scope.selectedEnv);
}
})
View:
<label class="item item-input item-select">
<div class="input-label">
Enviroment
</div>
<select ng-model="selectedEnv" ng-options="item as item.name for item in enviroments" ng-change="updateEnviroment()" ng-init="selectedEnv = enviroments[0]">
</select>
</label>
<p>{{selectedEnv}}</p>
And for clearity, the constant:
myApp.constant('apiUrls', [
{ "name": 'Production', "id": 1, "url": 'http://api.server.com/api/v1' },
{ "name": 'Developement', "id": 2, "url": 'http://apidev.server.com/api/v1' },
{ "name": 'Local', "id": 3, "url": 'http://localhost/CABApi/api/v1'}
]
);
console.log($scope.selectedEnv); => undefined (?)
Obviously something is not correct that I'm failing to recognize, any ideas?

Resources