Initialising select2 created dynamically

前端 未结 6 1770
孤独总比滥情好
孤独总比滥情好 2020-12-01 04:49

I have a select2 drop-down for which I provide a matcher function. It is initialised like this on initial page load:

jQuery(document).ready(function() {
             


        
6条回答
  •  旧时难觅i
    2020-12-01 05:15

    I worked this out similar to roshan's solution, but didn't pass the select object in the function. This is for a table output from an ajax call.

    $(document).ready(function() {
    
            function initSelect2() {
                $("[id^='DDAlertFreq']").select2({
                    minimumResultsForSearch: Infinity,
                    allowClear: false,
                    theme: "bootstrap"
                });
            };
    
    //define the dropdown and set to variable    
    var DDAlertFrequency = ''
    
    function RetrieveUserAlerts(uid) {
                    $.ajax({
                        url: 'SavedSearches.asmx/LoadUserAlerts',
                        dataType: 'json',
                        method: 'post',
                        data: { userid: uid },
                        success: function (data) {
                            var tbl = $("#tblAlerts > tbody");
                            tbl.empty();
                            $.each(data, function () {
                                userAlert.alert_idx = this['alert_idx'];
                                userAlert.Name = this['Name'];
                                userAlert.price_alert_low = this['price_alert_low'];
                                userAlert.alert_frequency = this['alert_frequency'];
                                userAlert.alert_max_per_day = this['alert_max_per_day'];
                                userAlert.alert_to_email = this['alert_to_email'];
                                userAlert.alert_to_sms = this['alert_to_sms'];
                                userAlert.active = this['active'];
                                userAlert.alert_start_date = moment(this['alert_start_date']).format("MM/DD/YY");
                                userAlert.alert_end_date = moment(this['alert_end_date']).format("MM/DD/YY");
                                userAlert.OpenSectionID = this['OpenSectionID'];
    // modify the dropdown to assign unique id and match selection
                                var selectAlert = DDAlertFrequency.replace("DDAlertFreq", "DDAlertFreq_" + userAlert.alert_idx).replace('"' + userAlert.alert_frequency + '"', '"' + userAlert.alert_frequency + '" selected');
                                var tblRow = ''
                                    + userAlert.Name
                                 + ''
                                + ''
                                 + ''
                                + ''
                                 + ''
                                + ''
                                 + ''
                                + selectAlert //modified Select2 dropdown
                                 + ''
                                 + ''
                                 + ''
                                + userAlert.alert_start_date
                                 + ''
                                + userAlert.alert_end_date
                                 + ''
                               + ''
                                 + ''
                                tbl.append(tblRow);
                                initSelect2(); //call the function to initialize all Select2 dropdowns created
                            });
                        },
                        error: function (err) {
                            console.log('Error (RetrieveUserAlerts): ' + JSON.stringify(err, null, 2));
                        }
                    });
                };
    

    Sorry for leaving in the extraneous stuff - I commented on areas of interest. Hope this helps others!

提交回复
热议问题