wordpress自定义函数使用`WP_List_Table`创建表?

时间:2013-12-21 06:10:26

标签: php wordpress

我是WordPress的新手。我正在尝试使用WP_List_Table类创建一个WordPress表。我创建了一个表,但需要很长时间。所以,我想创建一个允许我创建WordPress表的函数,在这里我可以将数据和列数组传递给函数,然后该函数将创建所需的WordPress表。我想创建具有编辑,删除和可排序功能的表。

1 个答案:

答案 0 :(得分:2)

嘿试试这个代码吧它是动态函数但是你需要传递第一个参数kay并且name是id。

这是我的动态创建WP_List_table的类。

<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of wplist_table
 *
 * @author renishkhunt
 */
if (!class_exists('WP_List_Table')) {
    require_once( ABSPATH . 'wp-admin/includes/class-wp-list-table.php' );
}

class wplist_table extends WP_List_Table
{

    //put your code here
    var $data = array();
    var $default_columns = array();

    public function wplist_table($datad, $columns)
    {
        parent::__construct();

        $this->data = $datad;
        $this->default_columns = $columns;
    }

    function get_columns()
    {

        return $this->default_columns;
    }

    function prepare_items()
    {
        $columns = $this->get_columns();
        $hidden = array();
        $sortable = $this->get_sortable_columns();
        $this->_column_headers = array($columns, $hidden, $sortable);
        usort($this->data, array(&$this, 'usort_recorder'));
        $per_page = 10;
        $current_page = $this->get_pagenum();
        $total_items = count($this->data);

        // only ncessary because we have sample data
        $this->found_data = array_slice($this->data, (($current_page - 1) * $per_page), $per_page);

        $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
        ));
        $this->items = $this->found_data;
    }

    function column_default($item, $column_name)
    {
        foreach ($this->default_columns as $keys => $values) {
            if ($values == $column_name) {
                if(isset($item[$column_name])){
                    return $item[$column_name];
                }
            }
        }
    }

    function get_sortable_columns()
    {

        $i=0;
        $sortables = array();
        foreach ($this->default_columns as $keys => $values) {

            if($i == 0){
                $i++;
                //continue;
            }
                $sortables[$keys] = array($values,false);
        }
        return $sortables;
    }

    function usort_recorder($a, $b)
    {
        $orderby = (!empty($_GET['orderby'])) ? $_GET['orderby'] : 'id';

        $order = (!empty($_GET['order'])) ? $_GET['order'] : 'asc';

        $resutl = strcmp($a[$orderby], $b[$orderby]);
        return ( $order === 'asc') ? $resutl : -$resutl;
    }

    function column_Name($item)
    {
        $action = array(
            'edit' => sprintf('<a href="?page=%s&action=%s&fields=%s">Edit</a>', $_REQUEST['page'], 'edit', $item['id']),
            'delete' => sprintf('<a href="?page=%s&action=%s&fields=%s">Delete</a>', $_REQUEST['page'], 'delete', $item['id'])
        );
        return sprintf('%1$s %2$s', $item['name'], $this->row_actions($action));
    }

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

    function column_db($item)
    {
        return sprintf("<input type='checkbox' name='id[]' value='%s'", $item['id']);
    }

}

?>

只需将该代码复制到文件中并传递像列名和数据这样的参数。

  $data = array(
    array("id" => 1, "name" => "Renish Khunt", "add" => "asd"),
    array("id" => 2, "name" => "Renish Khunt", "add" => "asd"),
    array("id" => 3, "name" => "Renish Khunt", "add" => "asd")
);
$columns = array(
    "name" => "name",
    "add" => "add"
);

然后在创建类对象后,将这两个参数传递给数据和列名称。

$mylist_table = new wplist_table($data, $columns);
echo '<div class="wrap"><h2>Custome Fields<a class="add-new-h2" href="?page=' . $_REQUEST['page'] . '&action=add">Add New</a></h2>';
$mylist_table->prepare_items();
$mylist_table->display();
echo "</div>";

我希望这对你来说是完整的,你需要在$ column数组中添加列名所需的动态类,$ data数组将列的名称添加为键或值,如下所示。

$data = array(
array("id" => 1, "name" => "Renish Khunt", "add" => "asd","newcolumn"=>"value"),
array("id" => 2, "name" => "Renish Khunt", "add" => "asd","newcolumn"=>"value"),
array("id" => 3, "name" => "Renish Khunt", "add" => "asd","newcolumn"=>"value")
);
$columns = array(
    "name" => "name",
    "add" => "add",
    "newcolumn"=>"New Column"
);
像这样,我希望这段代码能够全部用完。

谢谢。