简体   繁体   English

如何使用 jquery 附加数据以形成集体选择 optgroup 标签

[英]How to append data to form collective select optgroup tag with jquery

I'm trying to append data based on the seclect dropdown to another select.我正在尝试将基于 seclect 下拉列表的数据附加到另一个选择。 Everything works well but im finding difficulty in appending data to the second select.一切正常,但我发现将数据附加到第二个选择时遇到困难。 Any hlp help to approach it will be of much assistance任何帮助接近它的帮助都会有很大帮助

// The select that initiates onchange
{!! Form::select('item_inventory', array(1 => 'Asset', 2 => 'GL'), null, ['id'=> 'item-modules', 'class' => 'form-control de-account-select2', 'placeholder' => trans('general.form.select.field', ['field' => trans_choice('general.items', 1)])]) !!}

//The data that should be updated on response success
{!! Form::select('item[' . $item_row . '][name]', array(1=>'asset 1'),  null, ['id'=> 'item-name-' . $item_row, 'class' => 'form-control', 'placeholder' => trans('general.form.select.field', ['field' => trans_choice('general.accounts', 1)])]) !!}

//JQuery code
$(document).on('change', '#item-modules', function () {

            $.ajax({
                url: "{{url('expenses/Accounts') }}",
                type: 'GET',
                dataType: 'JSON',
                data: 'item_module_id=' + $(this).val(),
                headers: {'X-CSRF-TOKEN': '{{ csrf_token() }}'},
                success: function (data) {
                    let json = data.accounts;
                    console.log(json);
                    let html = "";
                    for (let option in json) {
                        html += `<optgroup label="` + option + `">`;
                        json[option].forEach(function (item) {
                            html += `<option value="` + 
                    item["Attrib_value"] + `" >` + item["Attrib_value"] + `</option>`
                        });
                        html += `</optgroup>`;
                    }

                    $('#item-name-' + item_row).html(html);
                 }
               });

Console.log Response Data Console.log 响应数据

Depreciation:
41: "700 - Depreciation"

Direct Costs:
20: "500 - Costs of Goods Sold"

Expense:
21: "600 - Advertising"
22: "605 - Bank Service Charges"
23: "610 - Janitorial Expenses"
24: "615 - Consulting & Accounting"

First define the dropdown variable in top of ajax request after function definition .首先在函数定义之后在ajax请求顶部定义下拉变量。

$(document).on('change', '#item-modules', function () {
let dropdown = $('#id_of_second_select');
dropdown.empty();
dropdown.append('<option selected="true" disabled>Set default select</option>');
dropdown.prop('selectedIndex', 0);

After posting the variable and getting in ajax , after success function , add the every variable by using this .发布变量并进入ajax后,在成功函数之后,使用this添加每个变量。

$.each(json, function (key, entry) {
    dropdown.append($('<option></option>').attr('value', entry).text(key));
})

This is how i modified my JQUERY and worked for me, i was missing some finer details ```这就是我修改我的 JQUERY 并为我工作的方式,我错过了一些更精细的细节```

     $(document).on('change', '#item-modules', function () {

        $.ajax({
            url: "{{url('expenses/Accounts') }}",
            type: 'GET',
            dataType: 'JSON',
            data: 'item_module_id=' + $(this).val(),
            headers: {'X-CSRF-TOKEN': '{{ csrf_token() }}'},
            success: function (data) {
                let json = data.accounts;

                let newDropdown = '';
                for (let option in json) {
                    newDropdown += `<optgroup label="` + option + `">`;
                    $.each(json[option], function (key, item) {
                        newDropdown += `<option value="` + key + `" >` + item + `</option>`
                    });
                    newDropdown += `</optgroup>`;
                }
                $('#item-name-'+String(item_row-1)).append(newDropdown);

            }
        })

    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM