我有产品视图。我想添加到此视图类别树中。我想到了jsTree。
我在我的项目中使用 Laravel 7 和 kalnoy/nestedset 和https://www.jstree.com
小米迁移文件:
Schema::create('categories', function (Blueprint $table) {
$table->id();
$table->string('category_name', 155);
$table->string('description', 155)->nullable();
$table->string('keywords', 155)->nullable();
$table->longText('content')->nullable();
$table->char('enable', 1)->default(0);
$table->string('photo', 155)->nullable();
$table->bigInteger('order')->default(0);
$table->string('slug', 160)->nullable();
NestedSet::columns($table);
$table->engine = "InnoDB";
$table->charset = 'utf8mb4';
$table->collation = 'utf8mb4_unicode_ci';
});
在控制器中我有:
public function categoryTree(CategoryRepositoryInterface $categoryRepository, Request $request)
{
$nodes = $this->getCategoriesTree($categoryRepository->getTree());
return $nodes;
}
private function getCategoriesTree($nodes): array
{
$categoryArray = array();
$traverse = function ($categories, $prefix = '-') use (&$traverse, &$categoryArray) {
foreach ($categories as $category) {
$categoryArray[] = ['id' => $category->id, 'name' => $prefix . ' ' . $category->category_name];
$traverse($category->children, $prefix . '-');
}
};
$traverse($nodes);
return $categoryArray;
}
在存储库中:
public function getTree()
{
return $this->model->orderBy('order', 'ASC')->get()->toTree();
}
我的模型是类别。
结果我有: https: //pastebin.com/uErKGgHP
如何将我的数据转换为 jsTree 格式?
摇曳的蔷薇