Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow table column classes to be callable #477

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

jamesj2
Copy link
Contributor

@jamesj2 jamesj2 commented Jul 10, 2023

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.

@CoolCookie
Copy link

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

@jamesj2
Copy link
Contributor Author

jamesj2 commented Jul 22, 2023

@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>

@CoolCookie
Copy link

Hello James,

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

Greetings
Sebastian

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants