Angularjs Full Select function implementation

Source: Internet
Author: User

Recently in a project to increase the full selection and anti-selection function, previously only have done the JQ version of the full selection and reverse selection.

Implementation Results :

1. Click the Select all checkbox to toggle Select All and empty all

2. Click the checkbox in the list, select all when all is selected

3. When you click the checkbox in the list to select it unchecked, the Select checkbox also becomes unchecked.

At first, I saw that the angular project would not be made after the whole selection.

Step One:

Then on the Internet to find information, found a very good demo, the results put it into the project when found that it can not achieve the needs of 2 and 3, then thought it was their own writing problems, and went to play the next demo, found that the demo can only achieve function 1.

Demo Address: Demo case

Lesson : After encountering the demo to all play before and then put in the project.

Step Two:

And then found a blog, this is the method inside can be perfect to achieve my needs. I used method 3 to put the validation in the project and put some of the code out here. In the project to replace the parameters according to the actual situation.

Blog Address: Success Stories Blog

Success Stories:

Html

<DivNg-controller= "Mycontroller">    <label for= "Flag">Select All<inputID= "Flag"type= "checkbox"Ng-model= "Select_all"Ng-change= "SelectAll ()">    </label>    <ul>        <Ling-repeat= "I in List">            <inputtype= "checkbox"Ng-model= "i.checked"Ng-change= "SelectOne ()">            <span>{{id}}</span>        </Li>    </ul></Div>

Angular

varApp = Angular.module (' myApp '), []); App.controller (' Mycontroller ', [' $scope ',function($scope) {$scope. List= [        {' ID ': 101},        {' ID ': 102},        {' ID ': 103},        {' ID ': 104},        {' ID ': 105},        {' ID ': 106},        {' ID ': 107}    ]; $scope. M= []; $scope. Checked= []; $scope. SelectAll=function () {        if($scope. Select_all) {$scope. Checked= []; Angular.foreach ($scope. List,function(i) {i.checked=true;            $scope. Checked.push (i.id); })        }Else{Angular.foreach ($scope. List,function(i) {i.checked=false; $scope. Checked= [];    })} console.log ($scope. checked);    }; $scope. SelectOne=function() {Angular.foreach ($scope. List,function(i) {varindex =$scope. Checked.indexof (i.id); if(i.checked && Index = = =-1) {$scope. Checked.push (i.id); } Else if(!i.checked && Index!==-1) {$scope. Checked.splice (Index,1);        }; })        if($scope. list.length = = =$scope. Checked.length) {$scope. Select_all=true; } Else{$scope. Select_all=false;    } console.log ($scope. checked); }}]);

Angularjs Full Select function implementation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.