如何在单击箭头时在表格中显示下拉列表的选定值

时间:2017-08-16 11:43:36

标签: html angularjs

在下面的代码中,它会在单击箭头时显示另一个面板中下拉列表中的选定值,并在单击删除箭头时将其推回到同一面板。如何在相同的代码而不是将数据推送到面板(选定),它可以将数据推送到面板内的表格?将新数据添加到新行后,每次单击箭头

<html>
<head>
  <title>Bootstrap Example</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.5/angular.js"></script>
  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.0rc1/angular-route.js"></script>
  <script src="https://code.angularjs.org/1.4.7/i18n/angular-locale_da.js"></script>
</head>

<body ng-app="app" ng-controller="MoveCtrl">

<div class="container">
  <div class="panel-group">
            <div class="panel panel-primary">
              <div class="panel-heading">Available</div>
              <div class="panel-body">
                    <select ng-model="availableList" multiple ng-options="x as x.name for x in available" style="width: 800px"></select> 
              </div>
            </div>

    <div ng-init="myVar = 'arrowBack.png'">
        <img ng-src="{{myVar}}" style="height: 100px" value= "add" ng-click="moveItem(availableList[0], available,selected)">


    <div ng-init="myVar = 'arrowForward.png'">
        <img ng-src="{{myVar}}" style="height: 100px" value= "remove" ng-click="moveItem(selectedList[0], selected,available)">
    </div>
</div>

            <div class="panel panel-primary">
              <div class="panel-heading">Selected</div>
              <div class="panel-body">
                <select ng-model="selectedList" multiple ng-options="x as x.name for x in selected" style="width: 800px"> </select> 
              </div>
            </div>

  </div>
</div>

<script>
angular.module('app', []).controller('MoveCtrl', function($scope) {


    $scope.moveItem = function(item, from, to) {

    console.log('Move item   Item: '+item+' From:: '+from+' To:: '+to);

        var idx=from.indexOf(item);
        if (idx != -1) {
            from.splice(idx, 1);
            to.push(item);      
        }
    };

 $scope.selected= [];                                

    $scope.available = [
      {
        id: 1, 
        name: 'bar'
      }, 
      {
        id: 2, 
        name: 'foo'
      },
      {
        id: 3,
        name: 'goo'
      }
    ];
  });  
</script>

</body>
</html>

它的工作方式与http://jsfiddle.net/f1pnw8cq/类似。但是我需要使用表格来代替选定的面板,其中数据将从可用面板中推送。

1 个答案:

答案 0 :(得分:1)

updated fiddle

您需要将select替换为HTML中的table

<table>
   <tr ng-repeat="x in selected">
      <td>{{x.name}}</td>
   </tr>
</table>

这将在第二个面板中添加table