In WP_List_Table
, the method column_$custom( $item )
, if implemented, is called once for each row of data that is being displayed in the list table. So, if my list table has three rows of data, then the method column_$custom( $item )
will be called three times, once for each row
In addition to displaying the data, I need to know the "row index" of the data being displayed. In the above example, I like to extract 0
because it is the first row, 1
for the second, and 2
for the third. Is there a way of knowing the row index?
In WP_List_Table
, the method column_$custom( $item )
, if implemented, is called once for each row of data that is being displayed in the list table. So, if my list table has three rows of data, then the method column_$custom( $item )
will be called three times, once for each row
In addition to displaying the data, I need to know the "row index" of the data being displayed. In the above example, I like to extract 0
because it is the first row, 1
for the second, and 2
for the third. Is there a way of knowing the row index?
1 Answer
Reset to default 1That should be possible, but it looks pretty complicated. As you have undoubtedly seen, the display_rows
method just passes a single line and there's no counter. This means that the row number must be in the $item
variable for you to extract it. Since it is not there natively, you must insert it before you start displaying the table.
Luckily, there is a method expressly designed to do this, prepare_items
. This is an abstract class, meaning it must be filled in by child classes that use this class. Take, for instance, a look at the way it is implemented in WP_Posts_List_Table
.
So what you'll have to is override the existing prepare_items
of the child class you are working with, and loop through the rows to add a new property $row_count
to each one of them. Then, when you implement column_$custom( $item ) you can extract $row_count
.
Disclaimer: obviously I didn't test this myself, it's just a concept I think should work.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744883986a4599007.html
评论列表(0条)