在Laravel中是否有一种更简单的方式将数据从页面传递到模态?
Is there an easier way of passing data from a page to a modal in Laravel?
我使用Laravel和Bootstrap制作的网站有一个表,列出了用户在网站上发布的书籍。下面的代码显示了视图中的表格,给出了一个图书集合:
<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>
可以看到,出现在表中每个条目的最后一列中的按钮的代码有三行,因为我还使用data属性存储了一本书的属性。这样我就可以将书的数据传递给按钮将调用的模态。下面是将要调用的模态:
<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,所以我可以得到所有必要的数据,为视图显示在模态上?
这里有一个更简单的方法,您只需将图书的内容保存在一个按book 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)
})
});
相关文章:
- 如何通过所选索引(AngularJS)在模态弹出窗口中显示数据
- 将数据传递给由服务创建的Ionic模态
- 从模态卸载数据
- 模态中的数据关闭在单击时被调用两次
- 如何销毁数据表的首次初始化(模态内的数据表)
- 数据表和模态
- 使用角度传递的数据更新模态
- 从 Angular 模态服务中的模态主体模板返回数据
- 使用 JavaScript 将数据传递到模态
- 将数据从一个模态发送到另一个模态
- (Laravel)使用数据库中的数据动态打开模态
- 模态窗口yii2中的Ajax验证数据
- angularjs模态将数据传输到外部控制器的正确方法
- 在nodejs中无需重定向即可将数据发送到模态
- 如何在AngularJS点击事件中将数据传递到模态中
- Ajax 模态窗口可以访问父窗口 PHP 数据吗?
- 如何使用 bootsrap 将数据传递到模态
- 从模态 AngularJS 获取数据
- AngularJS ng-click:如何将行数据传递到模态弹出文本框
- 如何更改数据模态的位置