Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
760 views
in Technique[技术] by (71.8m points)

html - Adding products in carousel generates new row in Angular4

Working in an Angular4 application,In this I have a carousel were I have shown the popular products .In my case in the default view I have displayed 3 products when I clicked on left or right button it will display another 3 products . currently I have 6 products (3+3) all are static values.

Issue :

When I add another 1 or more product in slider by static code it generates a new row of slides.

I have tried some examples nothing is worked for me.

What I want to do is If I add more products in carousel,carousel must display all the products as the normal slides .

Note : Now I have give the static values ,Actually in my project I will bind all the products from API response which will include image path,productname,price,hidden id.

I have created an stackblitz file there I kept all the static codes.

https://stackblitz.com/edit/angular-mn29mi?file=app%2Fapp.component.html

Dynamic code for binding products from API.

<div class="col-sm-4" *ngFor="let slider of productData; let i = index">
    <div class="card">
       <div class="card-img-top card-img-top-250 one" >
           <img class="img-fluid" [src]="slider['PRODUCT_IMAGE']" alt="Carousel 1">
           <img routerLink="/my-cart" (click)="getProductName(Pname1)" class="img-fluid two" [src]="slider['PRODUCT_IMAGE_ONHOVER']" alt="Carousel 1">
       </div>
           <div class="card-block pt-2">
               <div class="col-sm-12 text-center card-text">
                   <span>{{slider.PRODUCT_NAME}}</span>
                   <br>
                   <input type="hidden" value="{{slider.PRODUCT_ID}}" #Pname1>
                   <br>
                   <span>{{slider.PRODUCT_PRICE}}</span>
               </div>
           </div>
   </div>
</div>

Please help me to dynamically bind the products from API and make the slider will not generate new rows when number of product increase.

Component code :

export class LandingpageComponent {

  product_Name: any;
  productData: any;
  sliderPaths: any;
  sideMenuSliders : any;
  slider_Active_Item: any;
  side_Menu_Active_Item : string;
  slider_Sliding_Item : any;
  side_Menu_Slider_Image : any;

  constructor(private CartdataService: CartdataService, private router: Router,) {
    this.router.events.subscribe(
      () => window.scrollTo(0, 0) );
  }

  chunks(array, size) {
    let results = [];
    results = [];
    while (array.length) {
        results.push(array.splice(0, size));
    }
    return results;
}
  getProductName(Pname: any) {
    this.CartdataService.get_Product(Pname.textContent);
  }

  ngOnInit() {
    this.CartdataService.get_New_Products().subscribe(
      data => {
        this.productData = data
      });
  }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Here you go, All you need to do is chunk your data array in to parts of 3 , then loop through it twice this way.

Component Side :

ngOnInit() {
    this.CartdataService.get_New_Products().subscribe(
    data => {
        this.productData = this.chunks(data,3);
    });
}

chunks(array, size) {
    let results = [];
    results = [];
    while (array.length) {
        results.push(array.splice(0, size));
    }
    return results;
}

Template side :

<div class="row row-equal carousel-item m-t-0" *ngFor="let chunkProducts of productData;">
    <div class="col-sm-4" *ngFor="let slider of chunkProducts;">
        <div class="card">
        <div class="card-img-top card-img-top-250 one" >
            <img class="img-fluid" [src]="slider['PRODUCT_IMAGE']" alt="Carousel 1">
            <img routerLink="/my-cart" (click)="getProductName(Pname1)" class="img-fluid two" [src]="slider['PRODUCT_IMAGE_ONHOVER']" alt="Carousel 1">
        </div>
            <div class="card-block pt-2">
                <div class="col-sm-12 text-center card-text">
                    <span>{{slider.PRODUCT_NAME}}</span>
                    <br>
                    <input type="hidden" value="{{slider.PRODUCT_ID}}" #Pname1>
                    <br>
                    <span>{{slider.PRODUCT_PRICE}}</span>
                </div>
            </div>
        </div>
    </div>
</div>

WORKING DEMO


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...