Skip to content

Commit 43f1c32

Browse files
Display selected category full path
1 parent 483b0ba commit 43f1c32

File tree

4 files changed

+77
-16
lines changed

4 files changed

+77
-16
lines changed

resources/js/components/FormField.vue

Lines changed: 50 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,38 @@
11
<template>
2-
<default-field :field="field" :errors="errors" :show-help-text="false">
2+
<default-field :field="field" :errors="errors" :show-help-text="false" class="categories-tree">
33
<template slot="field">
4-
<div :dir="field.rtl ? 'rtl' : 'ltr'" class="p-2 nova-tree-attach-many">
5-
<treeselect v-model="selectedValues"
6-
:id="field.name"
7-
:multiple="field.multiple"
8-
:options="field.options"
9-
:flat="field.flatten"
10-
:searchable="field.searchable"
11-
:always-open="field.alwaysOpen"
12-
:disabled="field.disabled"
13-
:sort-value-by="field.sortValueBy"
14-
:placeholder="field.placeholder"
15-
:max-height="field.maxHeight"
16-
:value-consists-of="field.valueConsistsOf"
17-
:normalizer="normalizer"
18-
/>
4+
<div class="flex">
5+
<div :dir="field.rtl ? 'rtl' : 'ltr'" class="w-3/5 nova-tree-attach-many">
6+
<treeselect v-model="selectedValues"
7+
:id="field.name"
8+
:multiple="field.multiple"
9+
:options="field.options"
10+
:flat="field.flatten"
11+
:searchable="field.searchable"
12+
:always-open="field.alwaysOpen"
13+
:disabled="field.disabled"
14+
:sort-value-by="field.sortValueBy"
15+
:placeholder="field.placeholder"
16+
:max-height="field.maxHeight"
17+
:value-consists-of="field.valueConsistsOf"
18+
:normalizer="normalizer"
19+
/>
20+
</div>
21+
<div class="w-2/5 pl-8 pt-2" v-if="field.displayPath && fullUrl">
22+
<ul class="category-list pl-6">
23+
<li class="py-1 text-80 text-sm capitalize" v-for="(url,index) in fullUrl" :key="index"><strong>{{ index }}</strong> {{ url }}</li>
24+
</ul>
25+
</div>
1926
</div>
2027
</template>
2128
</default-field>
2229
</template>
2330

31+
<style type="text/css">
32+
.categories-tree .py-6.px-8.w-1\/2 { width: 80%; }
33+
.category-list { list-style: none;}
34+
</style>
35+
2436
<script>
2537
import {FormField, HandlesValidationErrors} from 'laravel-nova'
2638
@@ -37,9 +49,28 @@ export default {
3749
{
3850
return {
3951
selectedValues: null,
52+
fullUrl:null,
4053
};
4154
},
55+
watch: {
56+
selectedValues: function(val) {
57+
//do something when the data changes.
58+
if (val) {
59+
this.getCategoryFullPath(val);
60+
}
61+
}
62+
},
4263
methods: {
64+
getCategoryFullPath(val){
65+
Nova.request().get('/nova-vendor/nova-nested-tree-attach-many/get-category-full-path',{
66+
params:{
67+
categories: val || this.selectedValues,
68+
}
69+
})
70+
.then( ( data ) => {
71+
this.fullUrl = data.data;
72+
} );
73+
},
4374
normalizer( node )
4475
{
4576
return {
@@ -79,6 +110,9 @@ export default {
79110
{
80111
this.selectedValues = data.data || [];
81112
}
113+
if (this.selectedValues.length > 0){
114+
this.getCategoryFullPath(this.selectedValues);
115+
}
82116
} );
83117
}
84118
else

routes/api.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
<?php
22

33
Route::get('/{resource}/{resourceId}/attached/{relationship}/{idKey}', 'PhoenixLib\NovaNestedTreeAttachMany\Http\Controllers\NestedTreeController@attached');
4+
Route::get('/get-category-full-path', 'PhoenixLib\NovaNestedTreeAttachMany\Http\Controllers\NestedTreeController@getCategoryFullPath');

src/Http/Controllers/NestedTreeController.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace PhoenixLib\NovaNestedTreeAttachMany\Http\Controllers;
44

5+
use App\Models\Category;
56
use Illuminate\Routing\Controller;
67
use Laravel\Nova\Http\Requests\NovaRequest;
78
use PhoenixLib\NovaNestedTreeAttachMany\Domain\Relation\RelationHandlerFactory;
@@ -23,4 +24,20 @@ public function attached(NovaRequest $request, $resource, $resourceId, $relation
2324

2425
return $handler->retrieve($model, $relationship, $idKey);
2526
}
27+
28+
public function getCategoryFullPath(\Laravel\Nova\Http\Requests\NovaRequest $request){
29+
$categories = $request->input('categories');
30+
$urls = [];
31+
if ($categories){
32+
foreach ($categories as $key => $value){
33+
$category = Category::with(["stores" => function($q){
34+
return $q->select('store_name');
35+
}])->where("category_id",$value)->first();
36+
$category_tree = "[ " . $category->categoryBreadcrumb() . " ]";
37+
$stores = implode(' | ',array_column($category->stores->toArray(), 'store_name'));
38+
$urls[$category["category_name"]] = $category_tree.' | '.$stores;
39+
}
40+
}
41+
return $urls;
42+
}
2643
}

src/NestedTreeAttachManyField.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,15 @@ public function searchable(bool $searchable): NestedTreeAttachManyField
9696
return $this;
9797
}
9898

99+
public function displayPath(bool $displayPath = false): NestedTreeAttachManyField
100+
{
101+
$this->withMeta([
102+
'displayPath' => $displayPath,
103+
]);
104+
105+
return $this;
106+
}
107+
99108
public function withIdKey(string $idKey = 'id'): NestedTreeAttachManyField
100109
{
101110
$this->withMeta([

0 commit comments

Comments
 (0)