jqh 5 gadi atpakaļ
vecāks
revīzija
188bf86bc4

+ 1 - 1
config/admin.php

@@ -291,7 +291,7 @@ return [
     |    "skin-blue-light", "skin-black", "skin-black-light".
     |
     */
-    'skin' => 'skin-blue-light',
+    'skin' => 'skin-black',
 
     /*
     |--------------------------------------------------------------------------

+ 75 - 0
resources/views/form/listfield.blade.php

@@ -0,0 +1,75 @@
+@php($listErrorKey = "$column.values")
+
+<div class="{{$viewClass['form-group']}} {{ $errors->has($listErrorKey) ? 'has-error' : '' }}">
+
+    <label class="{{$viewClass['label']}} control-label">{{$label}}</label>
+
+    <div class="{{$viewClass['field']}}">
+
+        @if($errors->has($listErrorKey))
+            @foreach($errors->get($listErrorKey) as $message)
+                <label class="control-label" for="inputError"><i class="fa fa-times-circle-o"></i> {{$message}}</label><br/>
+            @endforeach
+        @endif
+
+        <table class="table table-hover">
+
+            <tbody class="list-{{$column}}-table">
+
+            @foreach(old("{$column}.values", ($value ?: [])) as $k => $v)
+
+                @php($itemErrorKey = "{$column}.values.{$loop->index}")
+
+                <tr>
+                    <td>
+                        <div class="form-group {{ $errors->has($itemErrorKey) ? 'has-error' : '' }}">
+                            <div class="col-sm-12">
+                                <input name="{{ $column }}[values][]" value="{{ old("{$column}.values.{$k}", $v) }}" class="form-control" />
+                                @if($errors->has($itemErrorKey))
+                                    @foreach($errors->get($itemErrorKey) as $message)
+                                        <label class="control-label" for="inputError"><i class="fa fa-times-circle-o"></i> {{$message}}</label><br/>
+                                    @endforeach
+                                @endif
+                            </div>
+                        </div>
+                    </td>
+
+                    <td style="width: 75px;">
+                        <div class="{{$column}}-remove btn btn-warning btn-sm pull-right">
+                            <i class="fa fa-trash">&nbsp;</i>{{ __('admin.remove') }}
+                        </div>
+                    </td>
+                </tr>
+            @endforeach
+            </tbody>
+            <tfoot>
+            <tr>
+                <td></td>
+                <td>
+                    <div class="{{ $column }}-add btn btn-success btn-sm pull-right">
+                        <i class="fa fa-save"></i>&nbsp;{{ __('admin.new') }}
+                    </div>
+                </td>
+            </tr>
+            </tfoot>
+        </table>
+    </div>
+</div>
+
+<template class="{{$column}}-tpl">
+    <tr>
+        <td>
+            <div class="form-group">
+                <div class="col-sm-12">
+                    <input name="{{ $column }}[values][]" class="form-control" />
+                </div>
+            </div>
+        </td>
+
+        <td style="width: 75px;">
+            <div class="{{$column}}-remove btn btn-warning btn-sm pull-right">
+                <i class="fa fa-trash">&nbsp;</i>{{ __('admin.remove') }}
+            </div>
+        </td>
+    </tr>
+</template>

+ 2 - 0
src/Form.php

@@ -73,6 +73,7 @@ use Dcat\Admin\Form\Concerns;
  * @method Field\MultipleImage  multipleImage($column, $label = '')
  * @method Field\Tree           tree($column, $label = '')
  * @method Field\Table          table($column, $callback)
+ * @method Field\ListField      list($column, $label = '')
  *
  * @method Field\BootstrapFile          bootstrapFile($column, $label = '')
  * @method Field\BootstrapImage         bootstrapImage($column, $label = '')
@@ -142,6 +143,7 @@ class Form implements Renderable
         'multipleImage'  => Field\MultipleImage::class,
         'tree'           => Field\Tree::class,
         'table'          => Field\Table::class,
+        'list'           => Field\ListField::class,
 
         'bootstrapFile'          => Field\BootstrapFile::class,
         'bootstrapImage'         => Field\BootstrapImage::class,

+ 134 - 0
src/Form/Field/ListField.php

@@ -0,0 +1,134 @@
+<?php
+
+namespace Dcat\Admin\Form\Field;
+
+use Dcat\Admin\Admin;
+use Dcat\Admin\Form\Field;
+use Illuminate\Support\Arr;
+
+class ListField extends Field
+{
+    /**
+     * Max list size.
+     *
+     * @var int
+     */
+    protected $max;
+
+    /**
+     * Minimum list size.
+     *
+     * @var int
+     */
+    protected $min = 0;
+
+    /**
+     * @var array
+     */
+    protected $value = [''];
+
+    /**
+     * Set Max list size.
+     *
+     * @param int $size
+     *
+     * @return $this
+     */
+    public function max(int $size)
+    {
+        $this->max = $size;
+        return $this;
+    }
+
+    /**
+     * Set Minimum list size.
+     *
+     * @param int $size
+     *
+     * @return $this
+     */
+    public function min(int $size)
+    {
+        $this->min = $size;
+        return $this;
+    }
+
+    /**
+     * Fill data to the field.
+     *
+     * @param array $data
+     *
+     * @return void
+     */
+    public function formatAttributeFromQuery($data)
+    {
+        $this->data = $data;
+
+        return Arr::get($data, $this->column, $this->value);
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function getValidator(array $input)
+    {
+        if ($this->validator) {
+            return $this->validator->call($this, $input);
+        }
+        if (!is_string($this->column)) {
+            return false;
+        }
+        $rules = $attributes = [];
+        if (!$fieldRules = $this->getRules()) {
+            return false;
+        }
+        if (!Arr::has($input, $this->column)) {
+            return false;
+        }
+        $rules["{$this->column}.values.*"] = $fieldRules;
+        $attributes["{$this->column}.values.*"] = __('Value');
+        $rules["{$this->column}.values"][] = 'array';
+        if (!is_null($this->max)) {
+            $rules["{$this->column}.values"][] = "max:$this->max";
+        }
+        if (!is_null($this->min)) {
+            $rules["{$this->column}.values"][] = "min:$this->min";
+        }
+        $attributes["{$this->column}.values"] = $this->label;
+        return validator($input, $rules, $this->getValidationMessages(), $attributes);
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    protected function setupScript()
+    {
+        $this->script = <<<SCRIPT
+$('.{$this->column}-add').on('click', function () {
+    var tpl = $('template.{$this->column}-tpl').html();
+    $('tbody.list-{$this->column}-table').append(tpl);
+});
+$('tbody').on('click', '.{$this->column}-remove', function () {
+    $(this).closest('tr').remove();
+});
+SCRIPT;
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function prepare($value)
+    {
+        return array_values($value['values']);
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function render()
+    {
+        $this->setupScript();
+        Admin::style('td .form-group {margin-bottom: 0 !important;}');
+        return parent::render();
+    }
+}