我想在表中实现一个功能,用户可以通过单击它来设置单元格的值。可以说有3-4个状态,也有一个纳克模型附在上面。
我在angularjs中寻找切换按钮,但它们只是开/关类型。
简而言之,单击按钮将值设置为: Active、Inactive、Excluded,以寻找具有多个状态的解决方案。对此的任何帮助都是非常感谢的。
发布于 2013-09-03 12:49:09
请检查下面的工作示例:
http://jsfiddle.net/vishalvasani/ZavXw/9/
和控制器代码
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;
}
}
}或
控制器的较短版本
function MyCtrl($scope) {
$scope.btnarr=0;
$scope.btnTxt=["Active","Inactive","Excluded"]
$scope.change=function(){
$scope.btnarr = ($scope.btnarr + 1) % $scope.btnTxt.length;
}
}和HTML
<div ng-controller="MyCtrl">
<button ng-modle="btnarr" ng-Click="change()">{{btnTxt[btnarr]}}</button>
</div>发布于 2013-09-03 12:47:30
没什么大不了的。
当我按角度制作菜单时,在每个项目上,我将有一个“选择”函数,然后从列表中选择特定的对象.
制作一个可迭代的按钮甚至更流畅:
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>实际的状态数组甚至不需要成为$scope的一部分,如果这里是您使用这些对象的唯一位置--那么在$scope上唯一需要拥有的对象就是currentState,您在调用cycleState方法时会设置它。
发布于 2013-09-03 13:14:01
这里有两种可能:从列表中选择状态,或者单击按钮本身来循环。
JS代码如下所示:
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>https://stackoverflow.com/questions/18592331
复制相似问题