79 lines
2.2 KiB
PHP
79 lines
2.2 KiB
PHP
<?php
|
|
|
|
namespace App\Admin\Extensions\Form;
|
|
|
|
use App\Models\{TypeAttr};
|
|
use Dcat\Admin\Form\Field;
|
|
|
|
class SelectAttr extends Field
|
|
{
|
|
protected $view = 'admin.extensions.select_attr';
|
|
|
|
protected $listen = "";
|
|
protected $goods_id = 0;
|
|
|
|
protected static $js = [
|
|
'/vendor/vue/vue.js'
|
|
// 'https://cdn.jsdelivr.net/npm/vue/dist/vue.js' // 这里可以将vue.js下载下来引入
|
|
];
|
|
|
|
public function listen($listen = '', $goods_id = 0)
|
|
{
|
|
$this->listen = $listen;
|
|
$this->goods_id = $goods_id;
|
|
return $this;
|
|
}
|
|
|
|
/**
|
|
* {@inheritdoc}
|
|
*/
|
|
public function render()
|
|
{
|
|
$this->addVariables(['listen' => $this->listen, 'goods_id' => $this->goods_id]);
|
|
|
|
return parent::render();
|
|
}
|
|
|
|
protected function prepareInputValue($value){
|
|
$goods_attrs = json_decode($value, true);
|
|
// dd($goods_attrs);
|
|
if(count($goods_attrs) > 1){
|
|
foreach($goods_attrs as $key => $goods_attr){
|
|
if(!isset($goods_attr['_remove_'])){
|
|
$goods_attr['_remove_'] = 0;
|
|
}
|
|
$goods_attrs[$key] = $goods_attr;
|
|
}
|
|
}
|
|
return $goods_attrs;
|
|
}
|
|
|
|
|
|
protected function formatFieldData($data)
|
|
{
|
|
// 获取到当前字段值
|
|
$value = parent::formatFieldData($data);
|
|
$select_attr_value = [
|
|
'arr'=>[],
|
|
'arr2'=>[],
|
|
'goodsAttr'=>$value
|
|
];
|
|
$goods_attrs = [];
|
|
foreach($value as $goods_attr){
|
|
$goods_attrs[$goods_attr['attr_id']][] = $goods_attr['attr_value'];
|
|
}
|
|
$type_attr = TypeAttr::where('type_id', $data['type_id'])->get()->toArray();
|
|
foreach($type_attr as $type_attr){
|
|
if(isset($goods_attrs[$type_attr['id']])){
|
|
$select_attr_value['arr'][] = [
|
|
'attr_name'=>$type_attr['attr_name'],
|
|
'attr_id'=>$type_attr['id']
|
|
];
|
|
|
|
$select_attr_value['arr2'][] = implode('<br>', $goods_attrs[$type_attr['id']]);
|
|
}
|
|
}
|
|
// dd($select_attr_value);
|
|
return json_encode($select_attr_value);
|
|
}
|
|
} |