4

I have one array that contain all the office list. another array is user selected list. so now I want to display all officelist and if the value in selected list is the same with office list then the checkbox will be checked. This is how I did it.
Code

<div *ngFor="let item of officeLIST">
  <div *ngFor="let officeDATA of allOffice.office">
    <div *ngIf="item.office_id == officeDATA.office_id">
      <input #officeArray type="checkbox" id="officeArray" name="officeArray" class="form-control" value="{{item.officename}}"
        checked> {{(item.officename == "" ? "--No data--" : item.officename)}}
    </div>
    <div *ngIf="item.office_id != officeDATA.office_id">
      <input #officeArray type="checkbox" id="officeArray" name="officeArray" class="form-control" value="{{item.officename}}">      {{(item.officename == "" ? "--No data--" : item.officename)}}
    </div>
  </div>
</div>

and the result isenter image description here

My officeLIST

this.officeLIST = [
  { "office_id": "1", "officename": "Sun Dept" },
  { "office_id": "2", "officename": "Moon" },
  { "office_id": "3", "officename": "Stars" }
]

allOffice.office array

"office": [
    {
      "office_id": "1",
      "officename": "Sun Dept"
    },
    {
      "office_id": "2",
      "officename": "Moon Dept"
    }
  ]
8
  • can you show us the data ofofficeLIST and allOffice.office , just by console.log(), if there is any error in console.post it here :) Commented Apr 7, 2017 at 4:53
  • might be in your *ngIf statement having some issue Commented Apr 7, 2017 at 4:56
  • @IsuruAb take a look my update question Commented Apr 7, 2017 at 5:14
  • @parvezalamkhan i couldnt find what the wrong.. :( Commented Apr 7, 2017 at 5:15
  • i declare office as string in the class, should i change it to array? Commented Apr 7, 2017 at 5:26

2 Answers 2

3

use this solution.this works fine for me. I have done all the things in constructor .if you want this in a method simply use the code block inside the constructor.

this is my ts file:

  officeLIST: Array<any> = [
    { "office_id": "1", "officename": "Sun Dept" },
    { "office_id": "2", "officename": "Moon" },
    { "office_id": "3", "officename": "Stars" }
  ];


  office: Array<any> = [
    {
      "office_id": "1",
      "officename": "Sun Dept"
    },
    {
      "office_id": "2",
      "officename": "Moon Dept"
    }
  ];
  newArray: Array<any> = [];
  constructor() {
    for (var i = 0; i < this.officeLIST.length; i++) {

      var ismatch = false; // we haven't found it yet

      for (var j = 0; j < this.office.length; j++) {

        if (this.officeLIST[i].office_id == this.office[j].office_id) {
          // we have found this.officeLIST[i]] in this.office, so we can stop searching
          ismatch = true;
          this.officeLIST[i].checked = true;//  checkbox status true
          this.newArray.push(this.officeLIST[i]);
          break;
        }//End if
        // if we never find this.officeLIST[i].office_id in this.office, the for loop will simply end,
        // and ismatch will remain false
      }
      // add this.officeLIST[i] to newArray only if we didn't find a match.
      if (!ismatch) {
        this.officeLIST[i].checked = false;//  checkbox status false
        this.newArray.push(this.officeLIST[i]);
      } //End if
    }
    console.log(this.newArray);

  }

this is my html:

<div *ngFor="let item of newArray">
  <input type="checkbox" [checked]="item.checked"> {{item.officename}}
</div>
Sign up to request clarification or add additional context in comments.

1 Comment

@user3431310 :) It is pleasure to help you.
1

Your user selected array will have ids of selected checkboxes. Suppose he had selected Sun Dept and Moon, then

this.userSelectedArray = ["1","2"];

<div *ngFor="let item of officeLIST">
      <input type="checkbox" [attr.checked]="userSelectedArray.indexOf(item.office_id) !== -1 ? true : false">
    </div>

6 Comments

hi all my checkboxed is checked.. forget to mention. selected array is actually default value selected by user. and officeArray is the main array that shows everything. so if the user selected sam with officeArray, the checkbox will be checked. or else it wont
means it will iterate officeArray(total 3 data). the view will show all this. let say user only select 2 data so the checkbox willl only be checked 2 and remaining one is unselected
so when user changes the state of checkbox,its id will be pushed to selectedArray then you can use the code in my ans.
Why is it not working for you. It is working for me.
So is the accepted code working. The fiddle showing only loading is not because of my code its because of some ngfor issue. Open console and see.
|

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.