我有一个项目,当用户点击一个按钮时,数据应该显示在一个表格上。但是,我使用的是此处所示的表格 http://i.stack.imgur.com/HW4rE.jpg .我想要的是,当用户点击注册表单上的添加按钮时,它应该显示在添加的主题表上,而不需要刷新页面。问题是我正在使用一个表,我无法使用 v-model 来绑定(bind)值。
这是我的 form.blade.php
报名表格
<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');
});
}
}
});
这是我的 ReservationController
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;
}
}
任何人都可以建议我如何解决这个问题,而不需要刷新页面。
顺便说一句,我的 console.log 中有这个错误
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
最佳答案
从服务器获取数据后,您应该使用 vue-devtools 检查 reservations
中实际包含哪些数据。
因为错误消息只是表示此数组中的对象没有 .subject
属性,这意味着您收到的数据可能没有您假设的结构,并且是找到您的解决方案的地方。
我不太了解 PHP 或 Laravel,但您的 GET 请求似乎从 index 方法获得响应,并且此方法似乎以主题数组而不是保留数组(应包含主题,根据您的模板代码)
关于laravel - 使用 vue js 和 laravel 添加表数据时获取未定义的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37429575/