pchouse/php-tabulator

一个库,用于使用 PHP 类和属性的属性来创建 Tabulator JavaScript 库的基本配置

1.0.0-BETA-4 2024-05-10 11:10 UTC

This package is auto-updated.

Last update: 2024-09-10 12:03:18 UTC


README

PHP json 选项生成器用于 Tabulator.js

安装

composer require pchouse/php-tabulator

此库用于生成 Tabulator 的 json 选项,使用 PHP 对象类中声明的元数据,使用类和属性中的 PHP 属性。

使用方法

对象

<?php
declare(strict_types=1);

namespace PChouse\Resources;

use PChouse\Tabulator\Column\ColumnDefinition;
use PChouse\Tabulator\Column\ColumnDefinitionAlign;
use PChouse\Tabulator\Column\Layout;
use PChouse\Tabulator\Column\Resizable;
use PChouse\Tabulator\Index;
use PChouse\Tabulator\KeyBinding;
use PChouse\Tabulator\Options;
use PChouse\Tabulator\Sorter\SortDirection;
use PChouse\Tabulator\Sorter\Sorter;
use PChouse\Tabulator\Sorter\SortMode;

#[Options(
    sortMode: SortMode::LOCAL,
    filterMode: SortMode::LOCAL,
    layout: Layout::FIT_COLUMNS,
    height: 500,
    placeholder: "Sem registos",
)]
#[KeyBinding]
class Tabulator
{

    #[Index]
    #[ColumnDefinition(
        visible: true
    )]
    private int $id;

    #[Sorter(SortDirection::DESC)]
    #[ColumnDefinition(
        width: 100,
        hozAlign: ColumnDefinitionAlign::LEFT,
        resizable: Resizable::HEADER,
        frozen: true,
        sorter: \PChouse\Tabulator\Column\Sorter::ALPHA_NUM
    )]
    private string $name;

    #[Sorter(SortDirection::ASC)]
    #[ColumnDefinition(
        hozAlign: ColumnDefinitionAlign::LEFT,
        resizable: Resizable::HEADER,
        sorter: \PChouse\Tabulator\Column\Sorter::ALPHA_NUM
    )]
    private string $surname;

    #[Sorter]
    #[ColumnDefinition(
        hozAlign: ColumnDefinitionAlign::RIGHT,
        resizable: Resizable::HEADER,
        frozen: true,
        sorter: \PChouse\Tabulator\Column\Sorter::ALPHA_NUM
    )]
    private int $age;

    #[ColumnDefinition(
        hozAlign: ColumnDefinitionAlign::CENTER,
        resizable: Resizable::HEADER,
        sorter: \PChouse\Tabulator\Column\Sorter::STRING
    )]
    private string $nationality;

    public function __construct()
    {
    }

    /**
     * @return int
     */
    public function getId(): int
    {
        return $this->id;
    }

    /**
     * @param int $id
     *
     * @return Tabulator
     */
    public function setId(int $id): Tabulator
    {
        $this->id = $id;
        return $this;
    }

    /**
     * @return string
     */
    public function getName(): string
    {
        return $this->name;
    }

    /**
     * @param string $name
     *
     * @return Tabulator
     */
    public function setName(string $name): Tabulator
    {
        $this->name = $name;
        return $this;
    }

    /**
     * @return string
     */
    public function getSurname(): string
    {
        return $this->surname;
    }

    /**
     * @param string $surname
     *
     * @return Tabulator
     */
    public function setSurname(string $surname): Tabulator
    {
        $this->surname = $surname;
        return $this;
    }

    /**
     * @return int
     */
    public function getAge(): int
    {
        return $this->age;
    }

    /**
     * @param int $age
     *
     * @return Tabulator
     */
    public function setAge(int $age): Tabulator
    {
        $this->age = $age;
        return $this;
    }

    /**
     * @return string
     */
    public function getNationality(): string
    {
        return $this->nationality;
    }

    /**
     * @param string $nationality
     *
     * @return Tabulator
     */
    public function setNationality(string $nationality): Tabulator
    {
        $this->nationality = $nationality;
        return $this;
    }
}

在 HTML/javascript/typescript 中

<html lang="en">
<head>
    <link href="https://unpkg.com/tabulator-tables/dist/css/tabulator.min.css" rel="stylesheet">
    <script type="text/javascript" src="https://unpkg.com/tabulator-tables/dist/js/tabulator.min.js"></script>
    <title></title>
</head>
<body>

<div id="tabulator"></div>

<script type="text/javascript">
    window.onload = async function(){

        let fetchOptions = await fetch("https://:8080/TabulatorOptions.php");
        let options = await fetchOptions.json();

        // set more options if needed
        // options.data = [];

        new Tabulator("#tabulator", options);
    };
</script>

</body>
</html>

处理 tabulator 选项请求

<?php
require_once "../../../vendor/autoload.php";

try {
    $options = \PChouse\Tabulator\Options::parse(
        new \ReflectionClass(\PChouse\Resources\Tabulator::class),
        new \PChouse\Resources\Translator()
    );

    if ($options instanceof \PChouse\Tabulator\Options) {
		// Set options programmatically
		// $options->set????  
	}
    
    $json = $options->toJson();

    \header("Content-Type: application/json; charset=UTF-8");
    echo $json;
} catch (\Throwable $e) {
    echo $e->getMessage();
}

许可

版权所有 2023 Reflexão, Sistemas e Estudos Informáticos, Lda

特此授予任何人获取此软件及其相关文档文件(“软件”)副本(以下简称“软件”)的自由,包括但不限于使用、复制、修改、合并、发布、分发、再许可和/或销售软件副本的权利,并允许向软件提供的人这样做,前提是遵守以下条件

上述版权声明和本许可声明应包含在软件的所有副本或主要部分中。

软件按“原样”提供,不提供任何明示或暗示的保证,包括但不限于对适销性、特定用途适用性和非侵权的保证。在任何情况下,作者或版权所有者均不对任何索赔、损害或其他责任承担责任,无论是基于合同、侵权或其他行为,无论源于、因之或与此软件或其使用或其他交易有关。

由 Joseon Chinnampo 灵魂为聪明人制作的艺术品 🇰🇷