项目作者: itstructure

项目描述 :
Detail view table for laravel framework
高级语言: PHP
项目地址: git://github.com/itstructure/laravel-detail-view.git
创建时间: 2021-04-24T11:53:21Z
项目社区:https://github.com/itstructure/laravel-detail-view

开源协议:MIT License

下载


Laravel Detail View

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

Introduction

This package is to displaying the model data in a Detail table.

Detail view appearance

Requirements

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

Installation

General from remote packagist repository

Run the composer command:

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

If you are testing this package from a local server directory

In application composer.json file set the repository, as in example:

  1. "repositories": [
  2. {
  3. "type": "path",
  4. "url": "../laravel-detail-view",
  5. "options": {
  6. "symlink": true
  7. }
  8. }
  9. ],

Here,

../laravel-detail-view - directory path, which has the same directory level as application and contains Detail View package.

Then run command:

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

Publish files (Not necessary)

  • To publish views run command:

    php artisan detail_view:publish --only=views

    It stores view files to resources/views/vendor/detail_view folder.

  • To publish translations run command:

    php artisan detail_view:publish --only=lang

    It stores translation files to resources/lang/vendor/detail_view folder.

  • To publish all parts run command without only argument:

    php artisan detail_view:publish

    Else you can use --force argument to rewrite already published files.

Else you can use --force argument to rewrite already published file.

Usage

View template part

Use @detailView() directive with config array in a blade view template.

Simple quick usage

You can simply set rows to display as string format in rowFields array.

Note: $model must be instance of Illuminate\Database\Eloquent\Model.

  1. @php
  2. $detailData = [
  3. 'model' => $model,
  4. 'title' => 'Detail table',
  5. 'rowFields' => [
  6. 'id',
  7. 'active',
  8. 'icon',
  9. 'created_at'
  10. ]
  11. ];
  12. @endphp
  1. @detailView($detailData)

Alternative variant without a blade directive:

  1. {!! detail_view([
  2. 'model' => $model,
  3. 'title' => 'Detail table',
  4. 'rowFields' => [
  5. 'id',
  6. 'active',
  7. 'icon',
  8. 'created_at'
  9. ]
  10. ]) !!}

Setting custom options

Rows

Simple example:

  1. @detailView([
  2. 'model' => $model,
  3. 'rowFields' => [
  4. [
  5. 'label' => 'First Name', // Row label.
  6. 'attribute' => 'first_name', // Attribute, by which the row data will be taken from a model.
  7. ],
  8. [
  9. 'label' => 'Last Name',
  10. 'value' => function ($model) {
  11. return $model->last_name;
  12. }
  13. ],
  14. ]
  15. ])
Formatters

There are the next formatter keys:

  • html - is for passing a row content with html tags.
  • image - is for inserting a row data in to src attribute of <img> tag.
  • text - applies strip_tags() for a row data.
  • url - is for inserting a row data in to href attribute of <a> tag.

For that keys there are the next formatters:

  • HtmlFormatter
  • ImageFormatter
  • TextFormatter
  • UrlFormatter

Also you can set formatter with some addition options. See the next simple example:

  1. @detailView([
  2. 'model' => $model,
  3. 'rowFields' => [
  4. [
  5. 'attribute' => 'url',
  6. 'format' => [
  7. 'class' => Itstructure\DetailView\Formatters\UrlFormatter::class,
  8. 'title' => 'Source',
  9. 'htmlAttributes' => [
  10. 'target' => '_blank'
  11. ]
  12. ]
  13. ],
  14. [
  15. 'attribute' => 'content',
  16. 'format' => 'html'
  17. ]
  18. ]
  19. ])
Table heads

To set column titles, you can set captionColumnConfig and valueColumnConfig as in example:

  1. @detailView([
  2. 'model' => $model,
  3. 'captionColumnConfig' => [
  4. 'label' => 'Custom title column',
  5. 'htmlAttributes' => [
  6. 'class' => 'th-title-class'
  7. ]
  8. ],
  9. 'valueColumnConfig' => [
  10. 'label' => 'Custom value column',
  11. 'htmlAttributes' => [
  12. 'class' => 'th-value-class'
  13. ]
  14. ],
  15. 'rowFields' => [
  16. [
  17. 'attribute' => 'content',
  18. ]
  19. ]
  20. ])

To hide all table row with head titles:

  1. @detailView([
  2. 'model' => $model,
  3. 'showHead' => false,
  4. 'rowFields' => [
  5. [
  6. 'attribute' => 'content',
  7. ]
  8. ]
  9. ])
Complex extended example
  1. @php
  2. $detailData = [
  3. 'model' => $model,
  4. 'title' => 'Detail title', // It can be empty ''
  5. 'htmlAttributes' => [
  6. 'class' => 'table table-bordered table-striped'
  7. ],
  8. 'captionColumnConfig' => [
  9. 'label' => 'Custom title column',
  10. 'htmlAttributes' => [
  11. 'class' => 'th-title-class'
  12. ]
  13. ],
  14. 'valueColumnConfig' => [
  15. 'label' => 'Custom value column',
  16. 'htmlAttributes' => [
  17. 'class' => 'th-value-class'
  18. ]
  19. ],
  20. 'rowFields' => [
  21. [
  22. 'attribute' => 'id', // REQUIRED if value is not defined. Attribute name to get row model data.
  23. 'label' => 'ID', // Row label.
  24. 'htmlAttributes' => [
  25. 'class' => 'tr-class'
  26. ]
  27. ],
  28. [
  29. 'label' => 'Active', // Row label.
  30. 'value' => function ($model) { // You can set 'value' as a callback function to get a row data value dynamically.
  31. return '<span class="icon fas '.($model->active == 1 ? 'fa-check' : 'fa-times').'"></span>';
  32. },
  33. 'format' => 'html', // To render row content without lossless of html tags, set 'html' formatter.
  34. ],
  35. [
  36. 'label' => 'Url link', // Row label.
  37. 'attribute' => 'url', // REQUIRED if value is not defined. Attribute name to get row model data.
  38. 'format' => [ // Set special formatter. $model->{$this->attribute} will be inserted in to 'href' attribute of <a> tag.
  39. 'class' => Itstructure\DetailView\Formatters\UrlFormatter::class, // REQUIRED. For this case it is necessary to set 'class'.
  40. 'title' => 'Source', // title between a tags.
  41. 'htmlAttributes' => [ // Html attributes for <a> tag.
  42. 'target' => '_blank'
  43. ]
  44. ]
  45. ],
  46. [
  47. 'label' => 'Icon', // Row label.
  48. 'value' => function ($model) { // You can set 'value' as a callback function to get a row data value dynamically.
  49. return $model->icon;
  50. },
  51. 'format' => [ // Set special formatter. If $model->icon value is a url to image, it will be inserted in to 'src' attribute of <img> tag.
  52. 'class' => Itstructure\DetailView\Formatters\ImageFormatter::class, // REQUIRED. For this case it is necessary to set 'class'.
  53. 'htmlAttributes' => [ // Html attributes for <img> tag.
  54. 'width' => '100'
  55. ]
  56. ]
  57. ],
  58. 'created_at', // Simple row setting by string.
  59. ]
  60. ];
  61. @endphp
  1. @detailView($detailData)

License

Copyright © 2021-2025 Andrey Girnik girnikandrey@gmail.com.

Licensed under the MIT license. See LICENSE.txt for details.