2017-06-17 60 views
0

我有一張表,從數據庫中獲取數據比我需要一次更新所有行。在每個單元格內添加了輸入字段。現在我希望能夠在輸入數據時立即更新所有用戶,但我不知道如何。一次保存多個錶行(Laravel 4)

下面是我的視角的圖片:

View

@extends('admin/master') 
@section('content') 

<section class="content"> 
    {{Form::open()}} 
    <div class="row"> 
     <div class="col-xs-12"> 
      <div class="box"> 
       <div class="box-header"> 
        <div class="col-lg-4"> 
         <h3 class="box-title">INPUT EXAM RESULTS FOR EACH STUDENT: </h3> 
        </div> 
        <div class="pull-right col-lg-8"> 
         <div class="col-lg-2 col-lg-offset-5 pull-left"> 
          <select class="btn bg-navy" name="city" > 
           <option>Select City</option> 
           <option>Prishtin</option> 
           <option>Prizren</option> 
          </select> 
         </div> 
         <div class="col-lg-5 pull-right"> 
          <div class="input-group"> 
           <input type="text" name="search_input" class="form-control" placeholder="Search here..."> 
           <div class="input-group-btn"> 
            <button type="submit" class="btn btn-info"><i class="fa fa-search"></i> Search</button> 
           </div> 
          </div> 
         </div> 
        </div> 
       </div> 
       <div class="box-body"> 
        <?php if (isset($data)) { ?> 
        <table id="example2" class="table table-bordered table-hover"> 
          <thead> 
           <tr> 
            <th>{{Lang::get('messages.stid')}} </th> 
            <th>{{Lang::get('messages.name')}}</th> 
            <th>Subject 1</th> 
            <th>Subject 2</th> 
            <th>Subject 3</th> 
            <th>Subject 4</th> 
           </tr> 
          </thead> 
          <tbody>@foreach ($data as $row) 
           <tr> 
            <td>{{$row->id}}</td> 
            <td>{{$row->fname}} {{$row->lname}}</td> 
            <td><input type="text" name="sub1" class="form-control" placeholder="Add marks here..."></td> 
            <td><input type="text" name="sub2" class="form-control" placeholder="Add marks here..."></td> 
            <td><input type="text" name="sub3" class="form-control" placeholder="Add marks here..."></td> 
            <td><input type="text" name="sub4" class="form-control" placeholder="Add marks here..."></td> 
           </tr> 
           @endforeach 
          </tbody> 
         </table> 
        <button type="submit" class="col-lg-2 pull-right btn btn-success"><i class="fa fa-save"></i> Save</button> 
        <?php } ?> 
       </div> 
      </div> 
     </div> 
    </div> 
    {{Form::close()}} 
</section> 

{{ HTML::script('/admin/assets/js/jquery-2.2.3.min.js') }} 
{{ HTML::script('/admin/assets/js/bootstrap.min.js') }} 
{{ HTML::script('/admin/assets/js/jquery.dataTables.min.js') }} 
{{ HTML::script('/admin/assets/js/dataTables.bootstrap.min.js') }} 

<script> 
    $(function() { 
     $("#example1").DataTable(); 
     $('#example2').DataTable({ 
      "paging": true, 
      "lengthChange": false, 
      "searching": false, 
      "ordering": true, 
      "info": true, 
      "autoWidth": false 
     }); 
    }); 
</script> 

@stop 

//控制器

public function search_input(){ 
     $input = Input::get('search_input'); 
     $city = Input::get('city'); 
     $data = Apply::where('exam_venue', '=', "$input") 
       ->where('city_applied', '=', "$city")->get(); 
     if (isset($_POST['save'])) { 

     } 
     return View::make('admin/exam/edit',compact('data')); 
    } 
+0

通過一次更新所有行,我假設你的意思是什麼時候使用搜索輸入?如果是這種情況,您需要設置服務器端處理並通過ajax請求數據表數據。或者你是否每次提交表單並重新加載整個頁面? – btl

+0

是的。但我不知道如何做到這一點,因爲我認爲更好 –

+0

只要你知道,這個包在處理服務器端處理方面做得非常出色。我在很多項目中使用它:https://github.com/yajra/laravel-datatables – btl

回答

0

你輸入元素必須陣列,通過$row->id鍵控(我想這是學生表的主鍵)來確定該行代表哪個學生。例如:

<td><input type="text" name="sub1[{{ $row->id}}]" class="form-control" placeholder="Add marks here..."></td> 

然後當您提交數據時,通過數組鍵查找學生並相應地更新他們的信息。一些事情的效果:

$key = // extract id from sub1 array 
Student::find($request->input($key)); 
// do updates here 
// repeat for each sub input... 
+0

不知道如何繼續:$ id = Input :: get('id_record'); $ sub1 = Input :: get('sub1'); $ sub2 = Input :: get('sub2'); $ sub3 = Input :: get('sub3'); $ sub4 = Input :: get('sub4'); Student :: find($ request-> input($ key)); $ rezult = new Exam_result(); ($ i = 0; $ i <= count($ id)-1; $ i ++){ $ rezult-> subject1 = $ sub1; $ rezult-> subject2 = $ sub2; $ rezult-> subject3 = $ sub3; $ rezult-> subject4 = $ sub4; $ result-> save();} –

+0

我在視圖 –

+0

這個id name =「sub1 [{{$ row-> id}}] 'sub1')'將是一個數組。然後你可以循環foreach($ request-> input('sub1')爲$ id)...' –