Bootstrap模式用于编辑数据

时间:2014-07-02 11:08:44

标签: jquery ajax twitter-bootstrap laravel modal-dialog

页面:listUsers.php

我有一个按钮

<button data-user-id="5" class="btn btn-info permissions" data-toggle="modal" data-target="#myModal" type="button">Edit</button>

触发下面的bootstrap模式。

<div class="modal fade" id="myModal" 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"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
        <h4 class="modal-title" id="myModalLabel">Manage Permissions</h4>
      </div>
      <div class="modal-body">
         <form id="permissions" class="form-horizontal" role="form" method="post">
             <div class="form-group">
                  <div class="col-md-3">
                   <label for="email">Email:</label> 
                   <input class="form-control" name="email" type="text" id="email">        
                 </div>
             </div>
            <div class="form-group">
               <div class="col-md-3">
                  <label for="phone">Phone:</label>
                    <input class="form-control" name="phone" type="text" id="phone">
                </div>
             </div>
         </form>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
        <button type="submit" class="btn btn-primary">Save</button>
      </div>
    </div>
  </div>
</div>

我希望使用特定的用户数据填充bootstrap模式表单,以便我可以编辑它。单击编辑时,我使用jQuery get将控件传递给控制器​​,该控制器将获取特定用户。

$(".permissions").click(function(event){
    var userid = $(this).data('user-id'));
    $.get("somecontroller/getUser", {userid: userid, status: status}, function(response){
        // to do....
         // how to populate the form.
    }); 
    return false;
});

控制器方法是

public function getUser($id)
    {
        $user = UserModel::find($id);
        if (is_null($user))
        {
            return Redirect::route('users.index');
        }
        return View::make('users.listUsers', compact('user'));
      // return which view? Do I need to create a new view?
      // I want the query result to be available to the modal form in listUsers.php which gets triggered after clicking the edit button in listUsers.php
    }

1 个答案:

答案 0 :(得分:0)

不是使用View :: make返回视图,而是返回您想要的数据,即用户;

$user->toArray();

然后你可以在你的javascript中使用你的响应变量。

$('#email').value(response.email);