How do i group dropdown values in Angularjs?
HTML:
<div ng-model="tsInput" id="tsInput"
name="tsInput" multi-select input-model="atsInDB"
output-model="atsOut" button-label="shortenedName"
item-label="shortenedName" tick-property="ticked" max-height="500px"
selection-mode="single" max-labels="0" orientation="vertical"
default-label="{{'MUSELECTED'|translate}}" helper-elements="filter"
on-item-click="updateProfileList()"
on-enter="handleInput(labelFilter)"
group-property="categoryName">
</div>
Js code
tsInput =
SkillNotInProfile: true
categoryName: "Others"
categoryUUID: ""
description: "Test description"
name: "CSV"
shortenedName: "<span title="Comma-separated values (CSV)">Others : CSV</span>"
I expect the grouped dropdown values like https://www.w3schools.com/tags/tryit.asp?filename=tryhtml_optgroup
Please note that the above link is just output reference only.
Related
I have 4 check boxes and I want to hide/show them based on the value that I am getting in my JSON. I'll get only one name of checkbox in my JSON and only that particular checkbox will be shown to user and it'll be marked as checked. How can I do that? Here is my material checkbox code
<div fxLayout="row wrap" class="py-8" fxLayoutAlign="space-evenly">
<mat-checkbox>Fragile</mat-checkbox>
<mat-checkbox>Flyer</mat-checkbox>
<mat-checkbox>Time Stipulated</mat-checkbox>
<mat-checkbox>Gift Wrapped</mat-checkbox>
</div>
Here is my json
Since I am getting name as flyer so only flyer checkbox will be shown to user as checked while all other will not be shown to user.
Not Cleared with your question. You are using Mat-checkbox without using of JSON file
If your Json will be
SampleJson= [
{
Name: "Fragile",
isActive: true
},
{
Name: "Flyer",
isActive: true
},
{
name: "Time Stipulated",
isActive: true
},
{
name: "Gift Wrapped",
isActive: false
},
]
then modify your html to
<div fxLayout="row wrap" class="py-8" fxLayoutAlign="space-evenly">
<mat-checkbox [(ngModel)]="SampleJson[0].isActive">Fragile</mat-checkbox>
<mat-checkbox [(ngModel)]="SampleJson[1].isActive">Flyer</mat-checkbox>
<mat-checkbox [(ngModel)]="SampleJson[3].isActive">Time Stipulated</mat-checkbox>
<mat-checkbox [(ngModel)]="SampleJson[0].isActive">Gift Wrapped</mat-checkbox>
</div>
Hope it works for you. Happy coding
I am using angular js for displaying dynamic option value in selectbox.
One issue i am facing is by default it is appending one empty option to the
select box. Below is the selectbox code.
<select class='flowprovetermin' ng-change='gradeReloadData()' data-ng-model="provetermin[0]">
{loop="$proveterminOptions"}
<option value="{$value['proevetermin']}">{$value['proevetermin']}</option>
{/loop}
</select>
Please any one let me know how to remove the default empty value.
Look into using ng-options to set the <select> options.
Example (HTML):
<select class='flowprovetermin'
ng-change='gradeReloadData()'
ng-model="mySelectedOption"
ng-options="myOptions">
</select>
Example (JS):
$scope.myOptions = [
{ id: "option1", name: "Option 1" },
{ id: "option2", name: "Option 2" },
{ id: "option3", name: "Option 3" }
];
Note:
It looks like you're assigning ng-model to an object in an array.
This object should probably be assigned to a variable.
ng-model on your <select> box is the selected option.
i am seeing the example here https://docs.angularjs.org/api/ng/directive/select and this have 3 select with the same value, how can i have different values selected in my options?
i have this:
<li ng-repeat="tarea in tareas">
<input type="checkbox" ng-model="tarea.COMPLETADA" ng-change="updTarea(tarea.ID)"/>
<span class="done-{{tarea.COMPLETADA}}" >{{tarea.NAME}} {{tarea.CLASIFICADORES}}</span>
<select ng-model="selectedOpt"
ng-options="clasificador.NAME for clasificador in clasificadores">
</select>
<button class="buttons delete right" ng-click="delTarea(tarea.ID)"> Eliminar</button>
</li>
so i can have 5,10,15 options, and i want to make a selected item with the value that i have in tarea.CLASIFICADORES, i tried with this
$scope.selectedOpt = $scope.clasificadores[1]
but that make all the options with the same value, like in the example...
how can i make different selected item in my options dynamically with a value i have in my ng-repeat in every item?
i load the data with ajax...
my problem is to set the default selected item with the tarea.CLASIFICADORES. for example, i have a todo list that have a classifier, i want my ng-options to select by default my database value clasifier when the page is load
The problem is, that you are using the same scope variable for all selections. You could store the selected options in an array too like this:
function TestCtrl($scope) {
$scope.items = [
{ id: 1, class: 1 },
{ id: 2, class: 2 },
{ id: 3, class: 1 },
];
$scope.classes = [
{ name: "class 1", id: 1},
{ name: "class 2", id: 2},
{ name: "class 3", id: 3}
];
};
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app>
<div ng-controller="TestCtrl">
<div ng-repeat="currentItem in items">
<select ng-model="selectedClass[$index]" ng-init="selectedClass[$index]=(classes|filter:{id: currentItem.class})[0]" ng-options="class.name for class in classes track by class.id" >
</select>
selected class: {{selectedClass[$index]}}
</div>
</div>
</div>
In this example I take use of the variable $index, which is set by the ng-repeat directive. As the name suggests it contains the current index of the repeat-loop.
UPDATE
I updated the code-snippet so it sets the default value for each select input.
The different items now contain a field with the id of the corresponding class. I initialize the select input with ng-init. With this directive I set selectedClass[$index] which is the selected value for the current item. As we only have the class-id as a property of the items I use a filter to find the corresponding class object with the id (classes|filter:{id: currentItem.class})[0]
To get rid of the filter you could just set the class of each item to the full class-object instead of the id.
Code in controller:
$scope.infoOptions = [
{ name: 'Select Option', value: '0' },
{ name: 'Some Option', value: '1' }
];
HTML:
<select data-ng-model="nothing" data-ng-options="info.name for info in infoOptions ">
</select>
Angular puts that damn empty option at the top/selected by default. I've seen some answers to this question that suggest selecting a default option in the $scope for the form, but this select box is in a template in a dynamic form (ie. can be a number of select boxes). This is really only for demonstration purposes - is there anyway I can get rid of that empty option in a template?
Provide simple code that can be used to fill HTML drop down list values dynamically using AngularJS code. Please include multiple solutions if possible.
Here is one of possible solutions.
HTML / AngularJS Code:
<div ng-controller="TheController" ng-app>
Location:
<select ng-model="locationId" ng-options="item.LocationId as item.LocationName for item in locations"></select>
<br/>
LocationId: {{locationId}} <br/>
</div>
JavaScript:
function TheController($scope) {
$scope.locations = [
{LocationId : 1, LocationName : 'USA' },
{LocationId : 2, LocationName : 'Canada' },
{LocationId : 3, LocationName : 'Pakistan' } ];
}
Live Demo: http://jsfiddle.net/8een5/1/