ken mutesh
ken mutesh

Reputation: 45

Make a checkbox selection default out of a group

I want to make the second checkbox always selected by default even on page reload like the image-

enter image description here

The user can select other checkboxes later but by default, the second should be selected always.

Here is the checkbox code-

<template>
    <div>
        <div class="tw-flex tw-flex-wrap">
            <div class="tw-w-full lg:tw-w-1/4">
                <collabmed-loading v-if="!initialised">Loading Patient Info...</collabmed-loading>

                <reception-patient-info
                    class="tw-px-0 tw-pt-0"
                    v-else
                    :patient-id="admissionRequest.patient_id"
                >
                </reception-patient-info>
            </div>

            <div class="tw-w-full lg:tw-w-3/4 lg:tw-pl-5">
                <acemed-card>
                    <acemed-title>
                        Check In Details
                        <template #right>
                            <div v-if="initialised" class="tw-text-lg tw-text-gray-800"><strong>Admission Type:</strong> {{ admissionRequest.admission_type_name }}</div>
                        </template>
                    </acemed-title>

                    <div>
                        <collabmed-loading v-if="!initialised"></collabmed-loading>

                        <div v-else>

                            <div class="tw-flex tw-flex-wrap">
                                <div class="tw-w-full lg:tw-w-1/2">
                                    <div class="tw-flex tw-flex-wrap tw-justify-between">
                                        <div>
                                            <v-tooltip bottom v-if="msetting('inpatient.inpatient_no_prefix')">
                                                <v-btn flat style="min-width: 50px" slot="activator" class="pull-right pt-3">
                                                    {{ msetting('inpatient.inpatient_no_prefix') }}-
                                                </v-btn>
                                                <span>IP Number Prefix</span>
                                            </v-tooltip>
                                        </div>

                                        <div>
                                            <v-text-field
                                                    v-model="obj.admission.inpatient_no"
                                                    class="mr-2"
                                                    label="IP Number"
                                                    hint="Possible Format: #######/YY"
                                            ></v-text-field>
                                        </div>

                                        <div>
                                            <v-tooltip bottom>
                                                <v-btn
                                                        color="primary"
                                                        style="min-width: 50px"
                                                        slot="activator"
                                                        @click="generateIpNumber()"
                                                        :loading="generating"
                                                >
                                                    <v-icon small>mdi-cached</v-icon>
                                                </v-btn>
                                                <span>Generate IP Number</span>
                                            </v-tooltip>
                                        </div>
                                    </div>

                                </div>

                                <div class="tw-w-full tw-pt-5 lg:tw-pt-0 lg:tw-w-1/2 lg:tw-pl-5">
                                    <collabmed-date-time-picker
                                        label="Admission Date"
                                        outline
                                        :maxDate="today"
                                        @input="setAdmissionDate"
                                    ></collabmed-date-time-picker>
                                </div>

                                <div class="tw-w-full lg:tw-w-1/2 tw-pt-5">
                                    <div v-if="! obj.admission.external_doctor">
                                        <div v-if="! obj.admission.doctor_id">
                                            <users-search
                                                :roles-like="['doc']"
                                                :label="'Admission Doctor'"
                                                @results="setDoctor"
                                            ></users-search>
                                        </div>
                                        <p v-else class="pt-3 pl-3 border-bottom subheading">Doctor:</p>
                                    </div>
                                    <p v-else class="pt-3 pl-3 border-bottom subheading">External Doctor:</p>
                                </div>

                                <div class="tw-w-full lg:tw-w-1/2 tw-pt-5 lg:tw-pl-5">
                                    <div v-if="obj.admission.doctor_id">
                                        <p class="pt-3 pl-3 border-bottom subheading">
                                            {{ obj.admission.doctor_name }}

                                            <v-btn small color="red" icon dark @click="unsetDoctor()">
                                                <v-icon small>delete</v-icon>
                                            </v-btn>
                                        </p>
                                    </div>

                                    <div v-else>
                                        <v-text-field
                                                v-model="obj.admission.external_doctor"
                                                class="mx-2"
                                                label="External Doctor"
                                                hide-details clearable
                                        ></v-text-field>
                                    </div>
                                </div>

                                <div class="tw-w-full lg:tw-w-1/2 tw-pt-5">
                                    <v-autocomplete
                                            v-model="obj.admission.ward_id"
                                            :items="wards"
                                            @change="updateBeds()"
                                            item-text="name"
                                            item-value="id"
                                            label="Select a Ward"
                                            hide-details
                                            outline
                                    ></v-autocomplete>
                                </div>

                                <div class="tw-w-full lg:tw-w-1/2 tw-pt-5 lg:tw-pl-5">
                                    <div v-if="wardCashCharge" class="pl-3">
                                        <p class="font-weight-bold title">Ward Charges</p>
                                        <p>Cash Charge: <strong>{{ wardCashCharge | numberFormat }}</strong></p>
                                        <p>Insurance Charge: <strong>{{ wardInsuranceCharge | numberFormat }}</strong></p>
                                    </div>
                                </div>

                                <div class="tw-w-full lg:tw-w-1/2 tw-pt-5">
                                    <v-autocomplete
                                            v-model="obj.admission.bed_id"
                                            :disabled="! obj.admission.ward_id"
                                            :items="beds"
                                            item-text="name"
                                            item-value="id"
                                            label="Select a Bed"
                                            hide-details
                                            outline
                                    ></v-autocomplete>

                                    <p v-if="overbookingAllowed"><em>Over-booking of beds has been allowed in the system</em></p>
                                </div>

                                <div class="tw-w-full lg:tw-w-1/2 tw-pt-5 lg:tw-pl-5">
                                    <v-autocomplete
                                            v-model="obj.admission.charges"
                                            :disabled="! obj.admission.ward_id"
                                            :items="charges"
                                            item-text="name"
                                            item-value="id"
                                            label="Select Other Charge(s)"
                                            class="ml-2"
                                            hide-details multiple
                                            outline chips
                                    ></v-autocomplete>
                                </div>

                                <div class="tw-w-full lg:tw-w-1/2 tw-pt-5">
                                    <v-alert :value="true" v-if="errors.any()" type="error" outline>
                                        <div v-html="errors.display()"></div>
                                    </v-alert>
                                </div>

                                <div class="tw-w-full tw-pt-5">
                                    <v-btn block dark color="primary" class="mt-4" :loading="saveLoader" @click="save()">
                                        Admit Patient
                                        <v-icon class="pl-2">arrow_right_alt</v-icon>
                                    </v-btn>
                                </div>

                            </div>
                        </div>

                    </div>
                </acemed-card>
            </div>

        </div>
    </div>
</template>

    export default {
        props: {
            admissionRequestId: {
                required: true,
            }
        },

        data() {
            return {
                admissionRequestObj: new AdmissionRequest(),
                obj: new Admission(),
                wards: null,
                saveLoader: false,
                beds: [],
                charges: [],
                wardCashCharge: null,
                wardInsuranceCharge: null,
                today: moment(new Date()).format('YYYY-MM-DD HH:MM:ss'),
                overbookingAllowed: false,
                generating: false,
            }
        },

        computed: {
            ...mapGetters([
                'getWards',
            ]),

            admissionRequest() {
                return this.admissionRequestObj.selected
            },

            initialised() {
                return this.wards && this.admissionRequest
            },

            saved() {
                return this.obj.saved
            },

            submitted() {
                return this.obj.form.submitted
            },

            contaminated() {
                return this.obj.form.errorDetected;
            },

            errors() {
                return this.obj.form.errors;
            },

            generatedIpNuber() {
                return this.obj.generatedIpNuber
            }
        },

        watch: {
            contaminated(val) {
                if(val) {
                    this.saveLoader = false
                }
            },

            submitted(val) {
                if(val) {
                    this.saveLoader = false
                }
            },

            saved(val) {
                if(val) {
                    this.saveLoader = false
                    this.dialog = false

                    window.location = route('inpatient.admissions.index').relative()
                }
            },

            admissionRequest(val) {
                if(val) {
                    this.obj.admission.admission_request_id = val.id
                    this.obj.admission.visit_id = val.visit_id
                    this.obj.admission.inpatient_no = val.visit.patient.inpatient_no
                    this.obj.admission.patient_id = val.visit.patient.id
                }
            },

            getWards(val) {
                if(val) {
                    this.wards = this.getWards.data
                }
            },

            generatedIpNuber(val) {
                this.generating = false
                if(val)
                    this.obj.admission.inpatient_no = val
            }
        },

        methods: {
            ...mapActions([
                'setWards',
            ]),

            initialise() {
                this.admissionRequestObj.find(this.admissionRequestId)

                this.setWards()

                this.obj.admission.admission_date = this.today

                this.overbookingAllowed = this.$options.methods.msetting('inpatient.allow_overbooking_of_wards') == 1
            },

            updateBeds() {
                let ward = _.find(this.wards, {id: this.obj.admission.ward_id})

                this.wardCashCharge = ward.cash_cost
                this.wardInsuranceCharge = ward.insurance_cost

                this.beds = _.map(ward.beds, item => {
                    let name = "Bed No. " + item.number
                    ! item.is_available ? name += " (Unavailable)" : '';

                    let disabled = ! item.is_available
                    this.overbookingAllowed ? disabled = false : null

                    return {
                        name: name,
                        id: item.id,
                        disabled: disabled
                    }
                })

                let firstAvailableBed = _.find(this.beds, { disabled: false })
                firstAvailableBed ? this.obj.admission.bed_id = firstAvailableBed.id : null

                if(this.charges && this.charges.length && this.charges.length >=2) {
                      // charges[1] = second item
                     this.obj.admission.charges.push(this.charges[1].id)
                  }

                this.charges = _.map(ward.charges, item => {
                    let name = item.name + " - Kshs. " + item.cost;
                    item.type == 'recurring' ? name += " [RECURRING]" : '';

                    return  {
                        id: item.id,
                        name: name
                    }
                })
            },

            setDoctor(doctor) {
                this.obj.admission.doctor_id = doctor.id
                this.obj.admission.doctor_name = doctor.full_name
            },

            unsetDoctor() {
                this.obj.admission.doctor_id = null
                this.obj.admission.doctor_name = null
            },

            generateIpNumber() {
                this.generating = true
                this.obj.generateInpatientNumber()
            },

            setAdmissionDate(datetime) {
                this.obj.admission.admission_date = datetime
            },

            save() {
                this.saveLoader = true
                this.obj.save()
            }

        },

        mounted() {
            this.initialise()
        }
    }
</script>

<style scoped lang="scss">

</style>

The api data looks like-

enter image description here

When I select bed, I call a method to fetch the charges. Thats where I have placed the code to select the admission charge where second charge item should always be selected.

Upvotes: 0

Views: 67

Answers (1)

Neha Soni
Neha Soni

Reputation: 4674

If you only want to keep the second item by default selected, then on the mounted hook, just find the second item's id in your charges array, and push this id to your v-model variable (obj.admission.charges) and you should see that the second item is always selected.

Here is the demo-

NOTE- I used your API data inside the data property of Vue as I can't send requests to your server. I also assumed the possible structure of obj.admission because you didn't mention it.

<!DOCTYPE html>
<html>
  <head>
    <link href="https://fonts.googleapis.com/css?family=Roboto:100,300,400,500,700,900" rel="stylesheet">
    <link href="https://cdn.jsdelivr.net/npm/@mdi/[email protected]/css/materialdesignicons.min.css" rel="stylesheet">
    <link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/vuetify.min.css" rel="stylesheet">
  </head>
  <body>
    <div id="app">
      <v-app>
        <v-container>
          <v-autocomplete
            v-model="obj.admission.charges"
            :disabled="!obj.admission.ward_id"
            :items="charges"
            item-text="name"
            item-value="id"
            label="Select Other Charge(s)"
            class="ml-2"
            hide-details 
            multiple
            outlined
            chips
            >
          </v-autocomplete>
        </v-container>
      </v-app>
    </div>
    <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/vue.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/vuetify.js"></script>
    <script>
      new Vue({
        el: '#app',
        vuetify: new Vuetify(),
        data() {
         return {
          search: null,
          obj: {
            admission: {
              charges: [],
              ward_id: "something",
            }
          },
          charges: [
           {
            id: 2,
            name: "Nursing Fee",
            cost: "3000.00",
            type: "recurring",
            created_at: "2022-08-23 08:00",
           },
           {
            id: 31,
            name: "Admission Fee",
            cost: "once",
            type: "3000.00",
            created_at: "2022-08-23 08:00",
           },
           {
            id: 32,
            name: "Dr. inpatient visit",
            cost: "3000.00",
            type: "once",
            created_at: "2022-08-23 08:00",
           },
           {
            id: 36,
            name: "Dr. Anne Masicka",
            cost: "3000.00",
            type: "once",
            created_at: "2022-08-23 08:00",
           },
           {
            id: 29,
            name: "Dr. inpatient visit (Dr. Marwa)",
            cost: "3000.00",
            type: "once",
            created_at: "2022-08-23 08:00",
           }
          ]
         }
        },
        mounted() {
         if(this.charges && this.charges.length && this.charges.length >=2) {
         // charges[1] = second item
         this.obj.admission.charges.push(this.charges[1].id)
         }
        }
      })
    </script>
  </body>
</html>

Upvotes: 1

Related Questions