mafortis
mafortis

Reputation: 7138

Laravel blade condition in ajax

I have my data returning by ajax into table in blade but i need a filter in my returned data.

Example

If I use blade loop I can use @if ($loop->last) in order to make special changes for my last record, but since my data is returning by ajax (JSON) I'm not sure how to do that.

Code

Commented the part i need filter

$.ajax({
    type:'GET',
    url:'{{url('dashboard/getProjectReimburses')}}/'+projectID,
    success:function(data){
    $(data.data).each(function(_, i){
        var number = i['amount'];
        var nf = new Intl.NumberFormat('en-US', {
            maximumFractionDigits:0, 
            minimumFractionDigits:0
        });
        var formattedNumber = nf.format(number);


        var row = `<tr data-id="${i.id}">'+
        '<td>${i.created_at}</td>'+
        '<td>${i.spent_date}</td>'+
        '<td>${i.cost_name}</td>'+
        '<td class="text-right">${nf.format(number)}</td>'+
        '<td>${i.description}</td>'+
        '<td>Attachments here</td>'+
        '<td>${i.statusName}</td>'+
        @role('employee|admin')
        '<td>${i.statusDate}</td>'+
        '<td>
// I need show this delete button only for latest row, for other rows just empty <td></td> //
        <Form method="DELETE" id="organizationFormDel">
        @csrf
        @method('DELETE')
        <button type="submit" data-id="${i.id}" class="btn btn-sm reimDelete btn-danger">Delete</button>
        </form>
// I need show this delete button only for latest row, for other rows just empty <td></td> //
        </td>'+
        '</tr>`;
        @else
        '<td>status date here</td></tr>`;
        @endrole
        $('#visit_table2').append(row);
    });
}
});

Any idea?

Update

Here is screenshot of my blade, I select 1 row from table 1, data shows in table 2

Now in table 2 as i marked it i want to show edit and delete button only for latest row other 3 rows empty <td>

one

Upvotes: 0

Views: 1819

Answers (3)

Bushart hussain
Bushart hussain

Reputation: 116

You can use a partial view for ajax. For example when you will send an ajax request on the backend then you will use a partial blade file on the backend and there you can use if statement in blade file and you will return Html as response and then you will append that Html to DOM on success.

$html = view('partial-blade',['data'=> $data])->render();

return $html;

Upvotes: 1

mafortis
mafortis

Reputation: 7138

Solved

Well I manage to do it the way I wanted, thanks to comments but my problem wasn't in controller unfortunately I couldn't convince friends that what i need is handle my ajax data on success and not controller.

Anyway here is my solution which works perfectly for me :

$(data.data).each(function(_, i){
  //.....
  var ms = _ == data.data.length -1; //get latest row
  if(ms == true){ // if is latest row
     // show my tr's with action buttons
     var row = `<tr data-id="${i.id}">'+
     //....
  } else {
     // show mt tr's without buttons
     var row = `<tr data-id="${i.id}">'+
     //....
  }
});

two

Thanks for all helps.

Upvotes: 0

Naeem Ijaz
Naeem Ijaz

Reputation: 825

You have to make a view and set all html code there. after calling Ajax and render view

$view = view('blade',["data"=>$data,])->render();
    return $view;

Upvotes: 0

Related Questions