Yii2 GridView多维数组

时间:2015-09-04 13:58:02

标签: php arrays gridview yii2

我有阵列:

from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait

wd = webdriver.Chrome(executable_path="your/path/to/chromedriver")

# Access website

wait = WebDriverWait(wd, 10)
confirm = wait.until(EC.element_to_be_clickable((By.XPATH, "//span[.='Confirm Sale']")))
confirm.click()

现在我想在GridView中显示这个以获得这样的表:

123 => [
    "2015-09-01" => ["sum"=>"1030","count"=>"4"],
    "2015-09-02" => ["sum"=>"10","count"=>"24"],
    "2015-09-03" => ["sum"=>"120","count"=>"34"],
    "2015-09-04" => ["sum"=>"200","count"=>"45"]
    ],
124 => [
    "2015-09-01" => ["sum"=>"132","count"=>"48"],
    "2015-09-02" => ["sum"=>"10","count"=>"24"],
    "2015-09-03" => ["sum"=>"120","count"=>"34"],
    "2015-09-04" => ["sum"=>"200","count"=>"45"]
    ],

如何获得这样的效果?

1 个答案:

答案 0 :(得分:1)

对于带有编号行的2d数组,每行包含一个元素数组,以下实现非常简单。

<强> 1。初始化数据提供者

首先通过传递allModels参数中的数据来初始化控制器中的数组数据提供程序。可以找到ArrayDataProvider的模式详细信息here

$dataProvider = new ArrayDataProvider([
            'allModels'=> $your2darray,
            'pagination' => [
                'pageSize' => 10,
            ],
        ]);

<强> 2。显示网格

假设您已经在视图中传递了上述数据提供者变量,请在网格中使用它,如下所示,指定要显示的列。

<?= GridView::widget([
        'dataProvider' => $dataProvider,
        'columns' => [
            ['class' => 'yii\grid\SerialColumn'],

            'id',
            'field1', 
             ...
            ['class' => 'yii\grid\ActionColumn'],
        ],
    ]); ?>