0

how to save these selected checkbox group values into any text field as comma seperated values of account record using apex in lwc.

HTML:

<lightning-checkbox-group name="Checkbox Group"
                          label='Event'
                          options={options}
                          value={value}
                          onchange={handleChange}></lightning-checkbox-group>
                          <p>Selected Values are: {selectedValues}</p>
              <lightning-checkbox-group name="Checkbox Group"
                          label='outsideEvent'
                          options={options1}
                          value={value}
                          onchange={handleChange}></lightning-checkbox-group>
                          <p>Selected Values are: {selectedValues}</p>
              <lightning-checkbox-group name="Checkbox Group"
                          label='selected?'
                          options={options2}
                          value={value}
                          onchange={handleChange}></lightning-checkbox-group>
                          <p>Selected Values are: {selectedValues}</p>

JS:
value = [];
get options() {
    return [
        { label: 'Yes', value: 'True' },
        { label: 'No', value: 'false' },
    ];
  }
 handleChange(e) {
        this.value = e.detail.value;
    }
  get selectedValues() {
    return this.value.join(',');
}
get options1() {
    return [
        { label: 'Yes', value: 'option3' },
        { label: 'No', value: 'option4' },
    ];
}
 handleChange(e) {
        this.value = e.detail.value;
    }
get options2() {
    return [
        { label: 'Yes', value: 'option5' },
        { label: 'No', value: 'option6' },
    ];
}
 handleChange(e) {
        this.value = e.detail.value;
    }
APEX:

public class testUtility{
@AuraEnabled
public static List<Account> saveAccList(Account acc){
insert acc;
}
}

1 Answer 1

0

All of the inputs share the same group name, and the same value property. This is going to cause problems, because each group is unaware of other checkboxes by the same name in other groups. You also should not declare the same function more than once, as you have no guarantee which function will be called (but definitely only one will be called).

Here's a minimally fixed version for the selection code:

import { LightningElement, track } from "lwc";

export default class App extends LightningElement {
  @track values = {
    Selected: [],
    OutsideEvent: [],
    Event: []
  };
  get options() {
    return [
        { label: 'Yes', value: 'True' },
        { label: 'No', value: 'false' },
    ];
  }
  get options1() {
    return [
        { label: 'Yes', value: 'option3' },
        { label: 'No', value: 'option4' },
    ];
  }
  get options2() {
      return [
          { label: 'Yes', value: 'option5' },
          { label: 'No', value: 'option6' },
      ];
  }
  get selectedValues() {
    return Object.values(this.values).flat().join(',');
  }
  handleChange(e) {
      this.values[e.target.name] = e.detail.value;
  }

}

<template>
    <lightning-checkbox-group name="Event" value={values.Event} label='Event' options={options} onchange={handleChange}>
    </lightning-checkbox-group>
    <p>Selected Values are: {selectedValues}</p>
    <lightning-checkbox-group name="OutsideEvent" value={values.OutsideEvent} label='outsideEvent' options={options1}
        onchange={handleChange}>
    </lightning-checkbox-group>
    <p>Selected Values are: {selectedValues}</p>
    <lightning-checkbox-group name="Selected" value={values.Selected} label='selected?' options={options2}
        onchange={handleChange}>
    </lightning-checkbox-group>
    <p>Selected Values are: {selectedValues}</p>
</template>

Demo.


As far as your Apex code is concerned, I don't really know this is involved in your main selection code. In general, you'll want to create the appropriate type of data and pass it in to the method.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .