简体   繁体   English

动态自举轮播项目

[英]dynamic bootstrap carousel items

How can loop and create dynamic bootstrap carousel items? 如何循环创建动态自举轮播项目?

this the json: 这是json:

"pro": {
            "52": {
              "product_id": "52",
              "category_id": "109",
              "name": "Morder Rock Double 3"
            },
            "54": {
              "product_id": "54",
              "category_id": "109",
              "name": "Morder Rock Double 5"
            },
            "57": {
              "product_id": "57",
              "category_id": "109",
              "name": "Morder Rock Double 8"
            },
            "59": {
              "product_id": "59",
              "category_id": "109",
              "name": "Morder Rock Double 10"
            },
            "61": {
              "product_id": "61",
              "category_id": "109",
              "name": "Mordern Single Cutaway 12"
            },
            "62": {
              "product_id": "62",
              "category_id": "109",
              "name": "Mordern Single Cutaway 13"
            }
}

This my code 这是我的代码

var itemslist = '';
var product_Data ='';
itemslist += '<div class="item active"><div class="container"><div class="row">'
$.each(nvalue.pro, function (lkey, lvalue) {
    product_Data += '<div class="col-md-3">\n\
    ' + lvalue.name + '<img class="img-responsive" 
    src="image/product_images/1.jpg" alt="Los Angeles" /> \n\
    </div>';
});
itemslist += product_Data + '</div></div></div>';

I need it like this : 我需要这样:

<div class="item active">
   <div class="container">
      <div class="row">
         <div class="col-md-3">
            Morder Rock Double 3<img class="img-responsive" src="image/product_images/1.jpg" alt="Los Angeles"> 
         </div>
         <div class="col-md-3">
            Morder Rock Double 5<img class="img-responsive" src="image/product_images/1.jpg" alt="Los Angeles"> 
         </div>
         <div class="col-md-3">
            Morder Rock Double 8<img class="img-responsive" src="image/product_images/1.jpg" alt="Los Angeles"> 
         </div>
         <div class="col-md-3">
            Morder Rock Double 10<img class="img-responsive" src="image/product_images/1.jpg" alt="Los Angeles"> 
         </div>

      </div>
   </div>
</div>

<div class="item">
   <div class="container">
      <div class="row">
        <div class="col-md-3">
            Mordern Single Cutaway 12<img class="img-responsive" src="image/product_images/1.jpg" alt="Los Angeles"> 
        </div>
        <div class="col-md-3">
            Mordern Single Cutaway 13<img class="img-responsive" src="image/product_images/1.jpg" alt="Los Angeles"> 
        </div> 
      </div>
   </div>
</div>

You need to add an index that keeps track of how many elements you have added, and then close one slider element and open the next every fourth image. 您需要添加一个索引,以跟踪添加了多少个元素,然后关闭一个滑块元素,然后每四张图像打开下一个。

I've added a variable i in the code below, and used modulus to check if it is a multiple of 4 to recognise when to do this. 我在下面的代码中添加了一个变量i ,并使用系数检查它是否为4的倍数以识别何时执行此操作。

  i % 4 

Modulus % divides the first number (in this case the variable i ) by the following number, and returns the remainder . 模数%将第一个数字(在这种情况下为变量i )除以随后的数字,并返回余数

You can then use this in an if statement, as if the modulus of a variable has no remainder, then it is exactly divisible by four, and therefore must be a multiple of four. 然后,您可以在if语句中使用它,就像变量的模数没有余数一样,它可以被4整除,因此必须是4的倍数。

// Check every fourth
if ( i % 4 == 0) {

   ...

}

Demo 演示

NB I've added some more JSON data to demonstrate that the code continues to work for large data sets. 注意:我添加了更多的JSON数据,以证明该代码对于大型数据集仍然有效。

 var proJSON = { "pro": { "52": { "product_id": "52", "category_id": "109", "name": "Morder Rock Double 3" }, "54": { "product_id": "54", "category_id": "109", "name": "Morder Rock Double 5" }, "57": { "product_id": "57", "category_id": "109", "name": "Morder Rock Double 8" }, "59": { "product_id": "59", "category_id": "109", "name": "Morder Rock Double 10" }, "61": { "product_id": "61", "category_id": "109", "name": "Mordern Single Cutaway 12" }, "62": { "product_id": "62", "category_id": "109", "name": "Mordern Single Cutaway 13" }, "67": { "product_id": "57", "category_id": "109", "name": "Morder Rock Double 8" }, "68": { "product_id": "59", "category_id": "109", "name": "Morder Rock Double 10" }, "69": { "product_id": "61", "category_id": "109", "name": "Mordern Single Cutaway 12" }, "71": { "product_id": "62", "category_id": "109", "name": "Mordern Single Cutaway 13" } } }; var itemslist = ''; var product_Data =''; itemslist += '<div class="item active"><div class="container"><div class="row">'; // Create index counter var i = 1; $.each(proJSON.pro, function (lkey, lvalue) { product_Data += '<div class="col-md-3">\\n' + lvalue.name + '<img class="img-responsive" src="image/product_images/1.jpg" alt="Los Angeles" />\\n</div>'; // Check every fourth if ( i % 4 == 0) { // Close off tag (hr just for demo purposes) product_Data += '</div></div></div><hr>'; // Add starting tags again product_Data += itemslist; } // Increment index counter i += 1; }); itemslist += product_Data + '</div></div></div>'; $("#sliders").append(itemslist); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div id="sliders"></div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM