cms-laravel/app/Livewire/CascaderForm.php

51 lines
1.3 KiB
PHP

<?php
namespace App\Livewire;
use Livewire\Component;
use Illuminate\Support\Facades\Http;
class CascaderForm extends Component
{
public $options = [];
public $selected = [];
public $url;
public function mount($url)
{
$this->url = $url;
$this->fetchOptions();
}
public function fetchOptions()
{
try {
$response = Http::get($this->url);
$this->options = $response->json('data') ?? [
['value' => 'zhejiang', 'label' => 'Zhejiang', 'children' => [
['value' => 'hangzhou', 'label' => 'Hangzhou', 'children' => [
['value' => 'xihu', 'label' => 'West Lake']
]]
]],
['value' => 'jiangsu', 'label' => 'Jiangsu', 'children' => [
['value' => 'nanjing', 'label' => 'Nanjing', 'children' => [
['value' => 'zhonghuamen', 'label' => 'Zhong Hua Men']
]]
]]
];
} catch (\Exception $e) {
session()->flash('error', 'Failed to load options: ' . $e->getMessage());
}
}
public function updatedSelected()
{
$this->dispatch('cascaderChanged', $this->selected);
}
public function render()
{
return view('livewire.cascader-form');
}
}