Change default visible columns in the Settings-Columns-Dropdown-Menu

I am asking myself: is there an easy way to change the default settings of the visible columns in the “Settings-Columns-Button”?

Background is: I want some columns be visible by default (which are probably saved within a user account property), but others should not be visible by default. The user should be able to change this setting and it will be saved in his account properties.

Reformulated: is there a way to program, which columns are set/unset in the “Settings-Columns-Dropdown-Menu”?

Did I miss something in the documentation?

Regards
Obiwanke

In summary there is no such feature in Grocery CRUD Enterprise but there is a work-around for this.

More specifically:

Step 1. Use the setUniqueId function to have a unique id for our CRUD so we will use it to cache our visibleColumns in JavaScript

For example:

$crud->setUniqueId('customers_1234');

or for simplicity you can have the table name as the uniqueId:

$crud->setUniqueId('customers');

Step 2. Now write a little JavaScript (yeah I know) to the default columns that you would like to have visible. Just to make it a slightly easier for you, I will write the JavaScript as part of the PHP output. So more specifically:

$tableName = 'customers';
$crud->setTable($tableName);
$crud->setUniqueId($tableName);
// ... some other $crud-> things

$output = $crud->render();

// Make sure that the extra JavaScript code is only applied on Main State
if ($crud->getState() === 'Main') {
    $defaultVisibleColumns = function ($uniqueId, $visibleColumns) {
        return "<script>
        if (!localStorage.getItem('gcrudVisibleColumns_$uniqueId')) {
                    localStorage.setItem('gcrudVisibleColumns_$uniqueId', '[\"" .
                implode('", "', $visibleColumns)
            . "\"]');
        }
        </script>\n";
    };
    $output->output = $defaultVisibleColumns($tableName, ["customer_name","contact_first_name","notes","email"]) . $output->output;
}

Of course this will be easier to just wait so you can have it as a functionality in Grocery CRUD Enterprise. I understand though that sometimes you just need a work-around as we all have deadlines and bosses so there is your work-around for this :slight_smile:

As many people asked for it, It is very possible to be a candidate for the next release.

Regards
Johnny

2 Likes