1

I am having data related to cars coming from an API call which I am displaying in angular ag-grid. I have some default and some customized options which user can select of his own choice. I also need to provide user an option to select the customized option. I am giving user an edit option inside the ag-grid if he clicks on edit one mat-dialog will open up with all customized options with checkbox user can select the option by clicking on the checkbox. if user next time try to edit the customized option the previously selected option checkbox should be checked so that user can know what options he has already selected.

I am facing issue in showing the already selected option as checked, Could anyone please tell me how can I achieve this. I have replicated the same scenario under this link

https://stackblitz.com/edit/ag-grid-with-edit-funcion?file=src%2Fapp%2Fapp.component.ts

I am using below data to display in ag-grid row if a user selects edit of first row the engine and seat-covers checkbox should be automatically selected .

{
  no: 1,
  make: "Toyota",
  model: "Celica",
  price: 35000,
  fields: ["engine", "seat-covers"]
},
{
  no: 2,
  make: "Ford",
  model: "Mondeo",
  price: 32000,
  fields: ["engine", "body-material", "seat-covers"]
},
{
  no: 3,
  make: "Porsche",
  model: "Boxter",
  price: 72000,
  fields: ["engine", "body-material", "seat-covers"]
}
Zahid Hussain
  • 981
  • 2
  • 13
  • 28

1 Answers1

1

you need, when create the formArray use the value of this.data.selectedRow.fields

  buildTaskFields() {
    const arr = this.allColumns.map(element => {
      return this.fb.control(this.data.selectedRow.fields.indexOf(element)>=0);
    });
    return this.fb.array(arr);
  }

See that the formArray gets the values, e.g. [true,false,false,true]

In submitButton we form an array using the value of the formArray

  onSave() {
    const value=this.businessConfigForm.value.taskFieldsArray
    this.data.selectedRow.fields=this.allColumns.filter((x,index)=>value[index])
    this.dialogRef.close(null);
  }

See that, as we pass as data the object itself of the row, we can change in the dialog component and that we needn't mannage "change event" of the checks

Your forked stackblitz

NOTE: you can see another aproach to mannage a list of checkbox in this SO, in this case you needn't create a formArray, just use a variable array=[...this.data.selectedRow.fields] (you need make a copy)

Update if we want to add a new feature,

The first is that allColumns belong to the main component, so we need pass this array in data, so to edit

 this.dialog.open(AddConfigComponent, {
        const selectedRow = evt.data;
        data: {
          data: selectedRow,
          features:allColumns
        }

And in constructor

   if (data) {
      this.data = data.data;
      this.allColumns=data.features;
    }

Well, now to add a new characteristic again, as we pass the array itself, we can use in save

   this.data.push("another characteristic")
   this.dialogRef.close(null);

Updae 2 if ours characteristics are object type {name:...} and not only string, we need change a few the save and the buildEditTaskFields

  buildEditTaskFields() {
    const arr = this.allColumns.map(element => {
      //value becomes true if exist and object with name equal to the element
      //see how we use find
      const value=this.data.fields.find(f=>f.name==element)!=null;

      return this.fb.control(value);
    });
    return this.fb.array(arr);
  }

In save, after filter the array AllColumns, we "map" the array of string to an array of object, some like

  onSave() {
    const value = this.businessConfigForm.value.taskFieldsArray;
    //the filter makes, e.g. the result is ["engine", "body-material"]
    this.data.fields = this.allColumns.filter(
      (x, index) => value[index]
      //the map becomes to [{name:"engine"},{name:"body-material"}]
    ).map(x=>({name:x}));
    this.dialogRef.close(null);
  }
Eliseo
  • 50,109
  • 4
  • 29
  • 67
  • Thanks for your help could you please check this link https://stackblitz.com/edit/ag-grid-with-edit-funcion1?file=src/app/add-config/add-config.component.ts I have added one more button to add features If user clicks on add button all the options should be shown if users clicks on edit button previously selected option should be checked – Zahid Hussain Jan 15 '21 at 07:55
  • 1
    the first you need, if you want to add new feature is that the variable allColumns belongs to the main component, I updated the answer to give you some clue, but I can't see in the link where you add a new feature – Eliseo Jan 15 '21 at 08:38
  • thank for your help could you please check this link https://stackblitz.com/edit/ag-grid-with-edit-funcion1?file=src%2Fapp%2Fapp.component.ts I have changed the fields data from string to objects could you please help me in this and i have added the add button is above ag grid – Zahid Hussain Jan 15 '21 at 15:03
  • 1
    I don't know if the button "Add CustomFeature" you want to add a new Feature or a new row to the table. About change field data I updated the answer and make a stackblitz https://stackblitz.com/edit/ag-grid-with-edit-funcion1-l8vxnv?file=src%2Fapp%2Fadd-config%2Fadd-config.component.ts – Eliseo Jan 17 '21 at 11:42
  • 1
    (In the stackblitz I add a button to add a "new Row". Really the code it's a few "spagetti code", but I hope you can understand it and make more "readable",e.g. to add a feature, create another component, or use if the data you pass has "no" to know if you need edit or add a new row – Eliseo Jan 17 '21 at 12:13
  • Thank you so much for your help this worked for me – Zahid Hussain Jan 18 '21 at 05:30