0

Hi,

I have a notification menu list in which i am binding the data which is coming from the REST API. My notification menu will look like this. See below image for clarity.

enter image description here

In the notifications, when ever i click on any of the list item then it should be removed from the list and on the above also there is notification bubble in red color with some count that how many notifications i have received. What i want to achieve is when i click on the notification menu list then the clicked list item should be removed from it also the count should decrease in red color bubble above the bell icon. For example, when i click on notification list item of first item the it should be removed as well as the count should be decreased.

notification.component.ts

try {

      this.currentUser = JSON.parse(this.cookieService.get('user-is-logged-in-details'));
      console.log(this.currentUser.uid);
      this._userService.getBellNotification(this.currentUser.uid)
      .subscribe(data => this.cookieData = data); 
      } catch (e) {
          console.log("Catch Block Error from Notification.Component.ts");
          //console.log("currentUser", this.currentUser.uid);
      }

apiservice.service.ts

getBellNotification(uid: any){
        const urlBell = this.config.apiUrl+'api/supplier-notification/'+uid+'?_format=json';
        let headers = new Headers();        
        headers.append("Content-Type", 'application/json');
        headers.append('Authorization', 'Basic ' + btoa('apiuser:@piUser@2017Supplier'));
        let requestoptions = new RequestOptions({headers: headers});
        return this.http.get(urlBell, requestoptions)
        .map(response => response.json())
        .catch(this.handleErrorObservable);
    }

notification.component.html

<span 
    class="notification" 
    dropdown (onShown)="onShown()" 
    (onHidden)="onHidden()" 
    (isOpenChange)="isOpenChange()">
    <a
        href dropdownToggle 
        (click)="false">
        <i 
        class="glyphicon glyphicon-bell" 
        [ngClass]="cookieData?.search_unread_count != 0 ? 'notification-icon': ' '">
    <span *ngIf="cookieData?.search_unread_count != 0">{{cookieData.search_unread_count}}</span> 
    </i>
    </a>
      <ul 
        *dropdownMenu 
        class="dropdown-menu notify-drop">
          <div 
            class="notify-drop-title">Notifications</div>
            <div 
                class="drop-content" 
                *ngFor="let item of cookieData.search_result">
                <li 
                    data-id="{{item.id}}" 
                    class="notification-items unread">  
                    <i 
                        class="fa fa-dot-circle-o" 
                        aria-hidden="true">
                    </i>
                    <a 
                        data-link="admin-invoice-list" 
                        href="javascript:;">{{item.message}}
                    </a>
                </li>
            </div>
      </ul>
</span>

Can anyone please suggest me how it can be achieved?

youi
  • 1,887
  • 5
  • 20
  • 31

2 Answers2

0

have youtried to attach a (click) function on each

  • so to remove (or do the action you want) on the click of
  • item?...something like:
     <div 
                    class="drop-content" 
                    *ngFor="let item of cookieData.search_result">
                    <li (click)="remove(item)"
                        data-id="{{item.id}}" 
                        class="notification-items unread">  
                        <i 
                            class="fa fa-dot-circle-o" 
                            aria-hidden="true">
                        </i>
                        <a 
                            data-link="admin-invoice-list" 
                            href="javascript:;">{{item.message}}
                        </a>
                    </li>
    

    then in your ts file:

       remove(item:any){
        //ALSO CALL A BACKEND API... TO REMOVE THEM .. THIS IS ONLY FRONT END
        this.cookieData = this.cookieData.filter(xx=>xx.id != item.id);
          this.cookieData.search_unread_count=this.cookieData.search_result.length;
        }
    

    Hope it helps you!!

  • federico scamuzzi
    • 3,708
    • 1
    • 17
    • 24
    • Seems a little bit of overkill to use filter to remove only one element from the array doesn't it? I think splice would be more appropriate ? – nubinub Dec 07 '17 at 13:34
    • yes for sure you can use also splice .. findIndex etc etc .. it is only a suggestion on how get click on the items clicked – federico scamuzzi Dec 07 '17 at 13:37
    0

    @federico scamuzzi ise explained what you missed exactly. but him answer is need some correction for your objects and requirements. I have update that in here, try it and let me know if you have any clarification.

               <div 
                class="drop-content" 
                *ngFor="let item of cookieData.search_result">
                <li (click)="remove(item)"
                    data-id="{{item.id}}" 
                    class="notification-items unread">  
                    <i 
                        class="fa fa-dot-circle-o" 
                        aria-hidden="true">
                    </i>
                    <a 
                        data-link="admin-invoice-list" 
                        href="javascript:;">{{item.message}}
                    </a>
                </li>
    

    and your remove function should be

    remove(item:any){
        //ALSO CALL A BACKEND API... TO REMOVE THEM .. THIS IS ONLY FRONT END
        this.cookieData.search_result = this.cookieData.search_result.filter(xx=>xx.id != item.id);
        this.cookieData.search_unread_count=this.cookieData.search_result.length;
    
        }
    
    Ramesh Rajendran
    • 37,412
    • 45
    • 153
    • 234
    • Ramesh, What is xx in the remove method? – youi Dec 08 '17 at 10:41
    • do search in google like `arrow function` or `lamda expression` – Ramesh Rajendran Dec 08 '17 at 10:58
    • How can we bind the raw html data coming from the json in the template? See this link for reference. https://stackoverflow.com/questions/47708894/property-binding-innerhtml-is-removing-the-anchor-tag-functionality-in-angular?noredirect=1#comment82378865_47708894 – youi Dec 08 '17 at 11:50
    • @youi What about this question and answers? Shall I close this question to `on hold`? – Ramesh Rajendran Dec 08 '17 at 11:54