Search code examples
javascriptphplaravellaravel-bladefullcalendar-3

How do you send an array to a modal for use in Laravel 5.5 blade with in_array for checkboxes to set the checked attribute?


I am trying to pass an array to a modal after I get a response from an ajax call on the eventClick() function in fullcalendar v3

// in my blade javascript section

This is getting good data but I need to send the tagcodes to the modal and an array like this..

$tagCodes 

which should be an array like this..

'AM','FLEX'

A. I don't know how to make it a usable array.

B. I need to send it to the front-end as a variable called $tagCodes which is an array.

eventClick: function (event, jsEvent, view) {
    let id = event.id;
    let tag_codes = event.tag_codes;
    console.log(tag_codes.split(" ").join(",").replace(/,\s*$/, ""));

    ( which is giving me this AM,FLEX )

    $.ajax({
        url: '/agenda/' + id,
        type: 'GET',
        data: {},
        dataType: 'json',
        success: function (data) {
            $('#calendarModal input[name=id]').val(data.calendar.id);
            $("#calendarModal .tagCodes").html(tag_codes.split(" ").join(",").replace(/,\s*$/, ""));
            $('#calendarModal').modal();
        }
    });
}

// in my modal section

<div class="col-12">

    <div class="row">
        <div class="col-12 tagCodes"></div>
        @foreach($tags as $tag)
        <div class="col-3 text-center">
            <label>
                <input type="checkbox"
                id="tag-{{ $tag->id }}" name="tags[]" value="{{ $tag->id }}"
                @if( in_array($tag->code, [THIS IS WHERE IM TRYING TO SEND THE ARRAY]) ) checked @endif
                > {{ $tag->code }}
            </label>
        </div>
        @endforeach
    </div>
</div>


Solution

  • Change this

    <input type="checkbox"
                    id="tag-{{ $tag->id }}" name="tags[]" value="{{ $tag->id }}"
                    @if( in_array($tag->code, [THIS IS WHERE IM TRYING TO SEND THE ARRAY]) ) checked @endif
                    >
    

    to

     <input type="checkbox" id="tag-{{ $tag->code }}" name="tags[]" value="{{ $tag->id }}">
    

    then in JavaScript do this.

    var codes = tag_codes.split(" ");
    for(var i=0;i<codes.length;i++)
    $('#tag-'+codes[i]).prop('checked', true);