薇尔技术/rest-grid

针对 Yii2:此扩展提供了类似于 GridView 的功能,但适用于 REST。

安装次数: 8,599

依赖项: 0

建议者: 0

安全性: 0

星星: 4

关注者: 4

分支: 1

开放性问题: 0

类型:yii2-extension

1.0.3 2018-04-02 18:10 UTC

This package is not auto-updated.

Last update: 2024-09-19 00:21:55 UTC


README

此扩展提供了类似于 GridView 的功能,但适用于 REST。

有关许可证信息,请参阅 LICENSE 文件。

Latest Stable Version Total Downloads Build Status

安装

安装此扩展的最佳方式是通过 composer

运行以下命令之一:

php composer.phar require --prefer-dist vivre-tech/rest-grid

或将其添加到 composer.json 的 require 部分:

"vivre-tech/rest-grid": "*"

用法

示例

$arrayRows = [];
foreach(range(1, 100) as $item) {
    $arrayRows[] = [
        'id' => $item,
        'name' => 'Product ' . $item,
        'price' => 100 + $item,
        'created_at' => date('Y-m-d H:i:s')
    ];
}

$dataProvider = new \yii\data\ArrayDataProvider([
    'allModels' => $arrayRows,
    'pagination' => [
        'pageSize' => 5,
    ],
]);


$grid = new \vivretech\rest\grid\Grid([
    'dataProvider' => $dataProvider,
    'columns' => [
        [
            'attribute' => 'id'
        ],
        [
            'attribute' => 'name'
        ],
        [
            'attribute' => 'price'
        ],
        [
            'attribute' => 'created_at'
        ]
    ]
]);

return $grid->run();

响应

{
    "metadata": {
        "id": "aa899d9e6e420821e20f04ea857e0c2d_0",
        "caption": null,
        "description": null,
        "options": [],
        "header": {
            "show": true
        },
        "filters": {
            "show": true
        },
        "footer": {
            "show": false
        },
        "request_params": {
            "pager": {
                "param": "page",
                "size": "per-page"
            },
            "sorter": {
                "param": "sort",
                "separator": ",",
                "multi_sort": false
            }
        }
    },
    "pager": {
        "results": {
            "total": 1212,
            "per_page": 5
        },
        "pages": {
            "total": 243,
            "current": 1
        }
    },
    "columns": [
        {
            "label": "Id",
            "attribute": "id",
            "description": null,
            "options": [],
            "sortable": true,
            "filterable": false,
            "header": {
                "value": [],
                "options": []
            },
            "filter": {
                "selected": null,
                "items": [],
                "options": []
            },
            "footer": {
                "value": [],
                "options": []
            }
        },
        {
            "label": "Name",
            "attribute": "name",
            "description": null,
            "options": [],
            "sortable": true,
            "filterable": false,
            "header": {
                "value": [],
                "options": []
            },
            "filter": {
                "selected": null,
                "items": [],
                "options": []
            },
            "footer": {
                "value": [],
                "options": []
            }
        },
        {
            "label": "Price",
            "attribute": "price",
            "description": null,
            "options": [],
            "sortable": true,
            "filterable": false,
            "header": {
                "value": [],
                "options": []
            },
            "filter": {
                "selected": null,
                "items": [],
                "options": []
            },
            "footer": {
                "value": [],
                "options": []
            }
        },
        {
            "label": "Created At",
            "attribute": "created_at",
            "description": null,
            "options": [],
            "sortable": true,
            "filterable": false,
            "header": {
                "value": [],
                "options": []
            },
            "filter": {
                "selected": null,
                "items": [],
                "options": []
            },
            "footer": {
                "value": [],
                "options": []
            }
        }
    ],
    "items": [
        {
            "id": 1,
            "name": "Product 1",
            "price": 101,
            "created_at": "2018-04-02 17:54:52"
        },
        {
            "id": 2,
            "name": "Product 2",
            "price": 102,
            "created_at": "2018-04-02 17:54:52"
        },
        {
            "id": 3,
            "name": "Product 3",
            "price": 103,
            "created_at": "2018-04-02 17:54:52"
        },
        {
            "id": 4,
            "name": "Product 4",
            "price": 104,
            "created_at": "2018-04-02 17:54:52"
        },
        {
            "id": 5,
            "name": "Product 5",
            "price": 105,
            "created_at": "2018-04-02 17:54:52"
        }
    ]
}

单元测试

如果您在开发环境中运行以下命令: composer install,那么您将在 /vendor/bin/phpunit 中找到 phpunit

如果通过 composer install 命令没有安装 phpunit,请按照以下步骤操作

  1. 在控制台/终端中运行 brew install phpunit

要测试,在项目的 root 目录下,根据 phpunit 的安装方式,您有两个选择来运行:

  1. 通过 composer install 命令安装,您需要在控制台/终端中执行: vendor/bin/phpunit
  2. 通过 brew 安装,您需要在控制台/终端中执行: phpunit