2016-06-16 5 views
2

これは難しい質問のタイトルがある場合はお詫び申し上げます。私は、特定のケースで適切なデータバインディングを実行する方法を理解する助けが必要です。私はTimesheetDataというオブジェクトを持っています。このオブジェクトは、6/13/2016のような週である文字列を格納しています。また、weekDataという配列を持ち、idを持つオブジェクトを格納していて、毎週の曜日を処理しています。ng-repeatを使用した場合のAngularJSデータのバインド

私はng-repeatを使用して、データのオプションの週をドロップダウン選択に入力しますが、選択された値を日付としてどのように今週の選択の下にテーブルを作成するかはわかりません。

以下のコードでは、<td>タグのデータを、選択したweekOfと等しいオブジェクトに格納されているデータに対応するように更新する必要があります。私は、選択タグ内の選択された項目のインデックスを使用して、配列のテーブル内のデータをバインドすることが解決策であると考えています。

誰でも私にそのインデックスを取得する方法を教えてもらえますか?

dash.TimesheetData.WeekData[x].xyz ここで、xはselectタグの値であり、選択されたインデックスです。

参照については、以下のいくつかのコード:参照用

<h2>Week of {{dash.TimesheetData.WeekOf}}</h2> 
      <p> 
       Week 
       <select style="color:black;" data-ng-model="dash.TimesheetData.WeekOf"> 
        <option data-ng-repeat="options in dash.TimesheetData.WeekData" value="{{options.WeekOf}}">{{options.WeekOf}}</option> 
       </select> 
      </p> 
      <div class="table-responsive"> 
       <div class="table-hover table-striped"> 
        <style> 
         .table td { 
          text-align: center; 
         } 

         .table th { 
          text-align: center; 
         } 
        </style> 
        <table class="table"> 
         <thead> 
          <tr> 
           <th>ID</th> 
           <th>Monday</th> 
           <th>Tuesday</th> 
           <th>Wednesday</th> 
           <th>Thursday</th> 
           <th>Friday</th> 
           <th>Saturday</th> 
           <th>Sunday</th> 
          </tr> 
         </thead> 
         <tbody> 
          <tr> 
           <td>{{dash.TimesheetData.WeekData[0].ID}}</td> 
           <td>{{dash.TimesheetData.WeekData[0].Monday}}</td> 
           <td>{{dash.TimesheetData.WeekData[0].Tuesday}}</td> 
           <td>{{dash.TimesheetData.WeekData[0].Wednesday}}</td> 
           <td>{{dash.TimesheetData.WeekData[0].Thursday}}</td> 
           <td>{{dash.TimesheetData.WeekData[0].Friday}}</td> 
           <td>{{dash.TimesheetData.WeekData[0].Saturday}}</td> 
           <td>{{dash.TimesheetData.WeekData[0].Sunday}}</td> 
          </tr> 
         </tbody> 
        </table> 
       </div> 
      </div> 

タイムシートオブジェクト:

function WeekObject(ID, Mon, Tue, Wed, Thur, Fri,Sat,Sun, WeekOf) { 
    this.ID = ID; 
    this.Monday = Mon; 
    this.Tuesday = Tue; 
    this.Wednesday = Wed; 
    this.Thursday = Thur; 
    this.Friday = Fri; 
    this.Saturday = Sat; 
    this.Sunday = Sun; 
    this.WeekOf = WeekOf; 
}; 

this.TimesheetData = { 
    WeekOf: '', 
    WeekData: [ 
     new WeekObject(1,7,4,4,4,1,0,0, '6/13/2016'), 
     new WeekObject(2,5,6,2,8,1,2,0,'6/20/2016') 
    ], 
}; 
+0

「WeekObject」のプロパティを反復しようとしていますか? – 8protons

+0

私はテーブルにある週のデータを表示しようとしています。ドロップダウンリストで選択されている週にテーブルデータをバインドします。 –

答えて

2

また、ng-optionsディレクティブを参照して、選択したHTML要素をポピュレートすることをお勧めします。このリンクからわかるように、週をng-modelスコープ変数に限定することができます。

https://docs.angularjs.org/api/ng/directive/select

+0

私はこのアプローチが好きですが、今は問題があります。私はng-repeatからのオプション変数をデータと呼ばれる新しい変数にバインドしようとしています。しかし、オプションの属性全体を別の変数にバインドするのではなく、オプションの属性をバインドすることしかできません。 –

+0

ここに私のバインディングがあります。 いつ{{data}}の週をします。私は{{data.ID}}を実行したときにオブジェクトを見ることができます。何も得られません。 –

+0

、このいずれかを試してみてください <名前= "MySelectと" ID = "MySelectと" NG-オプション= "dash.TimesheetData.WeekDataでオプションのオプション" NG-モデルは= "データ" を選択>しかし、あなたはに設定する必要がありますdataはTimesheetData.WeekDataのデフォルト値です。 $ scope.data = TimesheetData.WeekData [0] – enricop89

1

まず、あなたの質問は非常に明確ではありません。文法のいくつかをきれいにする。第2に、ドロップダウンに応じてテーブル内のコンテンツを変更する関数を書くことができます。

私はng-changeディレクティブをドロップダウンで使用しました。 作業中のプランナーを見つけて、あなたの調査結果を回答してください。

<select style="color:black;" data-ng-model="TimesheetData.WeekOf" ng-change="changeTable()"> 
     <option ng-repeat="day in array" value="{{day}}">{{day}}</option> 

var app = angular.module('plunker', []); 
 

 
app.controller('MainCtrl', function($scope) { 
 
    $scope.array = ["mon", "tue", "wen", "thu"]; 
 

 
    function WeekObject(ID, Mon, Tue, Wed, Thur, Fri, Sat, Sun, WeekOf) { 
 
    this.ID = ID; 
 
    this.Monday = Mon; 
 
    this.Tuesday = Tue; 
 
    this.Wednesday = Wed; 
 
    this.Thursday = Thur; 
 
    this.Friday = Fri; 
 
    this.Saturday = Sat; 
 
    this.Sunday = Sun; 
 
    this.WeekOf = WeekOf; 
 
    } 
 

 
    $scope.TimesheetData = { 
 
    WeekData: [ 
 
     new WeekObject(1, 7, 4, 4, 4, 1, 0, 0, '6/13/2016'), 
 
     new WeekObject(2, 5, 6, 2, 8, 1, 2, 0, '6/20/2016'), 
 
     new WeekObject(3, 5, 6, 2, 8, 1, 2, 0, '6/20/2016'), 
 
     new WeekObject(4, 5, 6, 2, 8, 1, 2, 0, '6/20/2016') 
 
    ], 
 
    }; 
 

 
    $scope.changeTable = function(index) { 
 
    for (var i = 0; i < $scope.array.length; i++) { 
 
     var test = $scope.array[i]; 
 
     if (test === $scope.TimesheetData.WeekOf) { 
 
     $scope.row = $scope.TimesheetData.WeekData[i]; 
 
     } 
 
    } 
 
    } 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.22/angular.min.js"></script> 
 
<!DOCTYPE html> 
 
<html ng-app="plunker"> 
 

 
    <head> 
 
    <meta charset="utf-8" /> 
 
    <title>AngularJS Plunker</title> 
 
    <script>document.write('<base href="' + document.location + '" />');</script> 
 
    <link rel="stylesheet" href="style.css" /> 
 
    <script data-require="[email protected]" src="https://code.angularjs.org/1.3.20/angular.js" data-semver="1.3.20"></script> 
 
    <script src="app.js"></script> 
 
    </head> 
 

 
    <body ng-controller="MainCtrl"> 
 
    <h2>Week of {{TimesheetData.WeekOf}}</h2> 
 
      <p> 
 
       Week 
 
       <select style="color:black;" data-ng-model="TimesheetData.WeekOf" ng-change="changeTable()"> 
 
        <option ng-repeat="day in array" value="{{day}}">{{day}}</option> 
 
       </select> 
 
      </p> 
 
      <div class="table-responsive"> 
 
       <div class="table-hover table-striped"> 
 
        <style> 
 
         .table td { 
 
          text-align: center; 
 
         } 
 

 
         .table th { 
 
          text-align: center; 
 
         } 
 
        </style> 
 
        <table class="table"> 
 
         <thead> 
 
          <tr> 
 
           <th>ID</th> 
 
           <th>Monday</th> 
 
           <th>Tuesday</th> 
 
           <th>Wednesday</th> 
 
           <th>Thursday</th> 
 
           <th>Friday</th> 
 
           <th>Saturday</th> 
 
           <th>Sunday</th> 
 
           <th>WeekOf</th> 
 
          </tr> 
 
         </thead> 
 
         <tbody> 
 
          <tr> 
 
           <th>{{row.ID}}</th> 
 
           <th>{{row.Monday}}</th> 
 
           <th>{{row.Tuesday}}</th> 
 
           <th>{{row.Wednesday}}</th> 
 
           <th>{{row.Thursday}}</th> 
 
           <th>{{row.Friday}}</th> 
 
           <th>{{row.Saturday}}</th> 
 
           <th>{{row.Sunday}}</th> 
 
           <th>{{row.WeekOf}}</th> 
 
          </tr> 
 
         </tbody> 
 
        </table> 
 
       </div> 
 
      </div> 
 
      <!--{{TimesheetData.WeekData[0].ID}}--> 
 
    </body> 
 

 
</html>

</select> 

$scope.changeTable = function(index){ 
for (var i=0; i<$scope.array.length;i++) 
{ 
    var test = $scope.array[i]; 
    if(test === $scope.TimesheetData.WeekOf) 
    { 
    $scope.row= $scope.TimesheetData.WeekData[i]; 
    } 
}} 

上記のコードを注意してください。

+0

ニースの答え。すばやく書きました。 – 8protons

+1

礼状ありがとうございました:) –

+0

私はこれを自分のコードに追加して、自分のコードの中で動作するようにしています。回答いただきありがとうございます、これは私がコードを実装するために5分ほどかかります。 –

関連する問題