xililo/searchable-ajax-select

Searchable Ajax Select Widget for Yii2

Installs: 11

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 1

Forks: 0

Open Issues: 0

Language:JavaScript

Type:yii2-extension

v1.0.0 2023-06-30 19:53 UTC

This package is auto-updated.

Last update: 2024-08-30 01:18:08 UTC


README

Searchable Ajax Select is a Yii2 widget that provides a searchable select input with the capability to make AJAX calls to load search options dynamically.

Installation

You can install the package via Composer by running the following command:

Usage

To use the Searchable Ajax Select widget in your Yii2 application, follow these steps:

composer require xililo/searchable-ajax-select
  1. In your form view, use the widget::

     use xililo\SearchableAjaxSelect\SearchableAjaxSelect;
     use yii\helpers\Url;
    
     <?= $form->field($model, 'attribute')->widget(SearchableAjaxSelect::class, [
         'ajaxURL' => Url::to(['controller/user-list']), // Replace with your actual AJAX URL
         'placeholder' => 'Search for options...',
         'minimumInputLength' => 3,
     ]) ?>

    Customize the ajaxURL, placeholder, and minimumInputLength options as needed.

  2. In your controller:

     public function actionUserList($q = null, $id = null) {
         \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
         $out = ['results' => ['id' => '', 'text' => '']];
         if (!is_null($q)) {
             $query = new Query;
             $query->select('id, name AS text')
                 ->from('city')
                 ->where(['like', 'name', $q])
                 ->limit(20);
             $command = $query->createCommand();
             $data = $command->queryAll();
             $out['results'] = array_values($data);
         }
         elseif ($id > 0) {
             $item = City::findOne($id);
             $name = ($item !=null) $item->name : 'null';
             $out['results'] = ['id' => $id, 'text' => $name];
         }
         return $out;
     }

Requirements

  • PHP >= 5.4
  • Yii2 Framework

License

This package is open source software licensed under the MIT License.