$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>How to sort by columns within a search result without displaying all rows again after sorting|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

How to sort by columns within a search result without displaying all rows again after sorting

matteradmin7PV0评论

I want to display a WP_List_Table on a submenu_page and offer a search function.

The display of the data works so far and also the search, but the column sorting causes problems. If I click on the column to sort it will be sorted and all data will be displayed. If I search for a keyword, I get a modified table accordingly, I click on a column to sort it, I get all data displayed again - but I want to sort within my search results, how can I achieve this?

I've been looking for a solution for some time and haven't found anything yet. Either I'm stumped or I'm just pretty stupid. Either way, I would be happy if someone could help me.

Please note: I'm new to the world of programming, so please forgive me all my unknowingness and stupidity!

Below is the complete source code of the page:

if (!class_exists('WP_List_Table'))
{
    require_once ('../../assets/lib/class-wp-list-table.php');
}

class My_List_Table extends WP_List_Table
{

    public function no_items()
    {
        _e('Unfortunately there are no ingredients available :(', 'wp-my-recipe');
    }
    /**
     * Prepare the items for the table to process
     *
     * @return Void
     */
    public function prepare_items($search = NULL)
    {
        $data = $this->table_data($search);
        $columns = $this->get_columns();
        $hidden = $this->get_hidden_columns();
        $sortable = $this->get_sortable_columns();
        $perPage = 1;
        $currentPage = $this->get_pagenum();
        if (!is_null($data))
        {
            $totalItems = count($data);
            $data = array_slice($data, (($currentPage - 1) * $perPage) , $perPage);
        }
        else
        {
            $totalItems = 0;
        }
        $this->set_pagination_args(array(
            'total_items' => $totalItems,
            'per_page' => $perPage
        ));
        $this->_column_headers = array(
            $columns,
            $hidden,
            $sortable
        );
        $this->items = $this->table_data($search);
    }
    /**
     * Override the parent columns method. Defines the columns to use in your listing table
     *
     * @return Array
     */
    public function get_columns()
    {
        $columns = array(
            'cb' => '<input type="checkbox" />', // to display the checkbox.
            'id' => 'ID',
            'name' => __('Ingredient name', 'wp-my-recipe') ,
            'kcalories' => __('Kilocalories per 100 ml/mg', 'wp-my-recipe') ,
            'reference' => __('Reference value', 'wp-my-recipe')
        );
        return $columns;
    }
    /**
     * Define which columns are hidden
     *
     * @return Array
     */
    public function get_hidden_columns()
    {
        return array(
            'id'
        );
    }
    /**
     * Define the sortable columns
     *
     * @return Array
     */
    public function get_sortable_columns()
    {
        return array(
            'name' => array(
                'name',
                false
            ) ,
            'kcalories' => array(
                'kcalories',
                false
            ) ,
            'reference' => array(
                'reference',
                false
            ) ,
        );
    }
    /**
     * Get the table data
     *
     * @return Array
     */
    function table_data($search = NULL)
    {

        //todo add real search
        if ($search != NULL)
        {
            $search = trim($search);
            $data = array(
                ['id' => 'dummyData',
                'name' => $search,
                'kcalories' => 'dummyData',
                'reference' => 'dummyData']
            );
        }
        else
        {
            $data = s_getAllIngredients();
        }

        return $data;
    }
    /**
     * Define what data to show on each column of the table
     *
     * @param  Array $item        Data
     * @param  String $column_name - Current column name
     *
     * @return Mixed
     */
    public function column_default($item, $column_name)
    {
        switch ($column_name)
        {
            case 'id':
            case 'name':
            case 'kcalories':
            case 'reference':
                return $item[$column_name];
            default:
                return print_r($item, true);
        }
    }
    // /**
    //  * Allows you to sort the data by the variables set in the $_GET
    //  *
    //  * @return Mixed
    //  */
    private function sort_data($a, $b)
    {

        // Set defaults
        $orderby = 'name';
        $order = 'asc';
        // If orderby is set, use this as the sort column
        if (!empty($_GET['orderby']))
        {
            $orderby = $_GET['orderby'];
        }
        // If order is set use this as the order
        if (!empty($_GET['order']))
        {
            $order = $_GET['order'];
        }
        $result = strcmp($a[$orderby], $b[$orderby]);
        if ($order === 'asc')
        {
            return $result;
        }
        return -$result;

    }

    protected function column_cb($item)
    {
        return sprintf('<label class="screen-reader-text" for="ingredient_' . $item['id'] . '">' . sprintf(__('Select %s') , $item['name']) . '</label>' . "<input type='checkbox' name='ingredient[]' id='ingredient_{$item['id']}' value='{$item['id']}' />");
    }

}

$myListTable = new My_List_Table();

//Fetch, prepare, sort, and filter our data...
if (isset($_POST['s']))
{
    $myListTable->prepare_items($_POST['s']);
}
else
{
    $myListTable->prepare_items();
}

echo "<form role='search' method='post' id='searchform'>";
$myListTable->search_box("Search", "search_post_id");
echo "</form>";

$myListTable->display();
Post a comment

comment list (0)

  1. No comments so far