0 follower

Final Class Yiisoft\Yii\DataView\PageSize\SelectPageSize

InheritanceYiisoft\Yii\DataView\PageSize\SelectPageSize » Yiisoft\Widget\Widget
ImplementsYiisoft\Yii\DataView\PageSize\PageSizeWidgetInterface
Uses TraitsYiisoft\Yii\DataView\PageSize\PageSizeContextTrait

Widget that renders a dropdown (select) input for choosing the page size.

Public Methods

Hide inherited methods

Method Description Defined By
addAttributes() Add a set of attributes to existing select tag attributes. Yiisoft\Yii\DataView\PageSize\SelectPageSize
attributes() Replace select tag attributes with a new set. Yiisoft\Yii\DataView\PageSize\SelectPageSize
render() Renders the page size select with the current context. Yiisoft\Yii\DataView\PageSize\SelectPageSize
withContext() Creates a new instance with the specified page size context. Yiisoft\Yii\DataView\PageSize\PageSizeContextTrait

Protected Methods

Hide inherited methods

Method Description Defined By
getContext() Gets the current page size context. Yiisoft\Yii\DataView\PageSize\PageSizeContextTrait

Method Details

Hide inherited methods

addAttributes() public method

Add a set of attributes to existing select tag attributes.

Same named attributes are replaced.

public addAttributes( array $attributes ): self
$attributes array

Name-value set of attributes.

return self

New instance with added attributes.

                public function addAttributes(array $attributes): self
{
    $new = clone $this;
    $new->attributes = array_merge($new->attributes, $attributes);
    return $new;
}

            
attributes() public method

Replace select tag attributes with a new set.

public attributes( array $attributes ): self
$attributes array

Name-value set of attributes.

return self

New instance with replaced attributes.

                public function attributes(array $attributes): self
{
    $new = clone $this;
    $new->attributes = $attributes;
    return $new;
}

            
getContext() protected method

Defined in: Yiisoft\Yii\DataView\PageSize\PageSizeContextTrait::getContext()

Gets the current page size context.

protected getContext( ): Yiisoft\Yii\DataView\PageSize\PageSizeContext
return Yiisoft\Yii\DataView\PageSize\PageSizeContext

The current page size context.

throws LogicException

If the context has not been set.

                final protected function getContext(): PageSizeContext
{
    if ($this->context === null) {
        throw new LogicException('Context is not set.');
    }
    return $this->context;
}

            
render() public method

Renders the page size select with the current context.

public render( ): string
return string

The rendered HTML select element, or empty string if there are insufficient options.

                public function render(): string
{
    $context = $this->getContext();
    if (!is_array($context->constraint) || count($context->constraint) < 2) {
        return '';
    }
    $options = [];
    foreach ($context->constraint as $value) {
        $options[$value] = (string) $value;
    }
    $attributes = array_merge($this->attributes, [
        'data-default-page-size' => $context->defaultValue,
        'data-url-pattern' => $context->urlPattern,
        'data-default-url' => $context->defaultUrl,
        'onchange' => 'window.location.href = this.value == this.dataset.defaultPageSize ? this.dataset.defaultUrl : this.dataset.urlPattern.replace("' . PageSizeContext::URL_PLACEHOLDER . '", this.value)',
    ]);
    return Html::select()
        ->optionsData($options, encode: false)
        ->value($context->currentValue)
        ->attributes($attributes)
        ->render();
}

            
withContext() public method

Defined in: Yiisoft\Yii\DataView\PageSize\PageSizeContextTrait::withContext()

Creates a new instance with the specified page size context.

public withContext( Yiisoft\Yii\DataView\PageSize\PageSizeContext $context ): Yiisoft\Yii\DataView\PageSize\SelectPageSize
$context Yiisoft\Yii\DataView\PageSize\PageSizeContext

The page size context to use.

return Yiisoft\Yii\DataView\PageSize\SelectPageSize

New instance with the specified context.

                final public function withContext(PageSizeContext $context): static
{
    $new = clone $this;
    $new->context = $context;
    return $new;
}