javascript - 如何使用 CSS 和 html 在容器内动态创建和排列复选框?

标签 javascript html angular typescript

我想创建一个容器,它将复选框排列到表列中。 目前,在下面的 html 代码中,它向我显示以下输出

Code Output

以下是我的代码:

<label class="control-label text-success">Add Applications</label>
<div class="container border">
  <div class="row">
    <div class="col-sm anyClass">
      <div
        class="form-check form-check-inline text-nowrap col-md-3 pl-2"
        *ngFor="let name of applicationList; let srno = index"
        [attr.data-index]="srno"
      >
        <div *ngIf="srno == 1||srno == 4||srno == 7||srno == 10">
          <span class="ml-4"></span>
        </div>
        <div *ngIf="srno == 2||srno == 5||srno == 8||srno == 11">
          <span class="ml-5"></span>
        </div>
        <input type="checkbox" name="{{name}}" value="{{name}}" />
        <label class="pt-2 pl-1">{{name}}</label>
      </div>
    </div>
  </div>
</div>

我的ts是:

applicationList = [
  'Application 6',
  'Application 7',
  'Application 8',
  'Application 9',
  'Application 10',
  'Application 11',
  'Application 12',
];

这里我根据数组列表中的 srno 编号对边距值进行了硬编码。 我希望这是动态计算的。 请帮助我,提前致谢。

最佳答案

您可以删除硬代码并使用flex来解决这个问题。

CSS:

    .box {
        height: 300px;
        display: flex;
        flex-direction: column;
        flex-wrap: wrap;
    }
    .box>* {
        flex: 1 1 80px;
    }
     <div class="box">
        <div>One</div>
        <div>Two</div>
        <div>Three</div>
        <div>Four</div>
        <div>Five</div>
        <div>Six</div>
        <div>Seven</div>
        <div>Eight</div>
        <div>Nine</div>
      </div>

有关柔性包装的更多信息:https://css-tricks.com/almanac/properties/f/flex-wrap/ 有关弯曲方向的更多信息:https://developer.mozilla.org/it/docs/Web/CSS/flex-direction

完整的 Flex 简单指南:https://css-tricks.com/snippets/css/a-guide-to-flexbox/

编辑我意识到现在不是一个垂直列表。这里是片段:

      .box {
        display: flex;
        flex-wrap: wrap;
    }
    .box>* {
        flex: 1 1 33%;
    }
      <div class="box">
        <div>One</div>
        <div>Two</div>
        <div>Three</div>
        <div>Four</div>
        <div>Five</div>
        <div>Six</div>
        <div>Seven</div>
        <div>Eight</div>
        <div>Nine</div>
      </div>

关于javascript - 如何使用 CSS 和 html 在容器内动态创建和排列复选框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53375992/

相关文章:

angular - 如何使用 Angular2 Component/Typescript 切换 bootstrap 4 模式

javascript - 以编程方式搜索 JQuery 数据表中的文本

javascript - 在 bool 变量之间切换

jQuery 正则表达式,文件名中允许多个点

html - 为当前用户自动检测来自多个来源的最佳质量 - jwplayer

Angular:如何将私有(private)方法纳入代码覆盖范围?

javascript - 窗口焦点在 Javascript 中不起作用

javascript - 如何使用SeleniumWebdriver和Python通过滚动找到网页上的所有元素

html - 选择框以填充剩余空间

node.js - node.isAncestor 不是一个函数