How do I change the appearance of the selected button in a row of Material Design buttons?

穿精又带淫゛_ 提交于 2020-01-05 02:46:07

问题


I have a HTML table and in several rows I have a Material Design button in each cell, dynamically generated using AngularJS.

<table class="plans">
    <tbody>
        <tr>
            <td>Work Plans</td>
            <td ng-click="pbmainController.selectPlan(plan.planName)"
                ng-repeat="plan in pbmainController.planList">
                    <md-button class="md-raised">{{plan.planName}}</md-button>
            </td>
        </tr>
    </tbody>
</table>

Only one of button can be selected at a time (i.e. like a radio button).

How do I change the color of the selected button?


回答1:


I got it to work by putting the ng-class on the button, using the ternary operator

<table class="plans">
    <tbody>
        <tr>
            <td>Work Plans</td>
            <td ng-click="pbmainController.selectPlan(plan.planName)"
                ng-repeat="plan in pbmainController.planList">
                    <md-button ng-class="plan.planName == selectedPlanName ? 'md-raised md-primary' : 'md-raised'">{{plan.planName}}</md-button>
            </td>
        </tr>
    </tbody>
</table>



回答2:


A ng-class attribute will do the trick.

In your function selectPlan you probably save the name somewhere. Assuming this var is named selectedPlan, you can do something like this :

<table class="plans">
    <tbody>
        <tr>
            <td>Work Plans</td>
            <td ng-click="pbmainController.selectPlan(plan.planName)"                   
                ng-repeat="plan in pbmainController.planList">
                    <md-button  ng-class="{'active' : plan.planName == selectedPlan}" class="md-raised">{{plan.planName}}</md-button>
            </td>
        </tr>
    </tbody>
</table>

ng-class will be apply your class (here active) only when the condition is true. So only when your plan is the selected one.



来源:https://stackoverflow.com/questions/32269802/how-do-i-change-the-appearance-of-the-selected-button-in-a-row-of-material-desig

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!