Add Sort Feature Into Table - mdhemalakhand1999/WordPressPluginDevelopment GitHub Wiki

First add get_sortable_columns() function to add sort option in top heading

/* For sortable columns */
function get_sortable_columns() {
    return ['age' => [
            'age', true
        ]
    ];
}

Now add this function into prepare items

function prepare_items() {
    // get columns
    $this->_column_headers = array($this->get_columns(), array(), $this->get_sortable_columns());
}

Now we will use these code for sort functionality

/**
 * Order functionality
 */
$orderby = $_REQUEST['orderby'] ?? '';
$order = $_REQUEST['order'] ?? '';
if('age' == $orderby) {
    if('asc' == $order) {
        usort($data, function($item1, $item2) {
            return $item2['age'] <=> $item1['age'];
        });
    } else {
        usort($data, function($item1, $item2) {
            return $item1['age'] <=> $item2['age'];
        });
    }
}

Here is our complete code:

class.persons-table.php

<?php
// first add class-wp-list-table.php
if ( ! class_exists( "WP_List_Table" ) ) {
	require_once( ABSPATH . "wp-admin/includes/class-wp-list-table.php" );
}

class Persons_Table extends WP_List_Table {
// construct first
function construct( $args = array()) {
parent::
construct($args);
}
// set data
function set_data($data) {
$this→items = $data;
}
// set our custom columns
function get_columns() {
return [
‘cb’ => ‘’,
‘name’ => ‘Name’,
‘email’ => ‘Email’,
‘age’ => ‘Age’
];
}
/***************** sortable start ***********************/
/* For sortable columns/
function get_sortable_columns() {
return [‘age’ => [
‘age’, true
]
];
}
/***************** sortable end ***********************/
function column_cb($item) {
return “<input type=‘checkbox’ value={$item[‘id’]} />”;
}
function column_email($item) {
return “{$item[‘email’]}”;
}
function column_age($item) {
return “{$item[‘age’]}”;
}
function prepare_items() {
// get columns
$this→column_headers = array($this→get_columns(), array(), $this→get_sortable_columns());
}
// for insert data into table ( item is each row)
function column_default($item, $column_name) {
return $item[$column
name];
}
}




data-table.php

<?php
/**
  • @package data_table
    /
    /

    Plugin Name: Data table
    Plugin URI: https://ptc.com/
    Description: Data table plugin is a light weight for count words.
    Version: 1.0
    Requires at least: 1.0
    Requires PHP: 5.2
    Author: HemalRika(HR) Foundation
    Author URI: https://hemalrika-hr.com
    License: GPLv2 or later
    Text Domain: data_table
    */
    require_once “class.persons-table.php”;
    function datatable_admin_menu() {
    // create a menu on admin page
    add_menu_page(“Data Table”, “Data Table”, “manage_options”, “datatable”, “datatable_display_func”);
    }
    add_action(“admin_menu”, “datatable_admin_menu”);

// this function will work for each row of data
function datatable_search_by_name($item) {
$name = strtolower( $item[‘name’] );
$search_name = sanitize_text_field( $REQUEST[‘s’] );
if ( strpos( $name, $search
name ) !== false ) {
return true;
}

return false;
}
function datatable_display_func() {
include_once “dataset.php”;
// create table using Persons_Table class
$table = new Persons_Table();
// if search submit, then apply filter for each row
if ( isset( $REQUEST[‘s’] ) && !empty($REQUEST[‘s’]) ) {
$data = array_filter( $data, ‘datatable_search_by_name’ );
}
/********************************** order functionality for sortable column **********************************/
$orderby = $_REQUEST[‘orderby’] ?? ’’;
$order = $
REQUEST[‘order’] ?? ‘’;
if(’age’ == $orderby) {
if(‘asc’ == $order) {
usort($data, function($item1, $item2) {
return $item2[‘age’] <=> $item1[‘age’];
});
} else {
usort($data, function($item1, $item2) {
return $item1[‘age’] <=> $item2[‘age’];
});
}
}
// insert data into table
$table→set
data($data);

// now prepare all items $table→prepare_items(); ?>
<?php // for search form into table $table→search_box(‘search’, ‘search_id’); // for display table $table→display(); ?>
<?php

}

⚠️ **GitHub.com Fallback** ⚠️