如何根据模态选择更新输入

时间:2016-09-10 17:55:27

标签: javascript jquery twitter-bootstrap laravel

我目前正在使用Laravel Framework。我发送一个用户关于事件的所有记录,但是如果用户有多个事件记录,那么他需要选择他想要编辑的事件,我已经创建了显示所有事件的模态,但是如何获取所选事件并使用该特定事件填充所有输入? 这是我的代码

控制器

public function dashboardEvents(){                                          
//Brings all the info from the user events                              
$data['events'] = UserEvent::where('user_id', Auth::user()->id)->get(); 
//This is to know if there's more than one record
if(UserEvent::where('user_id', Auth::user()->id)->get()->count()>1) {   
    $data['multiple'] = true;                                           
}  else {                                                               
    $data['multiple'] = false;                                          
}                                                                       
return view('user-pages/my-events', $data);                             

}

查看

<title>User Dashboard</title>
<body  class="html not-front not-logged-in no-sidebars page-node page-

<!-- Al the Inputs -->
<select class="form-control" style="margin-bottom: 15px;" id="user_roles">
  <option value="bride">I am a bride</option>
  <option value="groom">Im a groom</option>
  <option value="groomsman">Im a guest</option>
  <option value="wedding_planner">Im a wedding planner</option>
</select>

<input type="text" class="form-control" style="margin-bottom: 15px;">
  <div class="input-group date" data-provide="datepicker" style="margin-bottom: 15px;">
    <input type="text" class="form-control">
      <div class="input-group-addon">
         <span class="glyphicon glyphicon-th"></span>
      </div>
</div>

<select class="form-control" style="margin-bottom: 15px;" id="party-type">
  <option id="wedding">Wedding</option>
  <option id="party">Party</option>
  <option id="prom">Prom</option>
</select>

<select class="form-control" style="margin-bottom: 15px;" id="user-task">
   <option>I am shopping for just Groomsmen</option>
   <option>I am shopping for me</option>
</select>

<input type="text" class="form-control" id="phone_number_user" style="margin-bottom: 15px;">

<input type="text" class="form-control" id="usr" style="margin-bottom: 15px;" placeholder="New Password">

模态

<div class="modal fade" id="UserDashboardModal" role="dialog">
  <div class="modal-dialog">
    <!-- Modal content-->
    <div class="modal-content">
        <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal">&times;</button>
            <h4 class="modal-title">Choose an Event to Edit</h4>
        </div>
        <div class="modal-body">
            <select class="form-control">
                @foreach($event as $key=>$cat)
                <option value="{{ $cat->user_event_id }}">{{ $cat->name }}</option>
                @endforeach
            </select>
        </div>
        <div class="modal-footer">
            <button type="button" class="btn btn-default" data-dismiss="modal" id="modalFromEventButton">Select</button>
        </div>
    </div>
</div>

打开模态的脚本

<script>
  $(document).ready(function () {
      $('#UserDashboardModal').modal('show');
  });
</script>

如何获得所选活动?并自动上传具有正确数据的输入?

1 个答案:

答案 0 :(得分:0)

我会稍微更改您的查询,因为现在您要查询 userEvents 两次。您可以从第一个结果集中获取计数:

<强>控制器:

public function dashboardEvents()
{                                          
    // Get all the event ids for the user.
    $data['events'] = UserEvent::where('user_id', Auth::user()->id)->get(); 
    // Check count.
    $data['multiple'] = $data['events']->count() > 1 ? true : false;
    // Return view.
    return view('user-pages/my-events', $data);
}

使用编辑表单的表单元素更新您的Blade,其中包含与您的模式中的select下方的userEvent ID相对应的ID。

<强>刀片:

<form action="/your/update/route" method="POST">
    <input type="hidden" name="_token" value="{{ csrf_token() }}">
    @foreach($event as $key => $cat)
        <div id="cat-{{ $cat->user_event_id }}" style="display:none">
            <!-- build your form elements for update -->
        </div>
    @endforeach
</form>

使用选择框事件更新您的脚本,您可以在其中决定向用户显示哪个表单。

<强>脚本:

$(document).ready(function () {

    $('#UserDashboardModal').modal('show');

    $('.modal-body select').change(function() {
        // Get selected userEventId.
        var userEventId = $('.modal-body select option:selected').val();
        // Change your form with $('#cat-' + userEventId)
        // code...
    });

});

最后注意事项:最好只查询id&amp;用于在仪表板上预先填充选择的名称(因为用户可能有很多 userEvents )。然后,当用户使用ajax选择事件时,在单独调用时获取完整数据。祝你好运!