jeudi 3 janvier 2019

Why output using ajax not diplay in table bu json format?

I want to create a searching filters and display the output using ajax.

This is the output table under the form:

  <div class="panel panel-default">
                  <div class="panel-heading">Senarai Calon Anugerah</div>
                     <div class="panel-body">
                            @if (session('status'))
                                <div class="alert alert-success">
                                    
                                </div>
                            @endif


                             @if(Auth::check())
                                <div class="container table-responsive col-lg-12">
                                    <!-- <div class="container text-center"> -->      
                                        <table class="table table-striped table-bordered" id="calon_table" >
                                            <thead>
                                              <tr>
                                                <td class="text-center col-lg-3"><strong>Name</strong></td>
                                                <td class="text-center"><strong>Action</strong></td>
                                                <!-- <td class="text-center"><strong>Lihat Rekod</strong></td> -->
                                              </tr>
                                            </thead>

                                        <tbody id="calon_anugerah">

                                        </tbody>
                                        </table>
                                    <!-- </div> -->

                                </div>
                            @endif
                            @if(Auth::guest())
                                <a href="/login" class="btn btn-info"> Anda perlu log masuk.</a>
                            @endif
                     </div>
                  </div>
    </div>

The ajax code to get the data is:

 <script type="text/javascript">
  $('#search').on('click', function(){

    $.get("",function(data){

        $.each(data, function(i, value){
            // alert(value.name);
            var tr =$("<tr/>");
              tr.append($("<td/>",{
                  text : value.name
              }))
              $('#calon_anugerah').append(tr);
        });
    })
  })
</script>

I had queried the data using the code in CarianAnugerahController@search:

 $query = DB::table('itemregistrations')
    ->select('itemregistrations.ItemRegistrationID','itemregistrations.name', 'itemregistrations.Nobadan');

    if(request('umur')) {
        $query->whereRaw('YEAR(CURDATE()) - lahir_yy >= ?', [request('umur')]);  
    }

    if(request('negeri_lahir')) {
        $query->where('NegeriID', request('negeri_lahir'));
    }

    if(request('kategori')) {
        $query->where('CategoryID', request('kategori'));
    }

    if(request('pangkat')) {
        $query->where('OperasiID', request('pangkat'));
    }

$newitem = $query->get();

return response($newitem);

This is the route:

 Route::resource('carian_anugerah', 'Modul\CarianAnugerahController');
 Route::post('/search-calon', 'Modul\CarianAnugerahController@search');

I can get the value but it doesn't display in table..it shows the output in json format in a white page..

What is missing in the ajax code?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire