1

这是我的 WordPress 表格。我创建了一个数组,以便我可以尝试,但我需要添加类和 ID,以便我可以使用 CSS 来设置它的样式,就像顶级插件页面一样。

如何向表格元素添加类?

<?php

if(!class_exists('WP_List_Table')){
    require_once( ABSPATH . 'wp-admin/includes/class-wp-list-table.php' );
}


class TT_Example_List_Table extends WP_List_Table {

    var $example_data = array(
            array(
                'ID'        => 1,
                'title'     => '300',
                'rating'    => 'R',
                'director'  => 'Zach Snyder'
            ),
            array(
                'ID'        => 2,
                'title'     => 'Eyes Wide Shut',
                'rating'    => 'R',
                'director'  => 'Stanley Kubrick'
            ),
            array(
                'ID'        => 3,
                'title'     => 'Moulin Rouge!',
                'rating'    => 'PG-13',
                'director'  => 'Baz Luhrman'
            ),
            array(
                'ID'        => 4,
                'title'     => 'Snow White',
                'rating'    => 'G',
                'director'  => 'Walt Disney'
            ),
            array(
                'ID'        => 5,
                'title'     => 'Super 8',
                'rating'    => 'PG-13',
                'director'  => 'JJ Abrams'
            ),
            array(
                'ID'        => 6,
                'title'     => 'The Fountain',
                'rating'    => 'PG-13',
                'director'  => 'Darren Aronofsky'
            ),
            array(
                'ID'        => 7,
                'title'     => 'Watchmen',
                'rating'    => 'R',
                'director'  => 'Zach Snyder'
            )
        );


    function __construct(){
        global $status, $page;

        //Set parent defaults
        parent::__construct( array(
            'singular'  => 'movie',     //singular name of the listed records
            'plural'    => 'movies',    //plural name of the listed records
            'ajax'      => false        //does this table support ajax?
        ) );

    }


    function column_default($item, $column_name){
        switch($column_name){
            case 'rating':
            case 'director':
                return $item[$column_name] . 'hi';
            default:
                return print_r($item,true) . ' hi'; //Show the whole array for troubleshooting purposes
        }
    }


    function column_title($item){

        //Build row actions
        $actions = array(
            'edit'      => sprintf('<a href="?page=%s&action=%s&movie=%s">Edit</a>',$_REQUEST['page'],'edit',$item['ID']),
            'delete'    => sprintf('<a href="?page=%s&action=%s&movie=%s">Delete</a>',$_REQUEST['page'],'delete',$item['ID']),
        );

        //Return the title contents
        return sprintf('%1$s <span style="color:silver">(id:%2$s)</span>%3$s',
            /*$1%s*/ $item['title'],
            /*$2%s*/ $item['ID'],
            /*$3%s*/ $this->row_actions($actions)
        );
    }


    function column_cb($item){
        return sprintf(
            '<input type="checkbox" name="%1$s[]" value="%2$s" />',
            /*$1%s*/ $this->_args['singular'],  //Let's simply repurpose the table's singular label ("movie")
            /*$2%s*/ $item['ID']                //The value of the checkbox should be the record's id
        );
    }


    function get_columns(){
        $columns = array(
            'cb'        => '<input type="checkbox" />', //Render a checkbox instead of text
            'title'     => 'Title',
            'rating'    => 'Rating',
            'director'  => 'Director'
        );
        return $columns;
    }


    function get_sortable_columns() {
        $sortable_columns = array(
            'title'     => array('title',true),     //true means its already sorted
            'rating'    => array('rating',false),
            'director'  => array('director',false)
        );
        return $sortable_columns;
    }


    function get_bulk_actions() {
        $actions = array(
            'delete'    => 'Delete'
        );
        return $actions;
    }


    function process_bulk_action() {

        //Detect when a bulk action is being triggered...
        if( 'delete'===$this->current_action() ) {
            wp_die('Items deleted (or they would be if we had items to delete)!');
        }

    }


    function prepare_items() {


        $per_page = 5;


        $columns = $this->get_columns();
        $hidden = array();
        $sortable = $this->get_sortable_columns();


        $this->_column_headers = array($columns, $hidden, $sortable);


        $this->process_bulk_action();


        $data = $this->example_data;


        function usort_reorder($a,$b){
            $orderby = (!empty($_REQUEST['orderby'])) ? $_REQUEST['orderby'] : 'title'; //If no sort, default to title
            $order = (!empty($_REQUEST['order'])) ? $_REQUEST['order'] : 'asc'; //If no order, default to asc
            $result = strcmp($a[$orderby], $b[$orderby]); //Determine sort order
            return ($order==='asc') ? $result : -$result; //Send final sort direction to usort
        }
        usort($data, 'usort_reorder');



        $current_page = $this->get_pagenum();


        $total_items = count($data);



        $data = array_slice($data,(($current_page-1)*$per_page),$per_page);



        $this->items = $data;


        $this->set_pagination_args( array(
            'total_items' => $total_items,                  //WE have to calculate the total number of items
            'per_page'    => $per_page,                     //WE have to determine how many items to show on a page
            'total_pages' => ceil($total_items/$per_page)   //WE have to calculate the total number of pages
        ) );
    }

}



function tt_add_menu_items(){
    add_menu_page('Example Plugin List Table', 'List Table Example', 'activate_plugins', 'tt_list_test', 'tt_render_list_page');
} add_action('admin_menu', 'tt_add_menu_items');


function tt_render_list_page(){

    $testListTable = new TT_Example_List_Table();
    $testListTable->prepare_items();

    ?>
    <div class="wrap">

        <div id="icon-users" class="icon32"><br/></div>
        <h2>List Table Test</h2>


        <form id="movies-filter" method="get">
            <input type="hidden" name="page" value="<?php echo $_REQUEST['page'] ?>" />
            <?php $testListTable->display() ?>
        </form>

    </div>
    <?php
}
4

1 回答 1

6

做到这一点的唯一方法是重写WP_List_Table类的一些方法。

我继续修改您的类以支持表中每个 tr/td 的条件 HTML 类。您还不清楚您希望将类应用于哪些元素,也不清楚您想如何指定它,所以如果这不是您想要的,请原谅(并请指定更多详细信息)。

您可以在此处查看完整的代码(只有TT_Example_List_Table类存在 - 其余部分相同)。

基本上,您定义了一个名为$cond_classes. 该属性是一个多维条件数组。在那里,您有两个保留的顶级键-“奇数”和“偶数”。正如您所猜测的那样,它们将被访问为奇数或偶数的每一行。其余的顶级键可以是列 ID 或项目 ID。

每个顶级键可以保存一个数组或一个字符串如果顶级键保存一个字符串,那么当满足此条件时,添加该类如果顶级键保存一个数组,则循环遍历。二级数组可以有字符串值和key=>value对,其中key是类,value是条件数组。

我想这很令人困惑,但下面的示例应该让您了解它是如何工作的。

var $cond_classes = 数组(
    '奇数' => 数组(
        'odd-class', // 这个类总是被赋予奇数行和它们的列
        'special-odd-class' => array( // 如果行用于 ID 为 1、4 或 7 的项目,则仅将此类用于奇数行及其列
            'ID' => 数组(1、4、7)
        )
    ),
    '偶数' => 数组(
        '偶数级'
    ),
    '标题' => 数组(
        'custom_title_class',
        'special_title_class' => 数组(
            'ID' => array( 3, 7 ), // 这将只提供给 ID 为 3 或 7 的项目的“标题”列
            'title' => 'The Fountain', // 这只会提供给标题为“The Fountain”的项目的“title”列
        ),
    ),
    7 => 'id_7_class', // 这将被赋予一行,它是 ID 为 7 的项目的列
);

您可以在结果表中看到应用的类: HTML 代码预览

希望有帮助!如果您有任何问题 - 请继续 :)

于 2012-11-13T14:43:35.393 回答