最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Multi State button like toggle in angularJS - Stack Overflow

programmeradmin3浏览0评论

I want to implement a feature in table where user can set value of cell by clicking on it. there can be say 3-4 states,also a ng-model attached to it.

I looked for the toggle button in angularjs but they are mere on/off type.

In short; Clicking on the button will set the value as: Active, Inactive, Excluded Looking for solution with multiple state. Any help with this is really appreciated.

I want to implement a feature in table where user can set value of cell by clicking on it. there can be say 3-4 states,also a ng-model attached to it.

I looked for the toggle button in angularjs but they are mere on/off type.

In short; Clicking on the button will set the value as: Active, Inactive, Excluded Looking for solution with multiple state. Any help with this is really appreciated.

Share Improve this question asked Sep 3, 2013 at 12:27 SushrutSushrut 1,5511 gold badge12 silver badges16 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 8

Check the below working example :

http://jsfiddle/vishalvasani/ZavXw/9/

and controller code

function MyCtrl($scope) {

    $scope.btnarr=0;
    $scope.btnTxt=["Active","Inactive","Excluded"]
    $scope.change=function(){
        switch($scope.btnarr)
        {
            case 0:
                $scope.btnarr=1;
               break;
            case 1:
                 $scope.btnarr=2
               break;
            case 2:
                 $scope.btnarr=0;
               break;                
        }
    }
}

OR

Shorter Version of Controller

function MyCtrl($scope) {

    $scope.btnarr=0;
    $scope.btnTxt=["Active","Inactive","Excluded"]
    $scope.change=function(){
      $scope.btnarr = ($scope.btnarr + 1) % $scope.btnTxt.length;
    }
}

and HTML

<div ng-controller="MyCtrl">
    <button ng-modle="btnarr" ng-Click="change()">{{btnTxt[btnarr]}}</button>
</div>

There isn't much to it.

When I make menus in Angular, on each item, I'll have a "select" function, which then selects that particular object, out of the list...

Making an iterable button is even smoother:

var i = 0;
$scope.states[
    { text : "Active" },
    { text : "Inactive" },
    { text : "Excluded" }
];

$scope.currentState = $scope.states[i];

$scope.cycleState = function () {
    i = (i + 1) % $scope.states.length;
    $scope.currentState = $scope.states[i];
    // notify services here, et cetera
}


<button ng-click="cycleState">{{currentState.text}}</button>

The actual array of states wouldn't even need to be a part of the $scope here, if this was the only place you were using those objects -- the only object you'd need to have on the $scope would then be currentState, which you set when you call the cycleState method.

Here is a fiddle with two possibilities: selecting the state from a list or cycling by clicking the button itself.

http://jsfiddle/evzKV/4/

The JS code looks like this:

angular.module('test').directive('toggleValues',function(){
return {
    restrict: 'E',
    replace: true,
    template: '<div>Set Status:<div ng-repeat="value in values" class="status" ng-click="changeTo($index)">{{value}}</div><span ng-click="next()">Current Status (click to cycle): {{values[selectedValue]}}</span></div>',
    controller: ['$scope', '$element', function ($scope, $element) {
        $scope.values = ["Active", "Inactive", "Pending"];
        $scope.changeTo = function (index) {
            $scope.selectedValue = (index < $scope.values.length) ? index : 0;
        };

        $scope.next = function () {
            $scope.selectedValue = ($scope.selectedValue + 1) % $scope.values.length;
            // the modulo is stolen from Norguard (http://stackoverflow./a/18592722/2452446) - brilliant idea
        };

        $scope.selectedValue = 0;
    }]
};
});

HTML:

<div ng-app="test">
    <toggle-values></toggle-values>
</div>
发布评论

评论列表(0)

  1. 暂无评论