mrssoft/yii2-link-relative

Installs: 17

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 2

Forks: 1

Open Issues: 0

Type:yii2-extension

1.0.1 2018-09-05 07:14 UTC

This package is auto-updated.

Last update: 2024-04-08 14:57:05 UTC


README

Installation

The preferred way to install this extension is through composer.

Either run

php composer.phar require --prefer-dist mrssoft/yii2-link-relative "*"

or add

"mrssoft/yii2-link-relative": "*"

to the require section of your composer.json file.

Usage

Виджет:

    echo LinkRelativeWidget::widget([
        'model' => $model, //Базовая модель
        'relationName' => 'items', //Название отношения (конечного)
        'attributeTitle' => 'title', //Атрибут для вывода в таблице
        'viaToElementName' => 'item',
        'ajaxUrl' => ['item/search'], //Ссылка на поиск элементов
        'placeholder' => 'Поиск товара...',
        'name' => 'Товар' //Название элементов в таблице
    ]);

Поведение базовой модели:

    public function behaviors()
    {
        return [
            [
                'class' => SaveRelationBehavior::class,
                'relationName' => 'items'
            ],
        ];
    }

Действие для поиска элементов:

    public function actionSearch($search = null)
    {
        $out = ['results' => ['id' => '', 'text' => '']];

        if ($search !== null) {
            $data = Item::find()
                        ->select(['CONCAT(`type`, " ", `name`) as text', 'id'])
                        ->andWhere(['like', 'name', $search])
                        ->orWhere(['like', 'type', $search])
                        ->orderBy('text')
                        ->limit(20)
                        ->asArray()
                        ->all();
            $out['results'] = array_values($data);
        }

        Yii::$app->response->format = Response::FORMAT_JSON;
        return $out;
    }