0

I am using an Angular Wrapper for JSON Editor like this:

<div *ngFor="let act of editedActions" class="w-100-p p-24">
  {{act.test_step_id}}
  <json-editor [options]="editorOptions" [(data)]="act.action_json" [(eventParams)]="act.test_step_id" (jsonChange)="changeStepActions($event)"></json-editor>
  <button mat-raised-button class="w-100-p mt-24" color="primary" (click)="editRecordJson(act.test_step_id)">
    <span>Update</span>
  </button>
</div>

The problem is that eventParams should be different for each editor but it is not varying.

I think problem is this component code (but not sure) (This line is in the component taken from github):

@ViewChild('jsonEditorContainer', { static: true }) jsonEditorContainer: ElementRef;

The component is behaving like a singleton. Any help?

Edit: I edited this repo and added jsonchange event. Details here

4
  • 2
    where do you define jsonEditorContainer in your template, I can't see it. Also if you want multiple instances from within your ngFor you should use ViewChildren Commented Feb 3, 2020 at 13:49
  • it is defined in the component in the github link: github.com/mariohmol/ang-jsoneditor/blob/master/ang-jsoneditor/… Commented Feb 3, 2020 at 14:30
  • 1
    What is eventParams? Is an Input\Output of ang-jsoneditor? a custom directive? I can't find anything about it. Commented Feb 3, 2020 at 16:21
  • I edited it to try if i can get parameter by manually passing to event. It is same for the editRecordJson event. Commented Feb 3, 2020 at 16:28

4 Answers 4

1

You may want to use @ViewChildren with a direct reference to the component instead of a template variable string, to get all the JSON editors references:

@ViewChildren(JsonEditorComponent) jsonEditorContainers: QueryList<ElementRef>;

// ...

jsonEditorContainers.find(...);

It returns a QueryList that allows you to iterate through all ElementRef, and monitor the changes with an Observable changes.

Sign up to request clarification or add additional context in comments.

4 Comments

But @viewchildren is in the component that is not written by me. It is from open source component.
Ok I missed that, I modified my answer. I think you should query with (JsonEditorComponent) instead of a template variable string ('jsonEditorContainer') like they demonstrate in the repository readme's example.
I can get instances of editors. But still event is not sending parameters (or eventParams) dynamicly. How can I know which data is edited?
Then I don't know how to solve your problem. I have the same questions than PierreDuc, where does eventParams come from? If it does not exist on the library's component you cannot bind to it.
1

What is eventParams? What is jsonChange? I could be wrong, but data doesn't seem to be two way bindable either, according to the source code.

It seems like you might be looking for something like this:

<div *ngFor="let act of editedActions" class="w-100-p p-24">
  <json-editor [options]="editorOptions" 
               [data]="act.action_json"
               (change)="changeStepActions($event, act.test_step_id)">
  </json-editor>
</div>

You can then read the test_step_id in your changeStepActions method. If this works, I don't know how you made it compile in the first place.. are you using a CUSTOM_ELEMENTS_SCHEMA?

1 Comment

jsonchange is an event added by me. You can see in this pull reuest : github.com/mariohmol/ang-jsoneditor/pull/58 . eventparams is just an @input to controller. I tried return it at jsonchage event but it also did not work.
1

Its not necessary to use @ViewChildren for that you have to rewrite the entire code of component, make sure while using @ViewChild you pass correct editor reference.

As following

@ViewChild('carEditor' ) carEditor: JsonEditorComponent;
@ViewChild('mobileEditor') mobileEditor: JsonEditorComponent;

Stackblitz example for refernce :

Click here for code example

Comments

1
To use multiple jsoneditors in your view you cannot use the same editor options.

You should have something like:

<div *ngFor="let prd of data.products" class="w-100-p p-24" >
  <json-editor [options]="makeOptions()" [data]="prd" (change)="showJson($event)"></json-editor>
</div>
makeOptions = () => {
  return new JsonEditorOptions();
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.