借助ajax-Laravel在Bootstrap Modal上动态加载数据


Load Data Dynamically on Bootstrap Modal with help of ajax - Laravel

页面:default.blade.php

你好,我想要的是,当我点击show按钮时,下面的模态应该打开,它应该由来自数据库和通过ajax的数据组成,所以请帮助我。非常感谢您的帮助。

<div class="container">
          <table class="table">
                      <thead>
                        <tr>
                          <th>#</th>
                          <th>Name</th>
                          <th width="300">Adress</th>
                          <th>Contact</th>
                          <th>Email</th>
                          <th>Phone</th>
                          <th>Show</th>
                          <th>Edit</th>
                          <th>Delete</th>
                        </tr>
                      </thead>
                      <tbody>

                     @foreach ($usertoshow as $us)
                      <tr>
                        <td>{{$us->u_id}}</td>
                        <td>{{$us->u_name}}</td>
                        <td>{{$us->u_add}}</td>
                        <td>{{$us->u_contact}}</td>
                        <td>{{$us->u_eml}}</td>
                        <td>{{$us->u_phn}}</td>
                        <td>
                          <button type="button" class="btn btn-xs btn-success" data-toggle="modal" data-target="#clientInfo" id="showButton"><span class="glyphicon glyphicon-th"></span> **Show**</button>
                        </td>
                        <td>
                          <button type="button" class="btn btn-xs btn-info"><span class="glyphicon glyphicon-pencil"></span> Edit</button>
                        </td>
                        <td>
                          <button type="button" class="btn btn-xs btn-danger"><span class="glyphicon glyphicon-remove-sign"></span> Delete</button>
                        </td>
                      </tr>
                      @endforeach
                      </tbody>
                </table>
          </div>

模态代码为

<div class="modal fade" id="clientInfo" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
      <div class="modal-dialog">
        <div class="modal-content">
          <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
            <h4 class="modal-title" id="myModalLabel">Modal title</h4>
          </div>
          <div class="modal-body">
           <div class="alert alert-info" role="alert">
            Name: <strong></strong> 
          </div>
          </div>
          <div class="modal-footer">
            <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
          </div>
        </div>
      </div>
    </div>

控制器代码

<?php namespace App'Http'Controllers;
use App'Http'Requests;
use App'Http'Controllers'Controller;
use Illuminate'Http'Request;
use App'User;
use View,
    Response,
    Validator,
    Input,
    Mail,
    Session;

class UserController extends Controller {
    /**
     * Display a listing of the resource.
     *
     * @return Response
     */
    public function index()
    {
        $usertoshow = User::all();
        return view('pages.default')->with('usertoshow',$usertoshow);
    }
    /**
     * Show the form for creating a new resource.
     *
     * @return Response
     */
    public function insert()
    {
        $user = User::create(['u_name' => Input::get('inputName'), 'u_eml' => Input::get('inputMail'), 'u_srname' => Input::get('inputsurName'),'u_add' => Input::get('inputAdd'),'u_phn' => Input::get('inputPhone'),'u_contact' => Input::get('inputContact')]);
    }
    /**
     * Store a newly created resource in storage.
     *
     * @return Response
     */
    public function showdetail()
    {

    }
    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return Response
     */
    public function show($id)
    {
        //
    }
    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return Response
     */
    public function edit($id)
    {
        //
    }
    /**
     * Update the specified resource in storage.
     *
     * @param  int  $id
     * @return Response
     */
    public function update($id)
    {
        //
    }
    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return Response
     */
    public function destroy($id)
    {
        //
    }
}

您需要为每个"show"添加一个模态就在你的@foreach添加(包括一个文件来做模态代码)之前

@include ('layouts.modals.genModal' , ['record' => $us])

在genModal中
像这样的东西:

<div class="modal fade"
  id="show-form{{{ $record->id }}}" tabindex="-1" role="dialog" 
  aria-labelledby="myModalLabel" area-hidden="true"
  style="display: none;" >

模态物质的res

并将您的数据traget更改为相同的us id

<button type="button" class="btn btn-xs btn-success" 
  data-toggle="modal" data-target="#clientInfo" 
  id="showButton">
  <span class="glyphicon glyphicon-th"></span> **Show**
</button>
data-target="#show-form{{{ $record->id }}}"

适用于显示/编辑/删除,如果有人有解决方案,请告知,验证并不是那么容易。