【问题标题】:Adding div after each 4 loops每4个循环后添加div
【发布时间】:2012-05-24 13:31:45
【问题描述】:

我需要在每5张图片后添加一个div标签,所以html标记应该是这样的:

<div>
      <img src="http://farm1.static.flickr.com/143/321464099_a7cfcb95cf_t.jpg" />
      <img src="http://farm4.static.flickr.com/3089/2796719087_c3ee89a730_t.jpg" />
      <img src="http://farm1.static.flickr.com/79/244441862_08ec9b6b49_t.jpg" />
      <img src="http://farm1.static.flickr.com/28/66523124_b468cf4978_t.jpg" />

</div>
<div>

      <img src="http://farm1.static.flickr.com/163/399223609_db47d35b7c_t.jpg" />
      <img src="http://farm1.static.flickr.com/135/321464104_c010dbf34c_t.jpg" />
      <img src="http://farm1.static.flickr.com/40/117346184_9760f3aabc_t.jpg" />
      <img src="http://farm1.static.flickr.com/153/399232237_6928a527c1_t.jpg" />
    </div>
<div>

      <img src="http://farm4.static.flickr.com/3629/3323896446_3b87a8bf75_t.jpg" />
      <img src="http://farm4.static.flickr.com/3023/3323897466_e61624f6de_t.jpg" />
      <img src="http://farm4.static.flickr.com/3650/3323058611_d35c894fab_t.jpg" />
      <img src="http://farm4.static.flickr.com/3635/3323893254_3183671257_t.jpg" />
    </div>

我必须使用 foreach 循环,这就是要求,除此之外我可以使用任何控制语句,任何人都可以为这个逻辑提供任何帮助,我尝试过这样的事情没有成功,最大图像是 100,就是这样为什么我将 int 初始化为 100

 foreach (var page in images)
 {
    int b = 100 ;
    for (int a = 0 ; a = b ; a++)
    {
        <div><img src="page.name" alt="page.alt" /> </div>
    }
 }

【问题讨论】:

  • 为什么不检查a % 4 == 0并在它为真时打印div?
  • 为什么不能使用带有 div 和 4 个图像的中继器?
  • 希望我可以,foreach循环是工作的要求,我可以在foreach中集成其他循环

标签: c# loops foreach


【解决方案1】:

这些方面的东西应该会让你朝着正确的方向前进

foreach (var page in images)
 {
    int b = 100 ;
    bool first=true;
    bool wroteDivEnd = false;
    for (int a = 0 ; a = b ; a++)
    {
        wroteDivEnd=false;
        if (a% 4==0)
        {
           if (!first)
           { 
              // as long as this is not the first time we have written a 
              //start div, then we need to write and end one
              //write previous div end </div>
              wroteDivEnd=true;
           }
           //write new div start <div>
           //set the flag so we know we have written the first start div
           first=false;
        }
        // write out the images
        <img src="page.name" alt="page.alt" /> 

    }
    //make sure your last </div> is written
    if(!wroteDivEnd)
    { 
     //write out the final missing </div>
    }
 }

【讨论】:

  • 看起来不错,但问题是,如果我得到了 100 张图像,这使得一组 100/4 = 25 个 div 标签,但如果我有 99 张图像,那是一组 24 个,所以应该留下剩下的 3 张图片,但我希望 3 张图片也在 div 中,所以优先级为 4,但如果有 3 张则将 3 张图片包装在 div 中
  • @MrA 这就是这个位的意思 -> //make sure your last &lt;/div&gt; is written 我们必须为您解决所有问题吗?无论如何添加了一个编辑显示你可以如何做到这一点......
【解决方案2】:

你可以计算你的行数,当计数能被四整除时添加&lt;div&gt;

var count = 0;
foreach (var row in myData) {
    // Open before rows 0, 4, 8, 12, 16, and so on
    if (count%4 == 0) {
        // Add <div>
    }
    // Write the line
    // Close after rows 3, 7, 11, 15, and so on
    if (count%4 == 3) {
        // Add </div>
    }
}
// Don't forget the closing row in case the number of images
// is not divisible by four
if (count%4 != 3) {
    // Add </div>
}

【讨论】:

  • 看起来不错,但问题是,如果我得到了 100 张图像,这使得一组 100/4 = 25 个 div 标签,但如果我有 99 张图像,那是一组 24 个,所以应该留下剩下的 3 张图片,但我希望 3 张图片也在 div 中,所以优先级为 4,但如果有 3 张则将 3 张图片包装在 div 中
  • @MrA 这就是最后一个if 的用途!
  • @MrA 这就是最后的位。
【解决方案3】:

试试这个:

var imagesInDiv = 4;
var maxItems = 100;
for (var i = 0; i < images.Length && i < maxItems; ++i)
{
    if (i % imagesInDiv == 0)
    {
        <div>
    }

    <img src="@images[i].name" alt="@images[i].alt" />

    // if this is the last image in a set OR last image that you have OR last image allowed
    if (i % imagesInDiv == imagesInDiv - 1 || i == images.Length - 1 || i == maxItems - 1)
    {
        </div>
    }
}

编辑 1:我添加了一些 cmets

【讨论】:

    【解决方案4】:
    string finalstring = "";
    foreach (var page in images)
    {
       for (int a = 0 ; a < 100 ; a++)
       {
          if (a % 4 != 0)
             str += <img src="page.name" alt="page.alt" />;  
          else
          {
             str = "<div>" + str + "</div>";
             finalstring += str;  
             str = "";
          }
       }
    }
    

    【讨论】: