【问题标题】:select table row value on checkbox click and display selected value on div in angularjs在复选框上选择表格行值单击并在 angularjs 中的 div 上显示所选值
【发布时间】:2016-12-12 04:11:42
【问题描述】:

我需要在复选框单击时选择表格中的一行并在 div 上显示选定的行值,因为我是 angularjs 的新手,无法弄清楚它是如何完成的。 我尝试使用过滤器选项,但没有工作,请协助任何感谢。

我的代码

     <table  ng-show="ad_tabl==1" id="data" border="1" class="table table-striped responsive-utilities jambo_table bulk_action"  >
    <thead>
            <tr class="headings">
            <th><input type="checkbox" class=""/></th>
            <th>Index X 10 table1</th>
            <th>Steps</th>
            <th>Distance(ft)</th>
            <th>Distance....(m)</th></th>
            </tr>
    </thead>

    <tbody>
        <tr    ng-repeat="ad1 in action_distance" >
            <td class="a-center ">
            <input  ng-model="ad1.index" type="checkbox" class="tableflat_ad"/>
            </td>
            <td>{{ad1.index}}</td>
            <td>{{ad1.steps}}</td>
            <td>{{ad1.distance_ft}}</td>
            <td>{{ad1.distance_m}}</td>

        </tr>

    </tbody>    
    </table>
    <div>
    ////////  show row selected value here ///////////
    </div>

var app =  angular.module('formExample', [])
    .controller('ExampleController', ['$scope', function($scope) {


         $scope.action_distance = [
{
index:"0",
steps:       "<=2 in.(5cm.)",
distance_ft:       "",
distance_m:       "",
},{
index:       "1",
steps:       "Within Reach",
distance_ft:       "",
distance_m:       "",
},{
index:       "3",
steps:       "1-2 Steps",
distance_ft:       "",
distance_m:       "",
}
];
}]);

提前致谢。

【问题讨论】:

    标签: html angularjs


    【解决方案1】:

        var app = angular.module('main', []);
                                app.controller('DemoCtrl', function ($scope,$filter) {
               
                              $scope.selectAll = function (a) {
        angular.forEach($scope.action_distance, function (obj) {
            obj.check = a;
          
        });
                                 $scope.onchange();
      };
                                  $scope.test = {};         
                                  $scope.onchange=function()   {                            
              $scope.test = $filter('filter')($scope.action_distance, { check: true }, true); 	}; 
                                 
                                  
                 $scope.action_distance = [
        {
        index:"0",
        steps:       "<=2 in.(5cm.)",
        distance_ft:       "",
        distance_m:       "",
        },{
        index:       "1",
        steps:       "Within Reach",
        distance_ft:       "",
        distance_m:       "",
        },{
        index:       "3",
        steps:       "1-2 Steps",
        distance_ft:       "",
        distance_m:       "",
        }
        ];
                                  
                            });
                             
        <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
        <div ng-app="main" ng-controller="DemoCtrl">
         <table   id="data" border="1" class="table table-striped responsive-utilities jambo_table bulk_action"  >
            <thead>
                    <tr class="headings">
                    <th><input type="checkbox" class="" ng-change="selectAll(master)" ng-model="master" ng-init="master=false;"/></th>
                    <th>Index X 10 table1</th>
                    <th>Steps</th>
                    <th>Distance(ft)</th>
                    <th>Distance....(m)</th></th>
                    </tr>
            </thead>
    
            <tbody>
                <tr    ng-repeat="ad1 in action_distance" >
                    <td class="a-center ">
                    <input type="checkbox" ng-change="onchange()" ng-model="ad1.check" />
                       
                    </td>
                    <td>{{ad1.index}}</td>
                    <td>{{ad1.steps}}</td>
                    <td>{{ad1.distance_ft}}</td>
                    <td>{{ad1.distance_m}}</td>
    
                </tr>
    
            </tbody>    
            </table>
            <div>
        <br/><br/>  
               <table border="1"    >
               <th>Index X 10 table1</th>
                <th>Steps</th>
                <th>Distance(ft)</th>
                <th>Distance....(m)</th></th>
                </tr>
               <tr    ng-repeat="ad1 in  test" >               
                <td>{{ad1.index}}</td>
                <td>{{ad1.steps}}</td>
                <td>{{ad1.distance_ft}}</td>
                <td>{{ad1.distance_m}}</td>
    
            </tr>
          </table>
            </div>
        </div>

    【讨论】:

    • 为什么我们需要使用 ng-repeat 创建表格,我们不能直接在 div 或标签上显示它
    • //在此处传递所需的内容,例如 {{ad1.steps}}
      或也使用标签
    • 嗨,我只能在复选框上选择一个行值吗?如果单击其他复选框,值会追加,我只想保留我上次单击的行值,
    • 你好写代码只选择一个复选框意味着如果选择了一个复选框,其他的自动将其设为check:false;
    • 是的,但通过以下方式管理它 $scope.onchange_ad1=function(index,description){ $('input.ad1_check').on('change', function() { $ ('input.ad1_check').not(this).prop('checked', false); });控制台.log(索引); $scope.index_val1=索引; $scope.desc_val1=描述; };
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-05-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-09
    • 1970-01-01
    相关资源
    最近更新 更多