簡體   English   中英

Select2 在 Laravel Backpack 5 Pro 中使用樞軸(nn)在 ajax 中繼續顯示“搜索”

[英]Select2 keep showing "Searching" in ajax with pivot (n-n) in Laravel Backpack 5 Pro

我有 2 個模型:Order 和 Product,每個模型都具有正確設置的數據集的 belongsToMany。

在 OrderCrudController 中,我還使用了 FetchOperation 並創建了 fethProducts() 函數,如下所示:

use \Backpack\CRUD\app\Http\Controllers\Operations\FetchOperation;

public function fetchProducts()
{
    return $this->fetch([
        'model' => \App\Models\Product::class,
        'searchable_attributes' => ['name','sku']
    ]);
    // return $this->fetch(\App\Models\Product::class); <-- I also tried this one
}

protected function setupCreateOperation()
{
    CRUD::setValidation(OrderRequest::class);
    
    // other fields

    $this->crud->addField([
        'name' => 'products',
        'type' => 'relationship',
        'pivotSelect' => [
            'attribute' => 'name',
            'ajax'      => true,
        ],
        'subfields' => [
            [
                'name' => 'quantity',
                'type' => 'number',
            ],
        ],
    ]);
}

但是當我搜索產品時出現意外行為,盡管請求成功檢索了數據,但 select2 字段仍然“正在搜索”。

屏幕截圖 - select2 字段

屏幕截圖 - ajax 結果

PS:這個字段在沒有子字段、沒有供應商覆蓋等的情況下完美地工作,所以我認為我已經正確設置了所有內容。

任何人都可以幫忙嗎?

兩個月前有人問過這個問題,但不知何故我錯過了,今天有人在 GitHub 存儲庫上打開了一個問題后才注意到它。

我很高興你們找到了解決方案,但不幸的是我不能推薦它,因為使用 select2_from_ajax 會錯過不允許選擇相同樞軸兩次的功能,否則在保存條目時會產生不良后果。

我剛剛提交了一個 PR 來解決這個問題,當它合並時我會在這里聯系你們,可能在下周一之前。

干杯

我剛剛遇到了這個確切的問題,經過相當多的研究和試驗,我終於找到了解決方案!

問題似乎與pivotSelect 中的relationship字段類型有關。 嘗試改用select2_from_ajax並且不要忘記將方法顯式設置為 POST,這對我來說就像一個魅力。

以下是您可能會在您的情況下嘗試的方法:

$this->crud->addField([
    'name' => 'products',
    'type' => 'relationship',
    'pivotSelect' => [
        'attribute' => 'name',
        'type'      => 'select2_from_ajax',
        'method'    => 'POST',
        'data_source' => backpack_url('order/fetch/products') // Assuming this is the URL of the fetch operation
    ],
    'subfields' => [
        [
            'name' => 'quantity',
            'type' => 'number',
        ],
    ],
]);

暫無
暫無

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

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