79 lines
1.7 KiB
PHP
79 lines
1.7 KiB
PHP
<?php
|
|
|
|
namespace App\Admin\Form;
|
|
|
|
use Dcat\Admin\Form\Field;
|
|
use Illuminate\Support\Arr;
|
|
|
|
class CropExtends extends Field
|
|
{
|
|
protected $view = 'admin.form.crop_extends';
|
|
|
|
protected $listen = '';
|
|
|
|
protected static $js = [
|
|
'/vendor/vue/vue.js',
|
|
// 'https://cdn.jsdelivr.net/npm/vue/dist/vue.js' // 这里可以将vue.js下载下来引入
|
|
];
|
|
|
|
public function listen($listen = '')
|
|
{
|
|
$this->listen = $listen;
|
|
|
|
return $this;
|
|
}
|
|
|
|
public function render()
|
|
{
|
|
$this->addVariables(['listen' => $this->listen]);
|
|
|
|
return parent::render();
|
|
}
|
|
|
|
/**
|
|
* 格式化保存值
|
|
*
|
|
* @param [type] $data
|
|
* @return void
|
|
*/
|
|
protected function prepareInputValue($value)
|
|
{
|
|
$value = json_decode($value, true);
|
|
$toValue = null;
|
|
if ($value) {
|
|
foreach ($value as $item) {
|
|
$toValue[$item['name']] = $item['value'];
|
|
}
|
|
}
|
|
|
|
return $toValue;
|
|
}
|
|
|
|
/**
|
|
* 格式化渲染值
|
|
*
|
|
* @param [type] $value
|
|
* @return void
|
|
*/
|
|
protected function formatFieldData($data)
|
|
{
|
|
$value = parent::formatFieldData($data);
|
|
|
|
$crop = Arr::get($this->variables, 'crop', null);
|
|
$toValue = [];
|
|
if ($crop && $crop->extends) {
|
|
$extends = json_decode($crop->extends, true);
|
|
foreach ($extends as $ext) {
|
|
$toValue[] = array_merge($ext, ['value' => $value[$ext['name']] ?? null]);
|
|
}
|
|
}
|
|
|
|
return json_encode($toValue);
|
|
}
|
|
|
|
public function crop($crop)
|
|
{
|
|
return $this->addVariables(['crop' => $crop]);
|
|
}
|
|
}
|