0

I have to iterate over an array and populate a select box with it, but I dont want to append it to an element in html. I want to use the selectbox in a variable as html.

How can I use the selectbox function outcome inside the variable frm_str as html.

Example:

//users array

{"users":{"48f8fd57-5116-419c-b43a-cff90a4ae39b":"lolololo","4ab9f52a-d445-4b7a-b26d-1f5bc73ea751":"koen","c1d427f2-a39e-4b20-b531-3c325de96d85":"lolololo"}}

        // Setup module
    // ------------------------------
    
    var FullCalendarAdmin = function () {
    
        //
        // Setup module components
        //

var _componentRender = function () {
          _componentFullCalendarAdmin();
        };
    
        //
        // Bootbox message
        //
        var _bootboxContent = function (event, users) {
    
            var _selectUsers = function () {
                var userStr = [];
    
                jQuery.each(users, function (index, item) {
                    userStr.push('<option value="' + index + '">' + item + '</option>');
                });
    
                var object = $('<div/>').html(userStr).contents();
    
                return object;
            }
    
            var frm_str = '<form action="">' +
                '<div class="form-row">' +
                '<label class="col-md-auto col-form-label">Description</label>' +
                '<div class="col-md-8">' +
                '<select>' +
                _selectUsers +
                '</select>' +
                '</div>' +
                '</div>' +
                '</form>';
    
            var object = $('<div/>').html(frm_str).contents();
    
            return object
        }
    
    
        // Basic calendar
        var _componentFullCalendarAdmin = function (events, users) {
    
            // Define element
            var calendarAgendaViewElement = document.querySelector('.fullcalendar-agenda-admin');
    
            // Initialize
            if (calendarAgendaViewElement) {
                var calendarAgendaViewInit = new FullCalendar.Calendar(calendarAgendaViewElement, {
                    plugins: ['dayGrid', 'timeGrid', 'interaction'],
                    selectable: true,
                    select: function (event) {
                        bootbox.dialog({
                            title: 'Nieuwe afspraak.',
                            message: _bootboxContent(event, users),
                            buttons: {
                                success: {
                                    label: 'Save',
                                    className: 'btn-success',
                                    callback: function () {    
                                      
                                                calendarAgendaViewInit.addEvent(eventData);

    
    
                                        bootbox.alert({
                                            title: 'afspraaknaam: ' + name,
                                            message: 'Uw afspraak is toegevoegd.'
                                        });
                                    }
                                }
                            }
                        }
                        );
                        calendarAgendaViewInit.unselect();
                    },
                });
    
                calendarAgendaViewInit.render()
    
            }
        };
    
        //
        // Return objects assigned to module
        //
    
        return {
            init: function () {
                _componentRender();
            }
        }
        
    }();
    
    
    // Initialize module
    // ------------------------------
    
    document.addEventListener('DOMContentLoaded', function () {
        FullCalendarAdmin.init();
    });
        <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/fullcalendar/3.10.0/fullcalendar.min.js"></script>
    <script src="https://unpkg.com/@fullcalendar/core/main.min.js"></script>
    <script src="https://unpkg.com/@fullcalendar/interaction/main.min.js"></script>
    <script src="https://unpkg.com/@fullcalendar/daygrid/main.min.js"></script>
    <script src="https://unpkg.com/@fullcalendar/timegrid/main.min.js"></script>
    <script src="https://unpkg.com/@fullcalendar/moment/main.min.js"></script>
    <script src="https://unpkg.com/@fullcalendar/bootstrap/main.min.js"></script>
    <script src="https://unpkg.com/@fullcalendar/timeline/main.min.js"></script>

    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/fullcalendar/3.10.0/fullcalendar.min.css" />
DutchPrince
  • 333
  • 2
  • 16
  • 4
    I made you a snippet. Please update it with relevant JS (example users) HTML and CSS – mplungjan Jun 13 '19 at 11:17
  • I updated my code – DutchPrince Jun 13 '19 at 11:30
  • 1
    That did not make it work as a [mcve] Can you cut the code down to the absolute minimum? It does not run as you can see – mplungjan Jun 13 '19 at 11:34
  • What is the idea behind `var object = $('
    ').html(frm_str).contents(); object.find('.openPicker').on('click', function (e) {` Don't you mean `var object = $('
    ').html(frm_str); $(document).on('click',.openPicker', function (e) {`
    – mplungjan Jun 13 '19 at 11:37
  • Why is this duplicate lol – DutchPrince Jun 13 '19 at 11:38
  • 1
    You certainly will not get any event handling on `var object = $('
    ').html(frm_str).contents(); object.find('.openPicker').on('click', function (e) {` so that is at least ONE issue handled. What exactly do I have to do to see what you are asking? Your snippet still get errors
    – mplungjan Jun 13 '19 at 11:42
  • 2
    And users should be an array - the example does not run and the example users are not useful... Please help us help you by creating, and I repeat myself here a [mcve] – mplungjan Jun 13 '19 at 11:44
  • "var object = $('
    ').html(frm_str).contents(); object.find('.openPicker').on('click', function (e) {" this works i only need a function that returns a html string or a variable that does the same with a populated select box
    – DutchPrince Jun 13 '19 at 17:51
  • I can't itterate over my array inside a variable so i need to somehow make the function return all the html output, or i have to make a variable output the values. – DutchPrince Jun 13 '19 at 18:05

1 Answers1

0

I fixed my problem using join()

I only needed the values from the array as plain html so figured out using join could fix the problem :)

Thanks for all the help especially user:mplungjan

let _bootboxContent = function (event, users) {

    let _selectUsers = function () {
        var userArray = [];

        jQuery.each(users, function (index, item) {
            userArray.push('<option value="' + index + '">' + item + '</option>');
        });

        const userStr = userArray.join(" ");

        return userStr;
    }

    const frm_str = '<form action="">' +
        '<div class="form-row">' +
        '<label class="col-md-auto col-form-label">Description</label>' +
        '<div class="col-md-8">' +
        '<select>' +
        _selectUsers() +
        '</select>' +
        '</div>' +
        '</div>' +
        '</form>';

    const object = $('<div/>').html(frm_str).contents();

    return object
}
DutchPrince
  • 333
  • 2
  • 16