Ver Fonte

feat:dist添加产品分类修改

igb há 5 meses atrás
pai
commit
bb6deb64a7

+ 12 - 4
app/Distributor/Controllers/DistProductCategoryController.php

@@ -73,11 +73,11 @@ class DistProductCategoryController extends AdminController
                 return html_entity_decode($name); // 或者直接返回 $name,如果你确定不需要转义
             });
             $grid->column('parent_id');
-            $grid->column('distProductParameter.name',admin_trans_label('parameter_name'));
-            $grid->column('order')->orderable();
+            //$grid->column('distProductParameter.name',admin_trans_label('parameter_name'));
+            $grid->column('order');//->orderable();
             $grid->column('enabled')->switch();
             $grid->column('created_at');
-            $grid->column('updated_at')->sortable();
+            $grid->column('updated_at');//->sortable();
 //            $grid->filter(function (Grid\Filter $filter) {
 //                $filter->like('name');
 //            });
@@ -100,11 +100,19 @@ class DistProductCategoryController extends AdminController
         return Form::make(new DistProductCategory(), function (Form $form) {
             $form->display('id', 'ID');
             $form->select('parent_id', admin_trans_field('parent'))
-                ->options(DistProductCategory::selectOptions())
+                ->options(DistProductCategory::selectOptions(function ($query) {
+                    // 在这里定义查询条件
+                    $query->where('parent_id', 0);
+                }))
                 ->saving(function ($v) {
                     return (int) $v;
                 });
             $form->text('name')->required();
+            $form->text('order')
+                ->default(0)
+                ->rules('numeric')
+                ->help(admin_trans_label('order_tips')); // 添加备注
+
           //  $form->select('parameter_id', 'parameter')->options(DistProductParameter::selectOptions())->required();// DIST不需要绑定 parameter
             $form->switch('enabled')->default(1);
           //  $form->display('created_at');

+ 18 - 25
app/Distributor/Repositories/DistProductCategory.php

@@ -20,35 +20,27 @@ class DistProductCategory extends EloquentRepository
     // 调用模型方法
     public static function selectOptions(\Closure $closure = null)
     {
-        $selectOptions = Model::class::selectOptions($closure);
 
-// 用于存储结果
-        $headings = [];
+        $query = Model::query();
 
-// 遍历数组,提取以 ├─ 开头的部分
-        foreach ($selectOptions as $key => $value) {
-            // 检查字符串是否以 ├─ 开头
-            if (strpos($value, '├─') === 0 || strpos($value, '└─') === 0) {
+        if ($closure) {
+            $closure($query);
+        }
 
-                // 保存结果
-                $headings[$key] = $value;
-            }
+        // 确保查询构造器不为空
+        if (!$query) {
+            return [];
         }
 
-        return $headings;
-//        $query = Model::query();
-//
-//        if ($closure) {
-//            $closure($query);
-//        }
-//
-//        // 确保查询构造器不为空
-//        if (!$query) {
-//            return [];
-//        }
-//
-//        // 返回键值对数组
-//        return $query->orderBy('name')->pluck('name', 'id')->all();
+        // 返回键值对数组,并按 'name' 和 'order' 字段排序
+        $results = $query->orderBy('name')->orderBy('order')->pluck('name', 'id');
+
+        // 在每个 name 前面加上 '├─'
+        $results = $results->map(function ($name) {
+            return '├─' . $name;
+        });
+
+        return $results->all();
 
     }
 
@@ -95,7 +87,6 @@ class DistProductCategory extends EloquentRepository
         }
 
 
-
         return $model->makePaginator(
             1, // 传入总记录数
             $data // 传入数据二维数组
@@ -122,6 +113,8 @@ class DistProductCategory extends EloquentRepository
 
             // 如果存在子节点,则递归处理子节点
             if (isset($node['children']) && is_array($node['children'])) {
+
+
                 $flat = array_merge($flat, $this->flattenTree($node['children'], $level + 1));
             }
         }