Php JsTree与Laravel的集成
我有产品视图。我想添加到此视图类别树。我想到了jsTree 我在我的项目中使用了Laravel 7和kalnoy/nestedset,以及 Mi迁移文件:Php JsTree与Laravel的集成,php,laravel,laravel-5,Php,Laravel,Laravel 5,我有产品视图。我想添加到此视图类别树。我想到了jsTree 我在我的项目中使用了Laravel 7和kalnoy/nestedset,以及 Mi迁移文件: Schema::create('categories', function (Blueprint $table) { $table->id(); $table->string('category_name', 155); $table->string('
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();
}
我的模型是分类的
因此,我有:
如何将数据转换为jsTree格式?我不知道您是否需要帮助查看数据或数据格式,但我将尝试帮助解释json的格式,基本元素必须是3: -Id:当前元素的唯一标识符。 -父项:来自父项的id,或在没有父项时使用#。 -文本:显示项目名称 无法接受jsTree格式的当前输出数据,您必须调整此格式:
$('#yourdata').jstree({ 'core' : {
'data' : [
{"id": 1,"parent":"#","text": "- Books"},
{"id": 2,"parent":"1","text": "-- Comic Book"}
]
}
});
它将显示:
+ Book
-- Comic Book
作为子类别
我希望它能有所帮助