itstructure/laravel-detail-view

laravel框架的详情视图表格

1.0.5 2024-03-27 01:34 UTC

This package is auto-updated.

Last update: 2024-09-27 02:36:39 UTC


README

Latest Stable Version Latest Unstable Version License Total Downloads Build Status Scrutinizer Code Quality

简介

此包用于以详情表的形式显示模型数据。

Detail view appearance

要求

  • laravel 5.5+ | 6+ | 7+ | 8+ | 9+ | 10+ | 11+
  • php >= 7.1
  • composer

安装

从远程Packagist仓库进行一般安装

运行composer命令

composer require itstructure/laravel-detail-view "~1.0.5"

如果您正在本地服务器目录中测试此包

在应用程序 composer.json 文件中设置仓库,如下所示

"repositories": [
    {
        "type": "path",
        "url": "../laravel-detail-view",
        "options": {
            "symlink": true
        }
    }
],

这里,

../laravel-detail-view - 目录路径,与应用程序处于同一目录级别,并包含详情视图包。

然后运行命令

composer require itstructure/laravel-detail-view:dev-main --prefer-source

发布文件(非必需)

  • 要发布视图,运行命令

    php artisan detail_view:publish --only=views

    它将视图文件存储到 resources/views/vendor/detail_view 文件夹。

  • 要发布翻译,运行命令

    php artisan detail_view:publish --only=lang

    它将翻译文件存储到 resources/lang/vendor/detail_view 文件夹。

  • 要发布所有部分,运行不带 only 参数的命令

    php artisan detail_view:publish

    否则,您可以使用 --force 参数重写已发布的文件。

否则,您可以使用 --force 参数重写已发布的文件。

用法

视图模板部分

在blade视图模板中使用 @detailView() 指令与配置数组。

简单快速使用

您可以将要显示的行设置为 字符串 格式在 rowFields 数组中。

注意: $model 必须是 Illuminate\Database\Eloquent\Model 的实例。

@php
$detailData = [
    'model' => $model,
    'title' => 'Detail table',
    'rowFields' => [
        'id',
        'active',
        'icon',
        'created_at'
    ]
];
@endphp
@detailView($detailData)

不带blade指令的替代变体

{!! detail_view([
    'model' => $model,
    'title' => 'Detail table',
    'rowFields' => [
        'id',
        'active',
        'icon',
        'created_at'
    ]
]) !!}

设置自定义选项

简单示例

@detailView([
    'model' => $model,
    'rowFields' => [
        [
            'label' => 'First Name', // Row label.
            'attribute' => 'first_name', // Attribute, by which the row data will be taken from a model.
        ],
        [
            'label' => 'Last Name',
            'value' => function ($model) {
                return $model->last_name;
            }
        ],
    ]
])
格式化器

以下是一些格式化器键

  • html - 用于传递带有HTML标签的行内容。
  • image - 用于将行数据插入到 <img> 标签的 src 属性中。
  • text - 对行数据应用 strip_tags()
  • url - 用于将行数据插入到 <a> 标签的 href 属性中。

对于这些键,以下是一些格式化器

  • HtmlFormatter
  • ImageFormatter
  • TextFormatter
  • UrlFormatter

您还可以设置带有一些附加选项的格式化器。请参阅下面的简单示例

@detailView([
    'model' => $model,
    'rowFields' => [
        [
            'attribute' => 'url',
            'format' => [
                'class' => Itstructure\DetailView\Formatters\UrlFormatter::class,
                'title' => 'Source',
                'htmlAttributes' => [
                    'target' => '_blank'
                ]
            ]
        ],
        [
            'attribute' => 'content',
            'format' => 'html'
        ]
    ]
])
表头

要设置列标题,您可以将 captionColumnConfigvalueColumnConfig 设置为示例中的配置

@detailView([
    'model' => $model,
    'captionColumnConfig' => [
        'label' => 'Custom title column',
        'htmlAttributes' => [
            'class' => 'th-title-class'
        ]
    ],
    'valueColumnConfig' => [
        'label' => 'Custom value column',
        'htmlAttributes' => [
            'class' => 'th-value-class'
        ]
    ],
    'rowFields' => [
        [
            'attribute' => 'content',
        ]
    ]
])

要隐藏所有带有标题的表格行

@detailView([
    'model' => $model,
    'showHead' => false,
    'rowFields' => [
        [
            'attribute' => 'content',
        ]
    ]
])
复杂扩展示例
@php
$detailData = [
    'model' => $model,
    'title' => 'Detail title', // It can be empty ''
    'htmlAttributes' => [
        'class' => 'table table-bordered table-striped'
    ],
    'captionColumnConfig' => [
        'label' => 'Custom title column',
        'htmlAttributes' => [
            'class' => 'th-title-class'
        ]
    ],
    'valueColumnConfig' => [
        'label' => 'Custom value column',
        'htmlAttributes' => [
            'class' => 'th-value-class'
        ]
    ],
    'rowFields' => [
        [
            'attribute' => 'id', // REQUIRED if value is not defined. Attribute name to get row model data.
            'label' => 'ID', // Row label.
            'htmlAttributes' => [
                'class' => 'tr-class'
            ]
        ],
        [
            'label' => 'Active', // Row label.
            'value' => function ($model) { // You can set 'value' as a callback function to get a row data value dynamically.
                return '<span class="icon fas '.($model->active == 1 ? 'fa-check' : 'fa-times').'"></span>';
            },
            'format' => 'html', // To render row content without lossless of html tags, set 'html' formatter.
        ],
        [
            'label' => 'Url link', // Row label.
            'attribute' => 'url', // REQUIRED if value is not defined. Attribute name to get row model data.
            'format' => [ // Set special formatter. $model->{$this->attribute} will be inserted in to 'href' attribute of <a> tag.
                'class' => Itstructure\DetailView\Formatters\UrlFormatter::class, // REQUIRED. For this case it is necessary to set 'class'.
                'title' => 'Source', // title between a tags.
                'htmlAttributes' => [ // Html attributes for <a> tag.
                    'target' => '_blank'
                ]
            ]
        ],
        [
            'label' => 'Icon', // Row label.
            'value' => function ($model) { // You can set 'value' as a callback function to get a row data value dynamically.
                return $model->icon;
            },
            'format' => [ // Set special formatter. If $model->icon value is a url to image, it will be inserted in to 'src' attribute of <img> tag.
                'class' => Itstructure\DetailView\Formatters\ImageFormatter::class, // REQUIRED. For this case it is necessary to set 'class'.
                'htmlAttributes' => [ // Html attributes for <img> tag.
                    'width' => '100'
                ]
            ]
        ],
        'created_at', // Simple row setting by string.
    ]
];
@endphp
@detailView($detailData)

许可证

版权所有 © 2021-2024 Andrey Girnik girnikandrey@gmail.com

MIT许可证 下发布。有关详细信息,请参阅 LICENSE.txt。