Ver código fonte

8-11增加子分类的数量显示

Home 4 anos atrás
pai
commit
2a52faab39

+ 1 - 1
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}

+ 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();
+    }
 }