简体   繁体   English

Select2 在 Laravel Backpack 5 Pro 中使用枢轴(nn)在 ajax 中继续显示“搜索”

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

I have 2 models: Order and Product, each has belongsToMany with pivot set properly.我有 2 个模型:Order 和 Product,每个模型都具有正确设置的数据集的 belongsToMany。

In OrderCrudController, I also use FetchOperation and make a fethProducts() function as below:在 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',
            ],
        ],
    ]);
}

But it comes to unexpected behavior when I search the product, the select2 field remains "searching" though the request successfully retrieved the data.但是当我搜索产品时出现意外行为,尽管请求成功检索了数据,但 select2 字段仍然“正在搜索”。

screenshot - select2 field屏幕截图 - select2 字段

screenshot - ajax results屏幕截图 - ajax 结果

PS: this field works perfectly without subfields, no vendor overrides etc., so I think I've set everything correctly. PS:这个字段在没有子字段、没有供应商覆盖等的情况下完美地工作,所以我认为我已经正确设置了所有内容。

Anyone can help?任何人都可以帮忙吗?

This was asked two months ago but somehow I missed it, just noticed it today after someone opened an issue on the GitHub repository.两个月前有人问过这个问题,但不知何故我错过了,今天有人在 GitHub 存储库上打开了一个问题后才注意到它。

I am happy you guys found a solution for it but unfortunatelly I cannot recommend it as using the select2_from_ajax will miss the functionality to don't allow the selection of the same pivots twice, otherwise you will have undesired consequences when saving the entry.我很高兴你们找到了解决方案,但不幸的是我不能推荐它,因为使用 select2_from_ajax 会错过不允许选择相同枢轴两次的功能,否则在保存条目时会产生不良后果。

I've just submitted a PR to fix this issue, I will ping you guys here when it's merged, probably by next Monday.我刚刚提交了一个 PR 来解决这个问题,当它合并时我会在这里联系你们,可能在下周一之前。

Cheers干杯

I just came accross this exact problem and after quite some research and trials, i finally found a solution !我刚刚遇到了这个确切的问题,经过相当多的研究和试验,我终于找到了解决方案!

The problem seems to be related to the relationship field type inside the pivotSelect.问题似乎与pivotSelect 中的relationship字段类型有关。 Try to use select2_from_ajax instead and don't forget to set method to POST explicitly, that worked for me like a charm.尝试改用select2_from_ajax并且不要忘记将方法显式设置为 POST,这对我来说就像一个魅力。

Here is what you might try in your case :以下是您可能会在您的情况下尝试的方法:

$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