4

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.

3 Answers 3

8

Check the below working example :

http://jsfiddle.net/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>
Sign up to request clarification or add additional context in comments.

Comments

3

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.

Comments

2

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

http://jsfiddle.net/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.com/a/18592722/2452446) - brilliant idea
        };

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

HTML:

<div ng-app="test">
    <toggle-values></toggle-values>
</div>

1 Comment

Well shucks. It's not really a new idea, but I'll take it. The other really clean way to mod these types of things is in two statements: i += 1; i %= length; From a micro-op perspective there's no real difference. But it can look really clean.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.