2017-08-22 6 views
0

私はユーザのテーブルを持っています。私はそれらを 'グループ'でフィルタリングできる必要があります。ユーザーがドロップダウンリストからオプションを選択したのでとき、私は、ページ全体だけユーザーテーブルは、そのグループからリロードして表示ユーザーしないようにしたい選択変更後のsymfonyリロードテーブル

これは、ユーザーテーブル

Filter by group 
    <select class="filter-users"> 
     <option selected></option> 
     {% for group in groups %} 
       <option>{{ group.name }}</option> 
     {% endfor %} 
    </select> 
    <table id="dataTable"> 
     <tbody> 
      {% for user in users %} 
        <tr> 
        <td>{{user.id}}</td> 
        <td>{{user.fullName}}</td> 
        </tr> 
       {% endfor %} 
      </tbody> 
     </table> 

取得するには、このアクションの小枝グループ名でユーザーを表示する

/** 
    * @Route("/filter", name="users_filter") 
    */ 
    public function filterAction(Request $request) 
    { 
    $em = $this->getDoctrine()->getManager(); 

    $group = $request->request->get('group'); 

    $users = $em->getRepository('AppBundle:User')->findBy(['group' => group]); 

    return $this->render('AppBundle:User:list.html.twig', [ 
     'users' => $users 
    ]); 
} 
+0

[リダイレクトの可能性のある複製を作成選択ドロップダウンリストから項目を選択すると自動的に](https://stackoverflow.com/questions/580281/redirect-automatically-when-selecting-an-item-from-a-select-drop-down-list) –

+0

@TobiasXyページの特定の部分だけをリロードする必要があります。リダイレクトではありません – julie

答えて

1

テーブルのみをリロードする場合は、次の操作を行います。

まず、テンプレートを作成します。コントローラ

public function indexAction(Request $request) 
    { 
     $em = $this->getDoctrine()->getManager(); 

     $group = $request->request->get('group'); 

     $users = $em->getRepository('AppBundle:User')->findBy(['group' => group] 

     return new Response($this->renderView('table.html.twig', [ 
      'users' => $users, 
     ])); 
    } 

table.html.twig

 <table id="dataTable"> 
     <tbody> 
      {% for user in users %} 
        <tr> 
        <td>{{user.id}}</td> 
        <td>{{user.fullName}}</td> 
        </tr> 
       {% endfor %} 
      </tbody> 
     </table> 

そしてAJAX呼び出し

$('#select-button-div').on('change','#select-button',function() { 
    reloadData(); 
}); 

function reloadData() { 
     let data = {}; 
     let selected = $('#select-button').find(':selected').text(); 
     let usersTable = $('#entries'); 
     usersTable.empty(); 
     data['selectedVal'] = selected.val(); 
     let url = Routing.generate('ToYourRoute'); 
     table.load(url, data); 
    } 
関連する問題