laravel-splade icon indicating copy to clipboard operation
laravel-splade copied to clipboard

allow table column classes to be callable

Open jamesj2 opened this issue 11 months ago • 3 comments

I have tables with columns that I need to change the color based on what the data contains. For instance when displaying 'Approved' I want to make it green. If it's 'Cancelled' I want to make it red. This pull request allows the classes attribute to be a callable function and allows customizing the class based on the data in the column.

jamesj2 avatar Jul 10 '23 13:07 jamesj2

Hello James,

This is exactly what I need for a project (displaying 'Approved' I want to make it green. If it's 'Cancelled' I want to make it red). Do you have an example of how I implement it with your changes?

I would be very happy to receive an answer.

Greetings Sebastian

CoolCookie avatar Jul 22 '23 06:07 CoolCookie

@CoolCookie with this pull request you can do

->column('status', label: 'Status',  classes: function ($value, $item) {
    if ($value === 'Approved') {
        return 'text-green';
    }
    if ($value === 'Cancelled') {
        return 'text-red';
    }
})

Since I submitted this request. I've learned of another way to do this with x-splade-cell. Notice the 'status' attribute for x-splade-cell, it's the column key.

<x-splade-table :for="$table">
  <x-splade-cell status>
      @if ($item->status === 'Approved')
          <div class="text-green">Approved</div>
      @endif
      @if ($item->status === 'Cancelled')
          <div class="text-red">Cancelled</div>
      @endif
  </x-splade-cell>
</x-splade-table>

jamesj2 avatar Jul 22 '23 10:07 jamesj2

Hello James,

this is great and helps me a lot with my project. Thank you very much!

Greetings Sebastian

CoolCookie avatar Jul 23 '23 12:07 CoolCookie