1
0

2 Коммиты 937ec22020 ... 0154b3b4d5

Автор SHA1 Сообщение Дата
  Home 0154b3b4d5 8-12子分类换页优化 4 лет назад
  Home 2a52faab39 8-11增加子分类的数量显示 4 лет назад

+ 2 - 1
app/admin/controller/Category.php

@@ -28,7 +28,8 @@ class Category extends AdminBase
         }
 
         return View::fetch('', [
-            'categorys' => $categorys
+            'categorys' => $categorys,
+            'pid' => $pid
         ]);
     }
 

+ 2 - 2
app/admin/view/category/index.html

@@ -88,7 +88,7 @@
                     <td>
                         <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete delete" data-ptype="1"
                            lay-event="delete" data-id="{$vo.id}">删除</a>
-                        <a href="{:url('index', ['pid'=>$vo.id])}">获取子栏目</a>
+                        <a class="layui-btn layui-btn-xs layui-btnnormal " href="{:url('index', ['pid'=>$vo.id])}">获取子栏目({$vo.childCount})</a>
                     </td>
                 </tr>
                 {/volist}
@@ -132,7 +132,7 @@
             , curr: {$categorys.current_page}
             , jump: function (obj, first) {
                 if (!first) {
-                    location.href = "?page=" + obj.curr
+                    location.href = "?page=" + obj.curr + "&pid={$pid}"
                 }
             }
         });

+ 20 - 0
app/common/business/Category.php

@@ -71,6 +71,26 @@ class Category
         }
         $result = $list->toArray();
         $result['render'] = $list->render();
+
+        // 第一步拿到列表中的id
+        // 第二步in_array 求count
+        // 第三步把count填充到列表页中
+        $pids = array_column($result['data'], 'id');
+        if ($pids) {
+            $idCountResult = $this->model->getChildCountInPids(['pid' => $pids]);
+            $idCountResult = $idCountResult->toArray();
+
+            $idCounts = [];
+            foreach ($idCountResult as $countResult) {
+                $idCounts[$countResult['pid']] = $countResult['count'];
+            }
+        }
+        if ($result['data']) {
+            foreach ($result['data'] as $k => $value) {
+                $result['data'][$k]['childCount'] = $idCounts[$value['id']] ?? 0;
+            }
+        }
+
         return $result;
     }
 

+ 15 - 0
app/common/model/mysql/Category.php

@@ -61,4 +61,19 @@ class Category extends Model
         $data['update_time'] = time();
         return $this->where(['id' => $id])->save($data);
     }
+
+    /**
+     * 根据pid获取子分类的个数
+     * @param $condition
+     * @return mixed
+     */
+    public function getChildCountInPids($condition)
+    {
+        $where[] = ['pid', 'in', $condition['pid']];
+        $where[] = ['status', '<>', config('status.mysql.table_delete')];
+        return $this->where($where)
+            ->field(['pid', 'count(*) as count'])
+            ->group('pid')
+            ->select();
+    }
 }