Skip to content

外部数据源

外部数据源model

创建一个model(要实现app\model\ExternalData抽象类)

外部数据源无法编辑

php
<?php namespace app\model;
use app\model\ExternalData;

class Testorder extends ExternalData
{
    public function paginate($page = 1, $perPage = 10)
    {
        return [
            ["id" => 100],
            ["id" => 300],
        ];
    }

    public function count()
    {
        return 20;
    }

    /*详情页使用*/
    public function find($id)
    {
        if ($id == 100) {
            $obj = ["id" => 100, "text" => "1000"];
        } else {
            $obj = ["id" => 300, "text" => "3000"];
        }

        return $obj;
    }

}

table函数

在Controller中table函数

php
//第三个参数表示是否为外部数据源model
 $table = new Grid(new $this->model(),null,true);
        $table->column("id", "ID");
        //关闭分页功能
        $table->disablePagination();

        return $table;

detail函数

在Controller中detail函数(详情页使用)

php
//第三个参数表示是否为外部数据源model
  $show = new Show(new $this->model(), $id, true);
$show->field('id', 'ID');
$show->field('text', 'text');
return $show;

新基类(导出功能,性能优化)

newadmin/vendor/adminbase/model/NewExternalData.php

example

php
<?php namespace LinAdmin\example\Model;

use LinAdmin\model\NewExternalData;

class ExternalMock extends NewExternalData
{
    /**
     * 获取外部数据(导入时使用)
     * @return array
     */
    public function getExtData()
    {
        $data = [];
        for ($i = 1; $i <= 20; $i++) {
            $data[] = ["id" => $i, "text" => rand(100, 1000)];
        }
        return $data;
    }


}