2014-11-10 2 views
1

내 결과가 모두 만들어지고 편집되고 저장되는 곳에서 Laravel PHP 응용 프로그램을 실행 중입니다. 이제 사용자가 범주를 선택할 수있는 드롭 다운 상자가있는 기능을 추가하고 사용자가 드롭 다운 상자에서 선택하는 내용에 따라 결과가 홈 페이지에 표시됩니다.Laravel PHP : 결과를 표시하기 위해 드롭 다운 상자 사용

이 작업을 수행하는 방법에 대한 몇 가지 문서를 찾으려고했지만 가능한 유일한 해결책은 jQuery와 함께 AJAX를 사용하여 결과를 업데이트하는 것입니다. 나는 이것을하기의 더 쉬운, 우아한 방법이 있는지 생각해보고있다.

보기 :

@section('content') 

/* New section I recently added for including the dropdown box below */ 

<div class="row"> 
    <div class="panel panel-default"> 
     <div class="panel-heading clearfix"> 
      <b>{{'Select a category' }}</b> 
     </div> 
     <div class="panel-body">  
      <dl class="dl-horizontal"> 
       <div class="table-responsive"> 
        <table class="table"> 
         <tr> 
          <td> 
          {{Form::open(array('route' => 'select_vids_pics', 'files' => true)) }}  

           <div class="form-group"> 
        /* Form Code goes here, 'label' and 'select options' */ 

        <div class="form-group"> 
    {{ Form::submit('Select Category', array('class' => 'btn btn-primary')) }} 
</div> 

{{ Form::close() }} 

</div> 

          </td> 
         </tr>  


        </table> 
       </div>    
      </dl>  


     </div>  

    </div> 
</div>  


/* Blocks of code below for currently display all results */ 

<div class="row"> 
    <div class="panel panel-default"> 
     <div class="panel-heading clearfix"> 
      <b>{{'Overview of Videos' }}</b> 
     </div> 
     <div class="panel-body"> 
      @if ($allvids->count()) 
       <dl class="dl-horizontal"> 
        <!-- Add this div class to make the table responsive --> 
        <div class = "table-responsive"> 
         <table class="table"> 
          <tr> 
           <th>ID</th> 
           <th>Name</th> 
           <th>Description</th> 
           <th>Edge</th> 
           <th>Stone</th> 
           <th>Category</th> 
           <th>Project</th> 
           <th>Update Video</th> 
          </tr>   
        @foreach($allvids as $video) 
        <tr> 
         <td>{{ $video->video_id }}</b></td> 
         <td>{{ $video->video_name }}</td> 
         <td>{{ $video->video_description }}</td> 
         <td>{{ $video->video_edges }} </td> 
         <td>{{ $video->video_stones }}</td> 
         <td>{{ $video->category }}</td> 
         <td>{{ $video->video_project }}</td> 

         <td><b>{{ link_to_route("show_video", 'Edit', array($video->video_id)) }}</b></td> 
        </tr>  
        @endforeach 
        {{ $allvids-> links()}} 
         </table> 
        </div>  


       </dl> 
      @else 
       <b>{{ 'All Pics Vids' }}</b> 
      @endif 
     </div> 
    </div> 
</div> 

<div class="row"> 
    <div class="panel panel-default"> 
     <div class="panel-heading clearfix"> 
      <b>{{'Overview of Pictures' }}</b> 
     </div> 
     <div class="panel-body"> 
      @if ($allpics->count()) 
       <dl class="dl-horizontal"> 
        <!-- Add this div class to make the table responsive --> 
        <div class = "table-responsive"> 
         <table class="table"> 
          <tr> 
           <th>ID</th> 
           <th>Name</th> 
           <th>Description</th> 
           <th>Edge</th> 
           <th>Stone</th> 
           <th>Category</th> 
           <th>Project</th> 
           <th>Update Picture</th> 
          </tr>   
        @foreach($allpics as $picture) 
        <tr> 
         <td>{{ $picture->picture_id }}</b></td> 
         <td>{{ $picture->picture_name }}</td> 
         <td>{{ $picture->picture_description }}</td> 
         <td>{{ $picture->picture_edges }} </td> 
         <td>{{ $picture->picture_stones }}</td> 
         <td>{{ $picture->category }}</td> 
         <td>{{ $picture->picture_project }}</td> 

         <td><b>{{ link_to_route("show_picture", 'Edit', array($picture->picture_id)) }}</b></td> 
        </tr>  
        @endforeach 
        {{ $allpics-> links()}} 
         </table> 
        </div>  


       </dl> 
      @else 
       <b>{{ 'All Pics Vids' }}</b> 
      @endif 
     </div> 
    </div> 
</div> 



@stop 

컨트롤러 :로부터

1) 수락 입력 :

/* This is the only function that is being used in the view above */ 

public function index() 
{ 
    $allvids = Video::paginate(10); 
    $allpics = Picture::paginate(10); 
    $this->layout->content = \View::make('home.pics_vids_overview', array('allvids' => $allvids, 'allpics' => $allpics)); 
} 

내가 내 컨트롤러에 다른 함수/메소드를 추가해야합니까 드롭 다운 상자에서 사용자가 선택한 2) 해당 입력을 새롭게 정의 된 함수를 찾아서 그 결과에 따라 결과를 표시 하시겠습니까?

위의 방법을 사용하면 함수가 다음과 같이 보일까요?

public function pics_vids_categories() 
{ 
    $input = \Input::all(); 

    $videos = DB::table('videos')->where('category', '=', $input)->get(); 
    $pictures = DB::table('pictures')->where('category', '=', $input)->get(); 

    var_dump($videos); 
    var_dump($pictures); 

} 

내가 말했듯이, 나는 이것을하는 더 예쁜 방법이 있기를 바라고 있습니다.

+0

저는 완전히 대답하기에 Laravel에 익숙하지는 않지만 Zend Framework에서는 제가 교체 한 부분만을 렌더링하는 뷰 스크립트를 가지고있을 것이고 Ajax 요청에 엘리먼트로 반환 될 것입니다. JSON 문자열. – Sonny

답변

1

당신은 이런 식으로 index 방법을 변경할 수 있습니다 : 사용자가 카테고리를 선택하고 양식을 제출하면

public function index() 
{ 
    $vdo = Video::query(); 
    $pic = Picture::query(); 
    if($category = Input::get('category')) { 
     $vdo->where('category', $category); 
     $pic->where('category', $category); 
    } 
    $allvids = $vdo->paginate(10); 
    $allpics = $oic->paginate(10); 
    $data = compact('allvids','allpics'); 
    $this->layout->content = \View::make('home.pics_vids_overview', $data); 
} 

지금 다음 where('category', $category)는 결과를 필터링합니다; 그렇지 않으면 모든 레코드/모델이 반환됩니다.

+0

이 솔루션은 완벽하게 작동합니다! 그것의 우아하고 매우 효과적인! 사용자가 카테고리를 선택한 후 반환 된 결과로 페이지가 업데이트 된 후 드롭 다운 상자가 선택한 카테고리에 남아있는 경우 (예 : 사용자가 드롭 다운 상자에서 'Option2'카테고리를 선택하고 페이지가 반환 될 때) 결과는 'Option2'에서 드롭 다운을 계속 선택해야합니다. –

+0

'{{Form :: select ('category', $ categories, Input :: old ('categories', $ category))}}'를 사용해야합니다. –

+0

지금 내 양식은 "{{Form :: select ('category', array ('Project1'=> 'Project1', 'Project2'=> 'Project2', 'Project3'=> 'Project3' 'category4'=> 'Project4'), Input :: old ('categories', $ category), array ('class'=> 'form-control'))}} 정의되지 않았습니다. 내보기 에서이 양식을 사용하고 있습니다. 대신 '양식 :: 모델'로 변경되도록 내 '양식 :: 열기'메서드를 redfine 할 필요가 있습니까? –

관련 문제