unless you have thousands of topics, why would you need to lazy load anything?
How to lazy load child component and data
Extremely new to Intertia so I'm not even sure if my question is doable:
In my Laravel project I have a model called Topic which has a relationship back to itself to allow for subtopics, and for those topics to have subtopics etc etc.
I want to have a Vue component that initially displays all of the topic level topics in a tableish style, and when one of those topics is clicked, I want the table to expand to show the child topics, and when one of those children is clicked to show it's children etc.
I'm hoping there is a way to lazy load the children on demand without a full page refresh.
My TopicController index method:
public function index()
{
return Inertia::render('Topic/Index',[
'topics' => Auth::check() ? Topic::mine()->orderBy('name')->get() : null,
]);
}
Query Scope in Topic:
public function scopeMine($query, $parentId = null)
{
return $query->where(['user_id' => Auth::id(), 'topic_id' => $parentId ?? null])->withCount('children');
}
Topic/Index.vue:
<script setup>
import Layout from '@/Shared/Layout.vue';
import TopicBar from '@/Shared/Topic/TopicBar.vue';
defineProps({
topics: Array
});
</script>
<template>
<Layout title="Topics Index">
<div class="w-4/5 lg:w-1/2 space-y-3 fixed">
<div v-for="topic in topics">
<TopicBar :topic="topic"/>
</div>
</div>
</layout>
</template>
and TopicBar.vue
<script setup>
import { Link } from '@inertiajs/vue3';
import { ref } from 'vue';
import ChevronDown from '../SVG/ChevronDown.vue';
defineProps({
topic: Object
});
let showChildren = ref(false);
</script>
<template>
<div
class="bg-opacity-40 rounded p-2 flex items-center relative"
:class="topic.background"
>
<div class="truncate w-32">
{{ topic.name }}
</div>
<div class="text-base text-left hidden lg:inline ml-3 truncate w-52">
{{ topic.description }}
</div>
<div class="text-base text-left hidden lg:inline ml-3 truncate w-52">
{{ topic.children_count }}
</div>
<div class=" fill-white absolute right-2" v-if="topic.children_count > 0">
<ChevronDown width="30" height="30" @click="showChildren = ! showChildren" class="-rotate-90"/>
</div>
</div>
<div v-if="showChildren">
<!-- <div v-for="child in topic.children">
<TopicBar :topic="child" />
</div> -->
</div>
</template>
I see the documentation refers to the partial reload which is what I suspect I want, but I'm not clear how to lazy load child relationships.
I hope my rambling makes some sense!
TIA
Please or to participate in this conversation.