admin管理员组

文章数量:1410697

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?

Share Improve this question edited Dec 22, 2019 at 14:29 cjbj 15k16 gold badges42 silver badges89 bronze badges asked Dec 22, 2019 at 13:31 GreesoGreeso 2,2347 gold badges33 silver badges57 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

That 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.

本文标签: wp list tableIndex of row in WPListTable