Show record index in table row
Hey guys, I know this might sound a bit silly but I was reading the documentation of Filament and seem to not get my head around how can I show a simple table rows index instead of record id. Why I need that? Sometimes when we edit records and re-add some new, the ID of the table changes (of course) but we would still like to present the first one as 1, second as 2 etc.
I am sure I am missing something ridiculously simple but just to speed up the custom project development and ask the great knowledge here to share some more, I will wait patiently for the answer to laugh at my miss (again) π
6 Replies
use this as the first column of your table
It goes like this: $rowLoop->iteration gives you the current iteration of the loop so 1, 2, 3 etc. But lets imagine you have 10 records and showing only 5 per page. So on the second page, you want the index to start from 6 and onwards. So you have
getTableRecordsPerPage()
which returns 5, getTablePage()
which return 2 (second page) minus 1 equals 1. Let's say Iteration will be 1 for the first row of the second page. So the first row index is 1 + (5 * 1), which equals to 6
I hope that's somewhat clear
If not, let me knowWow, so there is really not something like the good old ++$q in the table row π I am really surprised that it is with that many lines that we just list table rows of previous + 1 for every new
Jokes aside, the code you suggested really worked well, @tuto1902 but I am thinking there really should be a custom function of input field created for the id which might use the code behind scenes but will be elegant to implement after π
Of if we could define the field as HTML and inside to use vanilla JavaScript to start from 0 and increment for every new row.
Thanks again for the help, if nothing simpler I can think of, at least this one works nicely no matter if a few lines or not.
At least I was able to improve a bit adding ->sortable at the end to allow sorting by ID ASC/DESC π
I don't know if it can get simpler. I mean, that example is straight out of the documentation. We shall not defy the Filament gods π
$rowLoop is the most basic counter of rows, which is just the Laravel's blade $loop property that we get when working with blade loops. So I'm assuming the table is working with a @foreach at its core
But at least it worked for you, so that's a plus
ππ»
Well, Well, I knew I must be missing something SIMPLE AND ELEGANT that the DEVs of Filament have thought of.
Here is the sweet and short version for the index column:
In other words, making a textColumn an index and assigning the function rowIndex() and we get the same result out of the box. Short, elegant and easy to know what it does π
Now I can rsip some coffee and move on with the other project needs in a chill mode π
It was all there, I just somehow tested it and it did not work initially but most likely I've changed the 'index' name to something else which broke it π
https://filamentphp.com/docs/3.x/tables/columns/text