Jet\DataListing_Filter_OptionSelect

Already predefined filter for defining the value selection. Just implement the return of the parameter name, the form field caption, the method defining the SELECT field options, and the WHERE generation.

Example:

namespace JetApplicationModule\EventViewer\REST;

use 
Jet\DataListing_Filter_OptionSelect;
use 
Jet\Form_Field_Select;
use 
Jet\Logger;
use 
Jet\Tr;

class 
Listing_Filter_EventClass extends DataListing_Filter_OptionSelect {
    
    public const 
KEY 'event_class';
    
    public function 
getKey(): string
    
{
        return static::
KEY;
    }
    
    public function 
getParamName() : string
    
{
        return 
'event_class';
    }
    
    public function 
getFormFieldLabel() : string
    
{
        return 
'Event class:';
    }
    
    
    protected function 
setFieldSelectOptionsForm_Field_Select $field ) : void
    
{
        
$options = [
            
''                          => Tr::_'- all -' ),
            
Logger::EVENT_CLASS_SUCCESS => Tr::_'success' ),
            
Logger::EVENT_CLASS_INFO    => Tr::_'info' ),
            
Logger::EVENT_CLASS_WARNING => Tr::_'warning' ),
            
Logger::EVENT_CLASS_DANGER  => Tr::_'danger' ),
            
Logger::EVENT_CLASS_FAULT   => Tr::_'fault' ),
        ];
        
        
$field->setSelectOptions$options );
    }
    
    public function 
generateWhere(): void
    
{
        if( 
$this->selected_value ) {
            
$this->listing->addFilterWhere( [
                
'event_class' => $this->selected_value,
            ] );
        }
    }    
}
Previous chapter
Jet\DataListing_Filter_DateTimeInterval
Next chapter
Exports - Jet\DataListing_Export