AngularJS Multiselect checkbox dropdown with Selected Checkbox - javascript

I have a Simple Multiselect checkbox dropdown. As I have given below.
<select name="edit_tags" class="form-control" id="advisor_article_tagsx" multiple="" required>
<option ng-repeat="service in services" value="{{service.id}}">{{service.name}}</option>
</select>
Now I want that some checkbox like index 1,3,5 comes with selected checkbox.
My AngularJS Code is given below.
$scope.article_edit = function(articles) {
$scope.services = [$scope.model[0], $scope.model[1]];
}
Then What I need to do for that ??
Thanks

Why not use ng-options which has good support & also ng-model on select field. So your template can be:
<select name="edit_tags" class="form-control" id="advisor_article_tagsx" multiple=""
required ng-model="selected" ng-options="service as service.name for service in services">
</select>
Working plunker example

Related

Filter table using select option in angular

<select class="form-control" [(ngModel)]="selected" id="department" name="department"
(ngModelChange)="onSelect(selected)">
<option value='' >Select Department</option>
<option *ngFor="let data of result" [value]="data.id" >
{{data.name}}
Please i want to filter table based on the data passed from the select oprion dropdown. Any help please.
This is the table i am using
</ngx-datatable-column> -->
We can filter the array using the selected value.
I created a stackblitz for the scenario:
Stackblitz: https://stackblitz.com/edit/angular-ivy-qdboig
Let us know if it helps or if you have any more questions.

How to assign default Values to Multiple select option in Angularjs

I have created multiple select option drop-downs. Now I want to assign default selected values to the multiple select options. I have created code and it's working fine but the problem with the options value. I want id as a value in options.
javascript:-
$scope.availableOptions = [{"client_id":"62024","client_name":"Advantage Land Co"},{"client_id":"59194","client_name":"Alpha - Brookings, SD"},{"client_id":"62413","client_name":"B and B Auto"},];
$scope.simpli_client = {};
$scope.simpli_client =["62024", "62413"];
and php code:-
<select id="multiSelect" class="ng-pristine ng-valid form-control" multiselect="" multiple="" ng-model="simpli_client" ng-options="option.client_id as option.client_name for option in availableOptions"></select>
It's working fine. Showing all the default values selected but I need client_id in option values. like this:-
<select id="multiSelect" class="ng-pristine ng-valid form-control" multiselect="" multiple="" ng-model="simpli_client" ng-options="option.client_id as option.client_name for option in availableOptions"><option value="62024" selected="selected">Advantage Land Co</option><option value="59194">Alpha - Brookings, SD</option><option value="62413" selected="selected">B and B Auto</option></select>
I need values in the option fields (value="62024").
Here is a working fiddle : https://jsfiddle.net/xhph7qny/1/
You can use track by provided by angular ng-options.
HTML
<select id="multiSelect" class="ng-pristine ng-valid form-control" multiselect="" multiple="" ng-model="simpli_client" ng-options="option.client_id as option.client_name for option in availableOptions track by option.client_id"></select>
JS
$scope.simpli_client =[{"client_id":"62024","client_name":"Advantage Land Co"},{"client_id":"59194","client_name":"Alpha - Brookings, SD"}];
});
The only change is JS is that the ng-model should contain the entire objects rather than only client ids.

ng-select is not working in angularjs?

I am facing some problem in drop down of angular js. Below is the html code
<select class="form-control" ng-init="projectName = options[0]" ng-model="projectName" name="projectName" ng-options="project.name for project in projectsList track by project.id" ng-class="{'submitted': submitted}" ng-selected="projectName" required>
<option value="">Select Project</option>
</select>
all the things coming fine.
But what i want is after selecting value from drop down we submit the form and in success am getting the value what i have selected. Now i want to set this value for the drop down, how to do it ?
In jquery it works
$("dropdown").val('successCallBackValue');
but in angular how to do it
After the sucess of the form submit just set the value in the dropdown model value as,
$scope.projectName = yourValue; // whatever selected value you are getting.
<select class="form-control" ng-init="projectName = options[0]" ng-model="projectName" name="projectName" ng-options="project.name for project in projectsList track by project.id" ng-class="{'submitted': submitted}" required>
<option value="">Select Project</option>
</select>

How to populate a value in a select ( HTML ) list in AngularJS

I am using a select list in a particular edit form page. Whenever a particular entry has to be edited, a new state with the edit form appears. It has a select list which has to populated with one of its options (ideally the value that is fetched from the server using API has to be populated into the select).
I am using Angular JS on the client side. Could somebody please tell me how to achieve the same thing using Angular JS?
I need that initial value inserted by the user in the select to be shown up as a default when the edit page is opened.
(MOREOVER, MY PLACEHOLDER NOT WORKING:"Choose a country")
Lets say there is something like this:
<div class="col-md-8">
<select data-placeholder="Choose a Country..." class="chosen-select form-control" style="width:350px;" tabindex="2" required="" ng-model="location">
<option value="" disabled selected>Select</option>
<option ng-repeat="location in locations1" value="{{location.id}}">{{location.name}}</option>
</select>
</div>
Use ngOptions.
<select ng-options="location as location.name for location in locations track by location.id" ng-model="selected"></select>
With ng-options you can do it like this:
<div class="col-md-8">
<select ng-model="selectedLocation" ng-options="location.id as location.name for location in locations1" class="chosen-select form-control" style="width:350px;" tabindex="2">
<option value="" disabled selected>Choose a Country</option>
</select>
</div>
The location.id is set to the options value and the location.name is set to the text.
To use a placeholder in a select is kind of what the default options does for you. So I would set the default option text to "Choose a country". Se this SO answer on that topic

AngularJS: ng-model does not update when ng-options refreshes

I'm new to AngularJS, so I apologize if this question is naive.
We have cascading selects that populate as you select values. When the value of Select A changes, the values in Select B also change since they filter based on the value in Select A.
So here is the scenario:
Choose option from Select A
Choose option from Select B
Change selection for Select A
Observe that options in Select B update accordingly.
Observe that bound model for Select B does not update accordingly.
This seems so basic that we are really scratching our heads. What is the point of two-way data binding if this scenario isn't covered?
Here is my view:
<body ng-controller="MainCtrl">
Make:
<select ng-model="makeng" ng-options="option.value as option.display for option in makes">
<option ng-disabled="true" ng-selected="true" value="">Select a make</option>
</select>
<br />
{{makeng}}
<br /> <br />
Model:
<select ng-model="modelng" ng-options="option.display for option in models | filter:{make:makeng}">
<option ng-disabled="true" ng-selected="true" value="">Select a model</option>
</select>
{{modelng}}
</body>
Here's a Plunkr, demonstrating:
http://plnkr.co/edit/9XrKgW?p=preview
P.S. The above example is purely fictional and forked from another plunkr. Just the easiest way to demonstrate what we are seeing.
This is how it is supposed to work. The select control changes the model in response to a user's selection, but if you change the set of allowed values from underneath it (i.e. by filtering out) it keeps the model intact.
The way to make this work is by invalidating the model in response to a change in Select A:
Make:
<select ng-model="makeng"
ng-options="option.value as option.display for option in makes"
ng-change="modelng = undefined">
<option value="">Select a make</option>
</select>
<br />
Model:
<select ng-model="modelng"
ng-options="option.display for option in models | filter:{make:makeng}">
<option value="">Select a model</option>
</select>

Categories