Saturday, 8 May 2021

select input data from table based on the existing variant type of object

In my project I have a problem that I find in a table where I go to select types of intervention. There are interventions that are unique, others that have variants. The problem arises when the selection of the variant (for example) c, the intervention with the variant of type a is always obtained. Starting from the beginning I have a class of this type:

export class Intervention {
    id: number
    code: string
    description: string
}
---------------------------------------------
import { Intervention } from "./intervention"

export class AssociationIntervention {
    id: number
    intervention: Intervention
    price: number
    variant: string
    variants?: string[] //used for contain all variants

An example of present data in Intervention is this:

CREATE TABLE `intervention` (
  `id` bigint(20) NOT NULL,
  `code` varchar(255) DEFAULT NULL,
  `description` varchar(255) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

INSERT INTO `intervention` (`id`, `code`, `description`) VALUES
(1, 'A', 'good'),
(2, 'B', 'ok'),
(3, 'C', 'no');

An example of present data in AssociationIntervention is this:

CREATE TABLE `association_intervention` (
  `id` bigint(20) NOT NULL,
  `intervention_id` bigint(20) DEFAULT NULL,
  `price` double NOT NULL,
  `variant` varchar(255) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

INSERT INTO `association_intervention` (`id`, `intervention_id`, `price`, `variant` ) VALUES
(1, 1, 22.16, NULL),
(2, 2, 18.17, 'a'),
(3, 2, 32.15, 'b'),
(4, 3, 10.29, NULL);

ALTER TABLE `association_intervention`
  ADD CONSTRAINT `FKc9gs2ok9kwfp1b7l1r0lvaapu` FOREIGN KEY (`intervention_id`) REFERENCES `intervention` (`id`),
 

The table on my page looks like this:

<table class="table">
      <thead>
        <tr>
          <th scope="colgroup">Code</th>
          <th scope="colgroup">Price</th>
          <th scope="colgroup">Select</th>
        </tr>
      </thead>
      <tbody>
        <ng-container *ngFor="let type of interventionVar; index as i">
          <tr *ngFor="let variant of type.variants; index as j">
            <td> - </td>
            <td></td>
            
            <td>
              <div class="custom-control custom-checkbox">
                <input type="checkbox" class="form-check-input" (click)="press(type, j)">
              </div>
            </td>
          </tr>
        </ng-container>
      </tbody>
    </table>

The method in ts is this:

interventionVar: AssociationIntervention[]
selectedElement: AssociationIntervention[] = []

press(selected: AssociationIntervention[], variant: number) {
    const newInterv = Object.assign({}, selected)
    this.selectedElement.push(newInterv)
    console.log(this.selectedElement[0])
}

The problem is that even if with the above data I went to select intervention B - b, I would always select intervention B - a. How can I solve?



from select input data from table based on the existing variant type of object

No comments:

Post a Comment