0

I have data table column like this...

[] | Building | Floor | Room number | Description | Price

In my list table class, I was set get_primary_column_name() method to return room_num.
The CSS class seems to set correctly, column-primary was changed to Room number column but when I resize the web browser to small screen the table is mess up and cannot collapse/expand correctly.

Here is the list table class.

namespace RdApm\App\Models;

if (!class_exists('\\RdApm\\App\\Models\\RoomsListTable')) {
    class RoomsListTable extends \RdApm\App\Models\WPListTable
    {


        /**
         * {@inheritDoc}
         */
        protected function column_cb($item)
        {
            return sprintf('<input type="checkbox" name="room_id[]" value="%s">', $item['room_id']);
        }// column_checkbox


        /**
         * {@inheritDoc}
         */
        protected function column_default($item, $column_name)
        {
            switch ($column_name) {
                case 'building_id':
                    if (isset($item['building_name'])) {
                        return '<a href="' . admin_url('admin.php?page=rd-apm-manage-buildings&amp;subpage=edit&amp;building_id=' . $item['building_id']) . '">' . $item['building_name'] . '</a>';
                    }
                    break;
                case 'rr_id':
                    if (isset($item['rr_price'])) {
                        $Number = new \Rundiz\Number\Number();
                        $output = '<a href="' . admin_url('admin.php?page=rd-apm-manage-roomrates&amp;subpage=edit&amp;rr_id=' . $item['rr_id']) . '">' . $Number->removeDotZero(number_format(stripslashes($item['rr_price']), 2, '.', '')) . '</a>';
                        $RoomRatesListTable = new RoomRatesListTable();
                        $output .= ' (' . $RoomRatesListTable->column_rr_type($item) . ')';
                        unset($Number, $RoomRatesListTable);
                        return $output;
                    }
                    break;
                default:
                    if (isset($item[$column_name]) && is_scalar($item[$column_name])) {
                        return $item[$column_name];
                    }
                    return '';
            }
        }// column_default


        /**
         * {@inheritDoc}
         */
        protected function get_bulk_actions()
        {
            $actions = [];

            $actions['delete'] = __('Delete', 'rd-apm');

            return $actions;
        }// get_bulk_actions


        /**
         * {@inheritDoc}
         */
        public function get_columns()
        {
            $columns = [
                'cb' => '<input type="checkbox">',
                'building_id' => __('Building name', 'rd-apm'),
                'room_floor' => __('Floor', 'rd-apm'),
                'room_num' => __('Room number', 'rd-apm'),
                'room_description' => __('Description', 'rd-apm'),
                'rr_id' => __('Price', 'rd-apm'),
            ];

            return $columns;
        }// get_columns


        /**
         * {@inheritDoc}
         */
        protected function get_primary_column_name()
        {
            $columns = $this->get_columns();
            $default = 'room_num';
            if (!isset($columns[$default])) {
                $default = $this->get_default_primary_column_name();
            }
            unset($columns);

            $column = apply_filters('list_table_primary_column', $default, $this->screen->id);
            if (empty($column) || !isset($columns[$column])) {
                $column = $default;
            }
            unset($default);

            return $column;
        }// get_primary_column_name


        /**
         * get column that can be sortable.
         * 
         * @return array
         */
        protected function get_sortable_columns()
        {
            $sortable_columns = [
                'room_floor' => ['room_floor', false],
                'room_num' => ['room_num', false],
            ];

            return $sortable_columns;
        }// get_sortable_columns


        /**
         * {@inheritDoc}
         * 
         * @global \wpdb $wpdb
         * @param array $options available options: search, sort (column name), order (ascending descending)
         */
        public function prepare_items(array $options = [])
        {
            // prepare columns
            $columns = $this->get_columns();
            $hidden = [];
            $sortable = $this->get_sortable_columns();
            $primary = $this->get_primary_column_name();
            $this->_column_headers = [$columns, $hidden, $sortable, $primary];
            unset($columns, $hidden, $primary);

            global $wpdb;
            $wpdb->show_errors();

            $per_page = 50;
            $current_page = $this->get_pagenum();

            $table_rooms = $wpdb->prefix . 'rdapm_rooms';
            $table_buildings = $wpdb->prefix . 'rdapm_buildings';
            $table_rr = $wpdb->prefix . 'rdapm_room_rates';

            // connect db get data.
            $sql = 'SELECT %*%';
            $sql .= ', ' . $table_rooms . '.building_id AS building_id';
            $sql .= ', ' . $table_rooms . '.rr_id AS rr_id';
            $sql .= ' FROM ' . $table_rooms;
            $sql .= ' LEFT JOIN ' . $table_buildings . ' ON ' . $table_rooms . '.building_id = ' . $table_buildings . '.building_id';
            $sql .= ' LEFT JOIN ' . $table_rr . ' ON ' . $table_rooms . '.rr_id = ' . $table_rr . '.rr_id';
            $sql .= ' WHERE %d';
            if (isset($options['search'])) {
                $sql .= ' AND (';
                $sql .= ' room_num LIKE %s';
                $sql .= ' OR room_description LIKE %s';
                $sql .= ' OR ' . $table_buildings . '.building_name LIKE %s';
                $sql .= ' OR ' . $table_rr . '.rr_description LIKE %s';
                $sql .= ' OR ' . $table_rr . '.rr_price LIKE %s';
                $sql .= ')';
            }

            $args = [];
            $args[] = 1;
            if (isset($options['search'])) {
                $args[] = '%'.$wpdb->esc_like($options['search']).'%';
                $args[] = '%'.$wpdb->esc_like($options['search']).'%';
                $args[] = '%'.$wpdb->esc_like($options['search']).'%';
                $args[] = '%'.$wpdb->esc_like($options['search']).'%';
                $args[] = '%'.$wpdb->esc_like($options['search']).'%';
            }

            // get total data
            $total_items = $wpdb->get_var(
                $wpdb->prepare(
                    str_replace(
                        [
                            '%*%', 
                        ], 
                        [
                            'COUNT(' . $table_rooms . '.room_id)', 
                        ], 
                        $sql
                    ), 
                    $args
                )
            );

            // set sort order
            $sort = 'room_id';
            if (isset($options['sort']) && array_key_exists($options['sort'], $sortable)) {
                $sort = $options['sort'];
            }
            if ($sort == 'building_id') {
                $sort = $table_rooms . '.building_id';
            } elseif ($sort == 'rr_id') {
                $sort = $table_rooms . '.rr_id';
            }

            $order = 'DESC';
            if (isset($options['order'])) {
                if (strtolower($options['order']) == 'asc') {
                    $order = 'ASC';
                }  elseif (strtolower($options['order']) == 'desc') {
                    $order = 'DESC';
                } elseif (strtolower($options['order']) == 'rand') {
                    $order = 'RAND';
                }
            }

            // get data that sliced per page and sort order
            $sql = str_replace('%*%', '*', $sql);
            // sort order
            $sql .= ' ORDER BY ' . $sort . ' ' . $order;
            // sliced per page
            $sql .= ' LIMIT ' . (($current_page - 1) * $per_page) . ', ' . $per_page;

            $results = $wpdb->get_results($wpdb->prepare($sql, $args), ARRAY_A);

            // clear unused items
            unset($args, $current_page, $order, $sort, $sortable, $sql, $table_buildings, $table_rooms, $table_rr);

            // create pagination
            $this->set_pagination_args([
                'total_items' => $total_items, 
                'per_page'    => $per_page
            ]);

            $this->items = $results;
        }// prepare_items


    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.