/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = TbKpi::find(); $dataProvider = new ActiveDataProvider(['query' => $query]); $this->load($params); if (!$this->validate()) { // uncomment the following line if you do not want to return any records when validation fails // $query->where('0=1'); return $dataProvider; } $query->andFilterWhere(['id' => $this->id]); $query->andFilterWhere(['like', 'topic', $this->topic])->andFilterWhere(['like', 'note', $this->note]); return $dataProvider; }
use yii\helpers\ArrayHelper; use app\models\TbKpi; use app\models\Cchangwat; /* @var $this yii\web\View */ /* @var $model app\models\Work */ /* @var $form yii\widgets\ActiveForm */ ?> <div class="work-form"> <?php $form = ActiveForm::begin(); ?> <?php $items = ArrayHelper::map(TbKpi::find()->all(), 'id', 'topic'); ?> <?php echo $form->field($model, 'kpi')->dropDownList($items, ['prompt' => '-- ตัวชี้วัด --']); ?> <?php $items = ArrayHelper::map(Cchangwat::find()->all(), 'code', 'name'); ?> <?php echo $form->field($model, 'prov')->dropDownList($items, ['prompt' => '-- จังหวัด --']); ?> <?php echo $form->field($model, 'target')->textInput(); ?>
<?php use yii\grid\GridView; use yii\helpers\Html; use app\models\Cchangwat; use app\models\TbKpi; ?> <div class="work-index"> <br> <p> <?php echo Html::a('เพิ่ม', ['create'], ['class' => 'btn btn-success']); ?> </p> <?php echo GridView::widget(['dataProvider' => $dataProvider, 'columns' => [['class' => 'yii\\grid\\SerialColumn'], ['attribute' => 'kpi', 'value' => function ($model) { $code = $model->kpi; $data = TbKpi::find()->where(['id' => $code])->one(); return $code . "-" . $data->topic; }], ['attribute' => 'prov', 'value' => function ($model) { $code = $model->prov; $data = Cchangwat::find()->where(['code' => $code])->one(); return $code . "-" . $data->name; }], 'target', 'result', ['label' => 'ร้อยละ', 'value' => function ($model) { return number_format($model->result / $model->target * 100, 2); }], ['class' => 'yii\\grid\\ActionColumn']]]); ?> </div>
/** * Finds the TbKpi model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param integer $id * @return TbKpi the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { if (($model = TbKpi::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }