用给定的发送到javascript来填充select2

问题描述 投票:0回答:1

在一个应用程序中,我在 EntityType 中创建了一个带有字段(选择)的表单,然后将其传递给 select2,那里没有问题。

树枝视图看起来像是所有保存的助手的数组,您可以通过打开模式来修改助手,我使用数据集正确地将我的值树枝传递给js,并且所有字段都已正确填写。

为了更好地获得一点视觉效果,打开模式并向其发送所有信息的按钮如下所示:

    <a href="javascript:;" class="menu-link px-3" {{ stimulus_action('deal--handle', 'editHelp') }} data-url2="{{ path('help_request_edit', {'id':help.id}) }}" data-organisme="{{ help.organization.value }}" data-date="{{ help.date|date('Y-m-d') }}" data-state="{{ help.state }}" data-amount-requested="{{ help.amountRequested }}" data-amount-to-deduct="{{ help.amountToDeduct }}" data-subrogation="{{ help.subrogation }}" data-quotations="{{ quotationInHelp|json_encode }}">
       Modify
    </a> 

data-quotations="{{ quotationInHelp|json_encode }}"
将数据发送到js,我得到的是这样的:

var quotations = JSON.parse(event.currentTarget.dataset.quotations);

在我的console.log(引用)中一切正常。

但是我找不到如何使用“quotations”变量的数据将它们传递到我的 select2 的值中,例如我的模态的其他字段是这样填充的:

        this.subrogationTarget.value = event.currentTarget.dataset.subrogation;

如果您对这个问题有任何想法/解决方案,谢谢!

twig jquery-select2
1个回答
0
投票

我终于用几行javascript找到了解决方案

var quotations = JSON.parse(event.currentTarget.dataset.quotations);
var i = 0;
    var idQuotations = [];
    for (let i = 0; i < quotations.length; ++i) {
        idQuotations.push(
            quotations[i]['quotation']
        );
    }
    $(this.quotationTarget).val(idQuotations).trigger("change");
© www.soinside.com 2019 - 2024. All rights reserved.