簡體   English   中英

使用laravel 5.2 Backpack從select_from_array字段中獲取選定的值

[英]Get selected value from select_from_array field using laravel 5.2 Backpack

我在新項目中使用Laravel 5.2 Backpack,在我的表單中有一個select_from_array字段,具體取決於所選值,我希望數據顯示在另一個select_from_array字段中。 不知道該怎么做。 請幫我解決一下這個。 這是我的代碼

Controller.php

public function __construct()
{
    if (Request::segment(3) == 'create') {
        $parentField1 = [
        'name' => 'cat_id',
        'label' => 'Category',
        'type' => 'select_from_array',
        'options' => $this->categories(),
        'allows_null' => false,
        ];
        $parentField = [
            'name' => 'subCat_id',
            'label' => 'SubCategory',
            'type' => 'select_from_array',
            'options' => $this->subcategories(),
            'allows_null' => false,
        ];

        array_unshift($this->crud['fields'], $parentField1,$parentField);

    }

public function categories()
{
    $cat = Request::get('cat_id');

    $currentId = 0;
    if (Request::segment(4) == 'edit' and is_numeric(Request::segment(3))) {
        $currentId = Request::segment(3);
    }

    $entries = Category::where('translation_lang', config('app.locale'))->where('parent_id',0)->orderBy('lft')->get();
    if (is_null($entries)) {
        return [];
    }

    $tab = [];
    $tab[0] = 'Root';
    foreach ($entries as $entry) {
        if ($entry->id != $currentId) {
            $tab[$entry->translation_of] = '- ' . $entry->name;
        }
    }
    return $tab;
}

public function subcategories()
{
    $currentId = 0;

    if (Request::segment(4) == 'edit' and is_numeric(Request::segment(3))) {
        $currentId = Request::segment(3);
    }

    $entries = Category::where('translation_lang', config('app.locale'))->where('parent_id','!=' ,0)->orderBy('lft')->get();

    if (is_null($entries)) {
        return [];
    }

    $tab = [];
    $tab[0] = 'Root';
    foreach ($entries as $entry) {
        if ($entry->id != $currentId) {
            $tab[$entry->translation_of] = '- ' . $entry->name;
        }
    }
    return $tab;
}

我想在subcategories()中使用選定選項的ID,可以在其中使用ID來獲取數據。

我認為對您來說最好的方法是為此目的創建一個自定義字段類型,其中包括兩個選擇。 請遵循以下步驟 select2.blade.php文件開始,然后添加實現目標所需的javascript(在第一個select2發生更改事件時,在下一個select2中更改選項)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM