Yii-data-widgets

提供:Dev Guides
移動先:案内検索

Yii-データウィジェット

Yiiは、データを表示するための一連のウィジェットを提供します。 DetailViewウィジェットを使用して、単一のレコードを表示できます。 ListViewウィジェットとグリッドビューを使用して、フィルタリング、並べ替え、ページネーションなどの機能を備えたレコードのテーブルを表示できます。

DBの準備

  • ステップ1 *-新しいデータベースを作成します。 データベースは、次の2つの方法で準備できます。
  • ターミナルで_mysql -u root –p_を実行します
  • CREATE DATABASE helloworld CHARACTER SET utf8 COLLATE utf8_general_ci;を介して新しいデータベースを作成します。

ステップ2 *- *config/db.php ファイルでデータベース接続を設定します。 次の構成は、現在使用されているシステム用です。

<?php
   return [
      'class' => 'yii\db\Connection',
      'dsn' => 'mysql:host=localhost;dbname=helloworld',
      'username' => 'vladimir',
      'password' => '12345',
      'charset' => 'utf8',
   ];
?>

ステップ3 *-ルートフォルダー内 *run。/yii migrate/create test_table 。 このコマンドは、DBを管理するためのデータベース移行を作成します。 移行ファイルは、プロジェクトルートの migrations フォルダーに表示されます。

ステップ4 *-この方法で移行ファイル(この場合は *m160106_163154_test_table.php )を変更します。

<?php
   use yii\db\Schema;
   use yii\db\Migration;
   class m160106_163154_test_table extends Migration {
      public function safeUp() {
         $this->createTable("user", [
            "id" => Schema::TYPE_PK,
            "name" => Schema::TYPE_STRING,
            "email" => Schema::TYPE_STRING,
         ]);
         $this->batchInsert("user", ["name", "email"], [
            ["User1", "[email protected]"],
            ["User2", "[email protected]"],
            ["User3", "[email protected]"],
            ["User4", "[email protected]"],
            ["User5", "[email protected]"],
            ["User6", "[email protected]"],
            ["User7", "[email protected]"],
            ["User8", "[email protected]"],
            ["User9", "[email protected]"],
            ["User10", "[email protected]"],
            ["User11", "[email protected]"],
         ]);
      }
      public function safeDown() {
         $this->dropTable('user');
      }
   }
?>

上記の移行では、id、name、およびemailのフィールドを持つ user テーブルが作成されます。 また、いくつかのデモユーザーを追加します。

ステップ5 *-プロジェクトルート *run。/yii migrate 内で、データベースに移行を適用します。

ステップ6 *-次に、 *user テーブルのモデルを作成する必要があります。 簡単にするために、 Gii コード生成ツールを使用します。 この url:http://localhost:8080/index.php?r = gii を開きます。 次に、「Model generator」ヘッダーの下にある「Start」ボタンをクリックします。 テーブル名(「ユーザー」)とモデルクラス(「MyUser」)を入力し、「プレビュー」ボタンをクリックして、最後に「生成」ボタンをクリックします。

データウィジェット準備DB

MyUserモデルがモデルディレクトリに表示されます。

DetailViewウィジェット

  • DetailViewウィジェット*は、単一のモデルのデータを表示します。 $ attributes プロパティは、表示するモデル属性を定義します。

ステップ1 *- *actionDataWidget メソッドを SiteController に追加します。

public function actionDataWidget() {
   $model = MyUser::find()->one();
   return $this->render('datawidget', [
      'model' => $model
   ]);
}

上記のコードでは、最初のMyUserモデルを見つけ、それを datawidget ビューに渡します。

ステップ2 *- *views/site フォルダー内に datawidget.php というファイルを作成します。

<?php
   use yii\widgets\DetailView;
   echo DetailView::widget([
      'model' => $model,
      'attributes' => [
         'id',
        //formatted as html
         'name:html',
         [
            'label' => 'e-mail',
            'value' => $model->email,
         ],
      ],
   ]);
?>

ステップ3 *- *http://localhost:8080/index.php?r = site/data-widget にアクセスすると、 DetailView ウィジェットの一般的な使用方法が表示されます。

DetailViewウィジェット