2016-05-25 9 views
2

ユーザーがボタンをクリックしたときにデータがテーブルに表示されるというこのプロジェクトがあります。しかし、私はhttp://i.stack.imgur.com/HW4rE.jpgのようにテーブルを使用しています。私が望むのは、ユーザーが登録フォームテーブルの追加ボタンをクリックすると、ページを更新する必要なく、追加されたサブジェクトテーブルに表示されるはずです。問題は、私がテーブルを使用していることと、値をバインドするためにvモデルを使用できないことです。vue jsとlaravelを使用してテーブルデータを追加するときに未定義の値を取得する

だからここ

<table class="table table-bordered"> 
    <tr> 
    <th>Section</th> 
    <th>Subject Code</th> 
    <th>Descriptive Title</th> 
    <th>Schedule</th> 
    <th>No. of Units</th> 
    <th>Room</th> 
    <th>Action</th> 
    </tr> 
    <body> 
    <input type="hidden" name="student_id" value="{{ $student_id }}"> 
    @foreach($subjects as $subject) 
     @foreach($subject->sections as $section) 
     <tr> 
     <td>{{ $section->section_code }}</td> 
     <td>{{ $subject->subject_code }}</td> 
     <td>{{ $subject->subject_description }}</td> 
     <td>{{ $section->pivot->schedule }}</td> 
     <td>{{ $subject->units }}</td> 
     <td>{{ $section->pivot->room_no }}</td> 
     <td> 
      <button 
       v-on:click="addSubject({{ $section->pivot->id}}, {{ $student_id}})" 
       class="btn btn-xs btn-primary">Add 
      </button> 

      <button class="btn btn-xs btn-info">Edit</button> 
     </td> 
     </tr> 
     @endforeach 
    @endforeach 
    </body> 
</table> 

AddedSubjects表

<table class="table table-bordered">  
    <tr> 
     <th>Section Code</th> 
     <th>Subject Code</th> 
     <th>Descriptive Title</th> 
     <th>Schedule</th> 
     <th>No. of Units</th> 
     <th>Room</th> 
     <th>Action</th> 
    </tr> 

    <body> 

    <tr v-for="reservation in reservations"> 
     <td>@{{ reservation.section.section_code }}</td> 
     <td>@{{ reservation.subject.subject_code }}</td> 
     <td>@{{ reservation.subject.subject_description }}</td> 
     <td>@{{ reservation.schedule }}</td> 
     <td>@{{ reservation.subject.units }}</td> 
     <td>@{{ reservation.room_no }}</td> 
     <td> 
     <button class="btn btn-xs btn-danger">Delete</button> 
     </td> 
    </tr> 

    </body> 
</table> 

そして、ここでは私のall.jsだ

new Vue({ 
el: '#app-layout', 

data: { 

    reservations: [] 

}, 
ready: function(){ 
    this.fetchSubjects(); 
}, 
methods:{ 

    fetchSubjects: function(){ 
     this.$http({ 
      url: 'http://localhost:8000/reservation', 
      method: 'GET' 
     }).then(function (reservations){ 
      this.$set('reservations', reservations.data); 
      console.log('success'); 
     }, function (response){ 
      console.log('failed'); 
     }); 
    }, 

    addSubject: function(section,subject,student_id){ 
     var self = this; 
     this.$http({ 
      url: 'http://localhost:8000/reservation', 
      data: { sectionSubjectId: section.pivot.id, studentId: student_id }, 
      method: 'POST' 
     }).then(function(response) { 
      self.$set('reservations', response.data); 
      console.log(response); 
      self.reservations.push(response.data); 
     },function (response){ 
      console.log('failed'); 
     }); 
    } 
    } 
}); 

はここに私のマイform.blade.php

登録フォームテーブルです予約C ontroller

class ReservationController extends Controller 
{ 

public function index() 
{ 
    $student = Student::with(['sectionSubjects','sectionSubjects.section', 'sectionSubjects.subject'])->find(1); 

    return $student->sectionSubjects;  

} 

public function create($id) 
{ 
    $student_id = $id; 

    $subjects = Subject::with('sections')->get(); 

    return view('reservation.form',compact('subjects','student_id')); 
} 

public function store(Request $request) 
{ 

    $subject = SectionSubject::findOrFail($request->sectionSubjectId); 

    $student = Student::with(['sectionSubjects','sectionSubjects.section', 'sectionSubjects.subject'])->findOrFail($request->studentId); 

    $subject->assignStudents($student); 

    return $student->sectionSubjects; 

    } 
} 

この問題を解決する方法については、ページを更新する必要はありません。私はあなたがそれがサーバーからフェッチされた後にreservationsの内側に実際にどのようなデータ検査するVUE-デベロッパーツールを使用する必要があります

Error when evaluating expression "reservation.section.section_code": TypeError: Cannot read property 'section_code' of undefined 

Error when evaluating expression "reservation.subject.subject_code": TypeError: Cannot read property 'subject_code' of undefined 

Error when evaluating expression "reservation.subject.subject_description": TypeError: Cannot read property  'subject_description' of undefined 

Error when evaluating expression "reservation.subject.units": TypeError: Cannot read property 'units' of undefined 
+0

実際のエラーメッセージとその発生場所を記入してください。 –

+0

@LinusBorg親切に更新されたコードを参照してください。ありがとうございました。 – Jearson

+0

@Linus Borgコードを更新しました。 – Jearson

答えて

0

私はconsole.logでこのエラーを持っているところで

エラーメッセージは単にこの配列のオブジェクトに.subjectプロパティがないことを意味します。つまり、受信したデータにはおそらく想定している構造がなく、それがソルーションが見つかる場所です。

PHPやLaravelについてよくわかりませんが、GETリクエストがインデックスメソッドから応答を受け取っているようですが、このメソッドは予約の配列ではなく、あなたのテンプレートコードに従って科目)

+0

私はvue-devtoolsを使いました。私はこの結果を得ました。 http://imgur.com/FUxWd2f response.dataを予約にプッシュするとき、フォーマットは配列であることが分かります。このリンクでわかるように、2:Arrayはオブジェクトではありません。これをどうすれば解決できますか? – Jearson

関連する問題