Consider the following example. For users there should be a field which indicates if the user is inactive. This is modelled in a table in Postgres by using a boolean as followed:
CREATE TABLE users
(id integer
GENERATED ALWAYS AS IDENTITY,
email_address text
NOT NULL,
inactive boolean
NOT NULL
DEFAULT false,
PRIMARY KEY (id),
UNIQUE (email_address));
The controller looks like this:
<?php
namespace App\Controllers;
use GroceryCrud\Core\GroceryCrud;
class UsersController extends BaseController
{
protected function _getDbData()
{
$db = (new \Config\Database())->default;
return ['adapter' => ['driver' => 'Pdo_Pgsql',
'host' => $db['hostname'],
'database' => $db['database'],
'username' => $db['username'],
'password' => $db['password'],
'charset' => 'utf8']];
}
protected function _getGroceryCrudEnterprise($bootstrap = true, $jquery = true)
{
$db = $this->_getDbData();
$config = (new \Config\GroceryCrudEnterprise())->getDefaultConfig();
$groceryCrud = new GroceryCrud($config, $db);
$groceryCrud->setCsrfTokenName(csrf_token());
$groceryCrud->setCsrfTokenValue(csrf_hash());
return $groceryCrud;
}
protected function _output($output = null, $title = null)
{
$output->title = $title;
if (isset($output->isJSONResponse)
&& $output->isJSONResponse) {
header('Content-Type: application/json; charset=utf-8');
echo $output->output;
exit;
}
return view('view', (array)$output);
}
public function index()
{
$crud = $this->_getGroceryCrudEnterprise();
$crud->setSubject('User', 'Users');
$crud->setTable('users');
$crud->columns(['email_address', 'inactive']);
$crud->fields(['email_address', 'inactive']);
$crud->inlineEditFields(['email_address', 'inactive']);
$crud->fieldType('email_address', 'email');
$crud->displayAs('email_address', 'E-Mail Address');
$crud->displayAs('wants_newsletter', 'Inactive');
$crud->requiredFields(['email_address', 'inactive']);
$crud->uniqueFields(['email_address']);
$crud->defaultOrdering('email_address');
$crud->callbackAddForm(function ($data) {
$data['inactive'] = false;
return $data;
});
$output = $crud->render();
if ($crud->getState() === 'Main') {
$output->output = '
<script>
window.addEventListener("gcrud.datagrid.ready", () => {
groceryCrudSetQuickSearchValue("inactive", false);
});
</script>
'
. $output->output;
}
return $this->_output($output, 'Users');
}
}
And that is the view:
<!DOCTYPE html>
<html>
<head>
<title>
<?php echo "Users"; ?>
</title>
<meta charset="utf-8" />
<?php foreach($css_files as $file): ?>
<link type="text/css" rel="stylesheet" href="<?php echo $file; ?>" />
<?php endforeach; ?>
</head>
<body>
<div>
<?php echo $output; ?>
</div>
<?php foreach($js_files as $file): ?>
<script src="<?php echo $file; ?>">
</script>
<?php endforeach; ?>
</body>
</html>
As you may have noticed, there is a little JavaScript in the output using groceryCrudSetQuickSearchValue() to set the quick filter value of inactive to false once the data grid is ready as described in the documentation. Unfortunately the filter is not set when the value is false. It get’s set when changed to true in the JavaScript though. Either way, false or true, the value in the dropdown in the header remains empty instead of showing the X or the check mark, even when the data is actually filtered.
We’re using Grocery Crud Enterprise 3.2.6 (i.e. currently the latest) in a CodeIgniter 4 project with the bootstrap v5 1.5.3 theme installed via Composer. All the 'remember_...' options ('remember_state_upon_refresh', 'remember_filters_upon_refresh', 'remember_quick_search' …) are set to false and really should be/remain so.
How to fix this?
And as a bonus question: What really would be neat is to have false as the default value for the filter. That is, it get’s set to false on loading and there is no yellow button for removing the filter if and only if the filter value is set to false. Clicking the yellow button (when there) will reset the filter value to false. I.e. the normal use is to only work with active users. Only in special cases inactive users should be shown. I believe that might need some tinkering with the JavaScript but I cannot find much documentation about that part.