wp list table - Index of row in WP_List_Table

In WP_List_Table, the method column_$custom( $item ), if implemented, is called once for each row of data that is being

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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744883986a4599007.html

相关推荐

  • wp list table - Index of row in WP_List_Table

    In WP_List_Table, the method column_$custom( $item ), if implemented, is called once for each row of data that is being

    1天前
    50

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信