我使用 Laravel 和 Bootstrap 制作的网站有一个表格,其中列出了用户在网站上发布的书籍。以下是在给定书籍集合的情况下在 View 中显示表格的代码:
<table class="table table-hover" id="table">
<thead>
<tr>
<th class="col-md-3">Title</th>
<th class="col-md-3">Author</th>
<th class="col-md-2">Price</th>
<th class="col-md-2">Courses</th>
<th class="col-md-2">Date Added</th>
<th class="col-md-2">More Info</th>
</tr>
</thead>
<tbody class="searchable">
@foreach($books as $book)
<tr>
<td>{{$book->title}}</td>
<td>{{$book->author}}</td>
<td>${{$book->price}}</td>
<td>{{$book->course_code}}</td>
<td>{{$book->added_on}}</td>
<td><button type="button" data-toggle="modal" data-id="{{$book->id}}" data-title="{{$book->title}}" data-author="{{$book->author}}"
data-price="{{$book->price}}" data-school="{{$book->school}}" data-description="{{$book->description}}" data-courses="{{$book->course_code}}"
data-date="{{$book->added_on}}" href="#more_info" class="btn btn-primary">More Info</button></td>
</tr>
@endforeach
</tbody>
</table>
正如您所看到的,表中每个条目的最后一列中出现的按钮的代码分为三行,因为我还使用数据属性来存储书籍的属性。这样我就可以将书籍的数据传递给按钮将调用的模式。这是将被调用的模态:
<div class = "modal fade" id = "more_info" role = "dialog">
<div class = "modal-dialog">
<div class = "modal-content">
<div class = "modal-header">
<h4 id="book_title">Title</h4>
<h5 id="book_author">Author</h5>
</div>
<div class = "modal-body">
<p id="book_price">Price: </p>
<p id="book_school">School: </p>
<p id="book_course_code">Course(s): </p>
<p id="book_date">Added On: </p>
<p>Description:<br></p>
<p id="book_description">Description<br></p>
</div>
<div class = "modal-footer">
<a class = "btn btn-default" data-dismiss = "modal">Close</a>
<input type="hidden" name="book_id" id="book_id" value=""/>
<button class = "btn btn-primary" data-dismiss = "modal" href="#contact_seller" data-toggle="modal">Contact Seller</button>
</div>
</div>
</div>
</div>
以下是将书籍数据从页面传递到模式的 Javascript 代码:
$(document).ready(function () {
$('#more_info').on('show.bs.modal', function (event) {
var button = $(event.relatedTarget) // Button that triggered the modal
var id = button.data('id') // Extract info from data-* attributes
var title = button.data('title')
var author = button.data('author')
var price = button.data('price')
var school = button.data('school')
var description = button.data('description')
var courses = button.data('courses')
var date = button.data('date')
var modal = $(this)
modal.find('#book_title').text(title)
modal.find('#book_author').text("By " + author)
modal.find('#book_price').text("Price: $" + price)
modal.find('#book_school').text("School: " + school)
modal.find('#book_course_code').text("Course(s): " + courses)
modal.find('#book_date').text("Added On: " + date)
modal.find('#book_description').text(description)
modal.find('#book_idx').text(id)
})
});
是否有更优雅的方式将数据从页面传递到模式?
我是否可以使用给定的图书 ID 进行数据库查找,以便获取 View 在模态上显示它们所需的所有数据?
最佳答案
这里有一个更简单的解决问题的方法,您只需将书籍的内容保存在由书籍 id 分组的变量中,该变量将成为您在按钮中设置的唯一属性。
<tbody class="searchable">
@foreach($books as $book)
<tr>
<td>{{$book->title}}</td>
<td>{{$book->author}}</td>
<td>${{$book->price}}</td>
<td>{{$book->course_code}}</td>
<td>{{$book->added_on}}</td>
<td><button type="button" data-toggle="modal" data-id="{{$book->id}}" href="#more_info" class="btn btn-primary">More Info</button></td>
</tr>
@endforeach
</tbody>
在表格结束下方但在 jquery 代码之前创建以下脚本标记,假设 $books 是一个集合实例(在 Laravel 5.x 中)
<script>
var appVairiables = {};
appVariables.books = {!! $books->groupBy('id') !!};
//output result will be like
// books = {
// 1 : {
// id : 1,
// author : "author1"
// ..
// },
// 3 : { .. }, 4 : { ... }
//}
</script>
现在在 jquery 函数中执行以下操作
$(document).ready(function () {
$('#more_info').on('show.bs.modal', function (event) {
var button = $(event.relatedTarget) // Button that triggered the modal
var id = button.data('id') // Extract info from data-* attributes
var book = appVariables.books[id]; //fetching the contents of book according to id received
var modal = $(this)
modal.find('#book_title').text(book.title)
modal.find('#book_author').text("By " + book.author)
modal.find('#book_price').text("Price: $" + book.price)
modal.find('#book_school').text("School: " + book.school)
modal.find('#book_course_code').text("Course(s): " + book.courses)
modal.find('#book_date').text("Added On: " + book.added_on)
modal.find('#book_description').text(book.description)
modal.find('#book_idx').text(book.id)
})
});
关于javascript - 在 Laravel 中是否有更简单的方法将数据从页面传递到模式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39173832/