If you need this with VueJs, we can help you, jQuery is quite hard for me))
How to display values in returned from ajax call in tabular format Laravel?
Guys I have a data in table which I displayed in view.blade in tabular format This is the code
<thead>
<th> <a href="" class="btn btn-success btn-sm" style="width: 100%;">Search</a></th>
</thead>
<thead >
<th>Date</th>
<th>Time</th>
<th>Purchased Milk (Lt)</th>
<th>Self Milk (Lt)</th>
<th>Total Milk (Lt)</th>
<th>Sold Milk (Lt)</th>
<th>Excess Milk (Lt)</th>
<th>Balance Milk (Lt)</th>
</thead>
<tbody>
@foreach($overalls as $overall)
<tr>
<td>{{ $overall->date }}</td>
<td>{{ $overall->time }}</td>
<td>{{ $overall->purchased_milk }}</td>
<td>{{ $overall->self_milk }}</td>
<td>{{ $overall->total_milk }}</td>
<td>{{ $overall->sold_milk }}</td>
<td>{{ $overall->excess_milk }}</td>
<td>{{ $overall->balance_milk }}</td>
</tr>
I have a search box to filter the displayed result. I wrote ajax call for the search box. That is if I enter a name in the search box the name is taken to the controller by ajax call and then searches the details of that name and then I displayed the search result in the controller itself as tabular format and returned the output.
this is my ajax
<script type="text/javascript">
$('#search_balance').on('keyup',function(){
$.ajax({
type : 'get',
url : '{{URL::to('search_overall_stock_details')}}',
data:{
'search_balancem': $('#search_balance').val()
},
success:function(data){
$('tbody').html(data);
}
});
})
</script>
This is my controller
public function search_overall_stock_details( Request $request )
{
$output="";
$overallstock = Overallstock::where(function ($query) use ($request)
{
if (!empty($request->search_balance)) {
$query->Where('date', 'LIKE', '%' . $request->search_balance . '%');
}
})->orderBy('created_at','desc')->paginate(5);
if($overallstock)
{
foreach ($overallstock as $key => $overallstocke)
{
$output.='<tr>'.
'<td>'.$overallstocke->date.'</td>'.
'<td>'.$overallstocke->time.'</td>'.
'<td>'.$overallstocke->purchased_milk.'</td>'.
'<td>'.$overallstocke->self_milk.'</td>'.
'<td>'.$overallstocke->total_milk.'</td>'.
'<td>'.$overallstocke->sold_milk.'</td>'.
'<td>'.$overallstocke->excess_milk.'</td>'.
'<td>'.$overallstocke->balance_milk.'</td>'.
'</tr>';
}
return Response($output);
}
But in some reasons its very difficult to work with tables in that controller.
What I need is to display the search result in tabular format in the same view.balde itself. i know need to write the tabular format in the controller.
Can anyone help me please ..!!
The solution is
to write it in ajax
this is the right code
$('#search_frmdate,#search_date,#search_time,#search_nl,#search_note').on('keyup',function(){
$.ajax({
dataType: 'json',
type : 'get',
url : '{{URL::to('search_excess_milk_details')}}',
data:{ 'search_frmdate': $('#search_frmdate').val(),
'search_todate': $('#search_date').val(),
'search_time': $('#search_time').val(),
'search_nl': $('#search_nl').val(),
'search_note': $('#search_note').val()
},
success:function(data)
{
console.log(data);
var res='';
$.each (data, function (key, value) {
res +=
'<tr>'+
'<td>'+value.date+'</td>'+
'<td>'+value.time+'</td>'+
'<td>'+value.no_of_litre+'</td>'+
'<td>'+value.note+'</td>'+
'</tr>';
});
$('tbody').html(res);
}
});
})
and the search in controller is
public function search_excess_milk_details( Request $request )
{
$xcess_milks = Excess_milk::where(function ($query) use ($request)
{
if (!empty($request->search_time)) {
$query->Where('time', 'LIKE', '%' . $request->search_time . '%');
}
})->orderBy('date','asc')->get;
return Response($xcess_milks);
}
Please or to participate in this conversation.