簡體   English   中英

如何將Json用於dataProvider Yii2

[英]How to use Json for dataProvider Yii2

是否熱衷於使用json作為dataProvider在Gridview Yii2中顯示數據?

這是我的控制器(從Yii2生成器生成代碼)代碼:

<?php

namespace app\controllers;

use Yii;
use app\models\Users;
use app\models\UsersSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;

/**
 * UsersController implements the CRUD actions for Users model.
 */
class UsersController extends Controller
{
    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['POST'],
                ],
            ],
        ];
    }

    /**
     * Lists all Users models.
     * @return mixed
     */
    public function actionIndex()
    {
        $searchModel = new UsersSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

        return $this->render('index', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
        ]);
    }

    /**
     * Displays a single Users model.
     * @param integer $id
     * @return mixed
     */
    public function actionView($id)
    {
        return $this->render('view', [
            'model' => $this->findModel($id),
        ]);
    }

    /**
     * Creates a new Users model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate()
    {
        $model = new Users();

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['view', 'id' => $model->id]);
        } else {
            return $this->render('create', [
                'model' => $model,
            ]);
        }
    }

    /**
     * Updates an existing Users model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param integer $id
     * @return mixed
     */
    public function actionUpdate($id)
    {
        $model = $this->findModel($id);

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['view', 'id' => $model->id]);
        } else {
            return $this->render('update', [
                'model' => $model,
            ]);
        }
    }

    /**
     * Deletes an existing Users model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     * @param integer $id
     * @return mixed
     */
    public function actionDelete($id)
    {
        $this->findModel($id)->delete();

        return $this->redirect(['index']);
    }

    /**
     * Finds the Users model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param integer $id
     * @return Users the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id)
    {
        if (($model = Users::findOne($id)) !== null) {
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }
}

我在索引中的gridview是這樣的:

<?php

use yii\helpers\Html;
use kartik\grid\GridView;
use yii\widgets\ActiveForm;
use yii\widgets\Pjax;
use yii\web\JsExpression;

/* @var $this yii\web\View */
/* @var $searchModel app\models\SalesOrderSearch */
/* @var $dataProvider yii\data\ActiveDataProvider */


?>

<div class="start-stock-index">

    <div class="form-group">
        <?= Html::a('Create Customer', ['create'], ['class' => 'btn btn-success']) ?>
    </div>

            <?php Pjax::begin(); ?>
            <?= GridView::widget([
                'dataProvider'=>$dataProvider,
                'filterModel'=>$searchModel,
                'showPageSummary'=>true,
                'pjax'=>true,
                'striped'=>true,
                'hover'=>true,
                'responsiveWrap' => false,
                'panel'=>['type'=>'primary', 'heading'=>$partner_name],
                'columns'=>[
                    [
                        'attribute' => 'cust_name',
                        'value' => 'cust_name',
                        'label' => 'Name',
                        'contentOptions' =>
                            ['style'=>'max-width: 100px; font-size: 12px;overflow: auto; word-wrap: break-word;'],
                    ],
                    [
                        'attribute' => 'phone_number',
                        'value' => 'phone_number',
                        'label' => 'Phone Number',
                        'contentOptions' =>
                            ['style'=>'max-width: 100px; font-size: 12px;overflow: auto; word-wrap: break-word;'],
                    ],
                    [
                        'attribute' => 'age_type',
                        'value' => 'ageType.age_name',
                        'label' => 'Age Category',
                        'contentOptions' =>
                            ['style'=>'max-width: 100px; font-size: 12px;overflow: auto; word-wrap: break-word;'],
                    ],
                    ['class' => 'kartik\grid\ActionColumn','template' => '{update} {delete}',],
                ],
            ]); ?>
            <?php Pjax::end(); ?>
    </div>

<?php
$script =  <<< JS


JS;

$this->registerJs($script);

?>

我想要的是顯示來自json的數據,以便它可以是寧靜的頁面。 謝謝。

如果您的JSON結構適合arrayDataProvider,則可以將JSON數據轉換為關聯數組,請使用arrayDataprovider

您可以將輔助程序用作http://www.yiiframework.com/doc-2.0/yii-helpers-json.html

http://www.yiiframework.com/doc-2.0/yii-helpers-basejson.html#decode()-detail

Json::decode($yourJSON);  

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM