Reputation: 115
My lecturer gave me a project for me and my friend, creating an app using newest Laravel framework. I'm not to familiar with this framework. I'm trying to load a data to my view using AJAX.
User Navigation for searching customer
This is the code for displaying the table
@foreach($mpelanggan as $mpelanggan)
{{ Form::open() }}
{{ Form:: hidden('_method' ,'DELETE')}}
<tr>
<p hidden id="kd" value="{{$mpelanggan->Kd_Pelanggan}}"/>
<td>{{$mpelanggan->Perusahaan}}</td>
<td align="center">
<button type="button" class="btntransparant" value="{{$mpelanggan->Perusahaan}}" id="ch">
<span class="glyphicon glyphicon-plus" aria-hidden="true"></span>
</button>
<a class="fontblack" >
<span class="glyphicon glyphicon-pencil" aria-hidden="true"></span>
</a>  
<button type="submit" class="btntransparant" onclick="return confirm('Are you sure you want to delete this data?');">
<span class="glyphicon glyphicon-trash" aria-hidden="true"></span>
</button>
</a>
</td>
</tr>
{!!Form::close()!!}
@endforeach
When user click the + button it will load the list of his transaction in this table:
My Routes:
Route::get('trans','TransController@index');
Route::get('getBTB',array('as'=>'getBTB','uses'=>'TransController@getBTB'));
My Controller TransController:
public function index(){
$Mpelanggan = Mpelanggan::all();
return view('tr.nav')->with('mpelanggan' , $Mpelanggan);
}
public function getBTB(Request $request){
$term = $request->term;
$data = Trbtb_h::where('Kd_Pelanggan',$term)->get();
$result=array();
foreach ($data as $key => $value) {
$result[]=['No_BTB'=>$value->No_BTB,'Tgl_BTB'=>$value->Tgl_BTB];
}
return Response::json(array('success'=>true,'result'=>$result));
}
The AJAX
$('#ch').click(function(){
var np=$(this).attr('value');
var get=$('#table1 > tbody');
$('#btb-title').text(np);
$("#pnl-btb").show();
$("#pnl-faktur").show();
$("#pnl-bayar").show();
$("#pnl-btb-list").show();
$.ajax({
url :'getBTB',
type :'GET',
dataType :'JSON',
success :'success',
data :{'term':kd},
success :function(result){
var data = JSON.parse(result);
var x;
for(x=0;x<data.length;x+=1){
var dat = data[i];
get.append('<tr><td>'+dat.No_BTB+'</td><td>'+dat.Tgl_BTB+'</td><td align="center"><button type="button" class="btntransparant" value="" id="ch"><span class="glyphicon glyphicon-plus" aria-hidden="true"></span> </button> <a class="fontblack" > <span class="glyphicon glyphicon-pencil" aria-hidden="true"></span> </a>  <button type="submit" class="btntransparant><span class="glyphicon glyphicon-trash" aria-hidden="true"></span></button></a></td></tr>');
}
}
})
});
I want to put the data from $result in the btody tag, just like the customer list table.
<div class="row" id="pnl-faktur">
<table class="table table-striped table-bordered" id="table2">
<thead>
<tr>
<th>Plh</th>
<th>No. Faktur</th>
<th>Tanggal</th>
<th>Jatuh Tempo</th>
<th>Piutang</th>
</tr>
</thead>
<tbody>
</tbody>
<tfoot>
<td colspan="4" align="right">Total</td>
<td><input type=text/></t>
</tfoot>
</div>
I watched some tutorial about AJAX and Laravel but none that I understand, I dont know what to do to load the $result to my view in the AJAX script. This is my first question in stackoverflow, if i made any mistake im sorry
Upvotes: 4
Views: 23208
Reputation: 4171
$arr=[];
$arr['javob'] = "ok";
$arr['scan_necessary'] = $scan_necessary;
return response()->json($arr, 204);
Upvotes: 2
Reputation: 115
The problem is I access the wrong array from the success function. So my friend advice me to split it into 2 different variable in the controllers
AJAX
$.ajax({
url :'getBTB',
type :'GET',
dataType :'json',
success :'success',
data :{'term':kd},
success :function(result){
$.each(result.result, function(i,index){
$('#table4 tbody').append('blablabla');
});
}
Controller
public function getBTB(Request $btbid){
$term = $btbid->term;
$data = Trbtb_h::where('Kd_Pelanggan',$term)->get();
$sen['success'] = true;
$sen['result'] = $data->toArray();
return Response::json( $sen );
}
Upvotes: 4