Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

freedomfflow's avatar

add 'selected' attribute to 'options' in multi-select based on session data

I have a couple of multi-select drop downs (along with dates). The selected data will be used as search criteria to filter a database query. When the page is submitted (POSTED to the server/controller), I create and execute a query, and then send back to the view with the data from the query. I'm storing the selection criteria in session variables.

HOW can I adjust my form data (the multi-selects) that are populated by Vue ajax calls, to add the 'selected' attribute to an option in the multi-select if that value exists in a session variable... that is, if the option value (via v-repeat loop) is in an array I've stored in a session var, I want to add the 'selected' attribute...

The point is that the selection criteria will persist for the user after the submit the request.. So I need to add the 'selected' attribute to the appropriate options.

Thanks

0 likes
3 replies
janareit's avatar

Just share some code please what you currently have...

1 like
freedomfflow's avatar

This is my javascript - getting data for 3 drop-downs which are report selection criteria: report name, payers, patients. The latter 2 dynamically generated from ajax calls to their db tables.

var reports = new Vue({

el: '#reports',

data: {
    selected: '0',
    options: [
        { text: '-- Choose Report --', value: 0},
        { text: 'Insurance', value: 1 },
        { text: 'Charge By Client', value: 2}
    ],
    payer: '',
    patient: '',
},

computed: {
    errors: function() {
        if (this.selected == 0) {
            return true;
        }

        return false;
    }
    
},

ready: function() {
    this.fetchPayers();
    this.fetchPatients();
},

methods: {
    fetchPayers: function() {
        this.$http.get('/reports/payer_ajax_list', function(payer) {
            this.$set('payer', payer);
        });
    },

    fetchPatients: function() {
        this.$http.get('/reports/patient_ajax_list', function(patient) {
            this.$set('patient', patient);
        });
    }
}

});

HERE is a partial of my view code. For each of the drop-downs, I want whatever was selected in each drop down to show as selected again after the submit returns them to the page. The form submits to 'do_report', where I grab some data from the database, and then return to the same (index) view where I show this partial view, and below it, I show the results of the query from the selection criteria submitted. I was thinking I'd store the selections (in the controller I submit to) into session variables. Therefore, with this strategy, I need to say something like the following for each drop-down 'option': if option.value is in session-array, add attribute 'selected' to that 'option' : NOTE: my firs 2 lines, a <div and my form tag, are getting munged... I capitalized FORM so you'd see it... so ignore the bad syntax there please:

FORM method="POST" action="/reports/do_report" class="form" id="reports-form" {!! csrf_field() !!}
    <div class="page-header">
        <h4 class="page-title">Select Report:</h4>
        <select name="report_select" v-model="selected" class="form-control">
            <option v-repeat="option: options" value="@{{ option.value }}">
                @{{option.text}}
            </option>
        </select>
    </div>

    <div class="row">
        <div class="col-md-6">
            <div class="form-group">
                Start Date
                <input type="date" name="startdate" id="startdate" class="form-control">
            </div>
        </div>
        <div class="col-md-6">
            <div class="form-group">
                End Date
                <input type="date" name="enddate" id="enddate" class="form-control">
            </div>
        </div>
    </div>

    <div class="row" id="payers">
        <div class="col-md-6">
            <div class="form-group">
                Payer
                <select name="payer_select[]" v-model="payers" class="form-control" multiple>
                    <option v-repeat="option: payer" value="@{{ option.payer_name }}">
                        @{{option.payer_name}}
                    </option>
                </select>
            </div>
        </div>
        <div class="col-md-6">
            <span class="show" v-if="selected == 2">
                <div id="patient_div" class="form-group">
                    Patient
                    <select name="patient_select[]" v-model="patients" class="form-control" multiple>
                        <option v-repeat="option: patient" value="@{{ option.patient_full_name }}">
                            @{{option.patient_full_name}}
                        </option>
                    </select>
                </div>
            </span>
        </div>
    </div><!-- /.row -->

    <br>
    <div class="row">
        <div class="col-sm-12">
            <div class="btn-group">
                <button type="submit" class="btn btn-default" id="btn-submit" v-attr="disabled: errors">Search</button>
                <a href="/reports" class="btn btn-default">Cancel</a>
            </div>
        </div>
    </div>


</form>
freedomfflow's avatar

NO NEED TO RESPOND.....

I am no longer submitting to the server. I'm submitting using v-on="submit...." and making an ajax post to my controller to get the data, and thus since I'm staying on the page my previous problem no longer exists...

Please or to participate in this conversation.