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

Expose Laravel Loop Object To Column #4857

Merged
merged 25 commits into from Nov 9, 2022
Merged

Expose Laravel Loop Object To Column #4857

merged 25 commits into from Nov 9, 2022

Conversation

Nuhel
Copy link
Contributor

@Nuhel Nuhel commented Nov 6, 2022

Sometime we need to show the Serial Number/Iteration Number in table. I could not find any way to do this with filament table. (May be there is a way, if so i will close this PR)

This PR Expose Laravel Loop Object To Columns,

We can use it like below

Tables\Columns\TextColumn::make('sl')->getStateUsing(function ($loop){
      return $loop->iteration;
}),

Also as it is laravel's foreach loop object, We can access every other property like
index, remaining, count etc.

Nuhel and others added 2 commits November 7, 2022 00:08
This PR Expose Laravel Loop Object To Column,

We can use it like below

Tables\Columns\TextColumn::make('sl')->getStateUsing(function ($loop){
      return $loop->iteration;
}),
@what-the-diff
Copy link
Contributor

what-the-diff bot commented Nov 6, 2022

  • Added rowIndex() method to TextColumn
  • Added HasRowLoopObject trait and $loop property in Column class
  • Passed $rowLoop object from index view component to layout components

@danharrin
Copy link
Member

Instead of loop, please use rowIndex and start from 0

@danharrin danharrin added the enhancement New feature or request label Nov 6, 2022
@danharrin danharrin marked this pull request as draft November 6, 2022 21:02
@Nuhel
Copy link
Contributor Author

Nuhel commented Nov 7, 2022

Instead of loop, please use rowIndex and start from 0

You mean instead of loop object just pass an int that starts from 0?

@zepfietje zepfietje added this to the v2 milestone Nov 7, 2022
@danharrin
Copy link
Member

well at the moment you're passing in the column loop object and not the row loop object? i think? which is wrong right?

@Nuhel
Copy link
Contributor Author

Nuhel commented Nov 7, 2022

well at the moment you're passing in the column loop object and not the row loop object? i think? which is wrong right?

Yes, if we pass the loop object, we will have some extra options like remaining, count, depth, last etc, which will help if we need those.

But it's your call , i will update the PR if you think loop object is not a good fit.

@danharrin
Copy link
Member

I agree now that the loop object is fine, but please test your code. I don't think it works, as you're passing in the column loop and not the row loop. For clarity we can name it rowLoop() as well, but I think it still needs fixing

Changed loop method to rowLoop. And fixed issue with loop object.
@Nuhel
Copy link
Contributor Author

Nuhel commented Nov 7, 2022

I agree now that the loop object is fine, but please test your code. I don't think it works, as you're passing in the column loop and not the row loop. For clarity we can name it rowLoop() as well, but I think it still needs fixing

Yes I was passing column loop -_- .

Test With normal Table Layout.
Screenshot 2022-11-07 at 7 19 55 PM

Test with Split and Stack
Screenshot 2022-11-07 at 7 19 26 PM

@Nuhel Nuhel marked this pull request as ready for review November 7, 2022 13:24
@danharrin
Copy link
Member

I've just refactored a bit, does it still work?

@Nuhel
Copy link
Contributor Author

Nuhel commented Nov 7, 2022

I've just refactored a bit, does it still work?

Not Working With Stack & Split.
Filament\Tables\Columns\Column::rowLoop(): Argument #1 ($loop) must be of type ?stdClass, Closure given,

@Nuhel
Copy link
Contributor Author

Nuhel commented Nov 7, 2022

I've just refactored a bit, does it still work?

{{ $layoutComponent->viewData(['recordKey' => $recordKey, 'rowLoop' => $rowLoop]) }}

rowLoop has to be passed in viewData.

Fixed Issue After Refactoring.
@danharrin
Copy link
Member

@Nuhel how about passing it into the object instead and then using $getRowLoop() in the view? I've added the trait to the Component object

@Nuhel
Copy link
Contributor Author

Nuhel commented Nov 7, 2022

@Nuhel how about passing it into the object instead and then using $getRowLoop() in the view? I've added the trait to the Component object

May be i did not get you, can you elaborate please?

@danharrin
Copy link
Member

I've made the changes, test again please

@Nuhel
Copy link
Contributor Author

Nuhel commented Nov 7, 2022

I've made the changes, test again please

Yes working.

Should I update documentation? And I am thinking to add below function to CanFormatState Trait

public function rowIndex(bool $fromZero = false): static
{
    $this->getStateUsing(static function ($rowLoop) use($fromZero): ?string {
        return $rowLoop->{ $fromZero? "index":"iteration"};
    });

    return $this;
}

@danharrin danharrin merged commit 7f42837 into filamentphp:2.x Nov 9, 2022
@Nuhel Nuhel deleted the Expose-Laravel-Loop-Object-To-Column branch November 9, 2022 17:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants