【问题标题】:Is there an easier way of passing data from a page to a modal in Laravel?有没有更简单的方法将数据从页面传递到 Laravel 中的模式?
【发布时间】:2017-01-03 13:42:31
【问题描述】:

我使用 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>

如您所见,表格中每个条目的最后一列中显示的按钮代码占据了三行,因为我还使用数据属性存储书籍的属性。这样我就可以将书的数据传递给按钮将召唤的模式。这是将被召唤的模式:

<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 进行数据库查找,以便获取视图所需的所有数据以在模式上显示它们?

【问题讨论】:

  • JSON 怎么样?将图书数据的 JSON 表示形式保存在一个 data- 属性中,然后从那里读取并解析 JSON JSON.parse(...)
  • 哦,好吧。我不是 Javascript 方面的专家,所以我没有想到这一点。我会努力学习并应用你所说的。

标签: javascript php jquery twitter-bootstrap laravel


【解决方案1】:

这里有一个更简单的方法来解决这个问题,你只需将书籍的内容保存在一个由书籍 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)
   })
});

【讨论】:

  • 在我修正了一些错别字并将 book 变量设置为 appVariables.books[id][0] 后,您的方法奏效了。谢谢!
  • 您可以使用 $books->keyBy('id') 将解决方案更改为 appVariables.books[id](将删除[0] 结尾)
猜你喜欢
  • 1970-01-01
  • 2021-08-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-12-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多