content logo

Bootstrap Grids:

Dynamic Bootstrap Grids with Javascript

The appearance of a website is an important factor in its overall score and popularity. There are also many factors that affect a website’s appearance. Organization has a significant impact on this matter as it makes the pages look less cluttered and cleaner. Again, there are many methods you can adopt to improve the organization of your web pages but one that is extremely effective is using Bootstrap grid layouts. You can design your own grid but it takes a lot of time and effort to create a professional Bootstrap grid.

This is why we are offering you a Bootstrap JavaScript grids layout code in this post for free. It allows you to sort all your data in rows or vertical columns with ease. Most Bootstrap grids are usually fixed in a screen but the following dynamic Bootstrap columns code helps you manage the system dynamically. Our dynamic Bootstrap rows let you change the order with a simple click which is really valuable if you like to customize a lot. You can get this dynamic Bootstrap cols right now by scrolling down to the code section. The best part is that all of our codes are completely free of charge and you don’t have to pay anything for it.

#Dynamic Bootstrap Cols #Dynamic Bootstrap Rows #Bootstrap Javascript Grids #Dynamic Bootstrap Columns

<!-- This script got from www.frontendfreecode.com -->
Videos Per Row: <input data-bind="value: videosPerRow"/> 
<button data-bind="click: addVid">Add vid</button>
<div data-bind="foreach: new Array(rowCount())" class="container">
  <div data-bind="foreach: $root.getVideosInRow($index())" class="row">  
    <div data-bind="attr: { 'class': $root.bootstrapColClass }">
      <a href="javascript:;" class="thumbnail">                
        <img data-bind="attr: { src: thumbnail }" />
        <div class="caption">
          <span data-bind="text: title"></span>
        </div>
      </a>
    </div>
  </div>
</div>

rowCount: <span data-bind="text: rowCount"></span><br/>
videosPerRow: <span data-bind="text: videosPerRow"></span><br/>
videos in row: <span data-bind="text: getVideosInRow(1)().length"></span><br/>
bootstrapColClass: <span data-bind="text: bootstrapColClass"></span><br/><a style="font-size: 8pt; text-decoration: none" target="_blank" href="http://www.frontendfreecode.com">Frontend Free Code</a>
                                                                            
var video = function (title) {
  var self = this;
  self.thumbnail = ko.observable("http://placehold.it/250x150");
  self.title = ko.observable(title);
};

var vm = function () {
  var self = this;

  self.videos = ko.observableArray([
  new video("1. Lorem ipsum dolor sit amet."),
  new video("2. Vivamus dictum lacus ac pharetra lacinia."),
  new video("3. Nunc consequat, sapien sed pentesque feugiat, nunc ligula."),
  new video("4. Etiam nec ligula commodo lacus."),
  new video("5. Nunc consequat, feugiat, nunc ligula."),
  new video("6. Etiam nec ligula commodo lacus."),
  new video("7. Etiam nec ligula commodo lacus."),
  new video("8. Etiam nec ligula sapien sed pelltesque lacus.")]);


  self.addVid = function () {
    var newIndex = self.videos().length + 1;
    self.videos.push(new video(newIndex + ". Etiam nec ligula sapien sed pelltesque lacus."));
  };

  self.videosPerRow = ko.observable(3);
  self.bootstrapColClass = ko.computed(function () {
    var colNum = 12 / self.videosPerRow();
    return "col-xs-" + colNum;
  });

  self.rowCount = ko.computed(function () {
    return Math.ceil(self.videos().length / self.videosPerRow());
  });

  self.getVideosInRow = function (rowNumber) {
    return ko.computed(function () {
      // Row Number starts at 0
      var startIndex = rowNumber * self.videosPerRow();
      var endIndex = startIndex + self.videosPerRow();
      return self.videos().slice(startIndex, endIndex);
    });
  };
};

ko.applyBindings(new vm());
<link rel='stylesheet' href='https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css'>
<script src='https://cdnjs.cloudflare.com/ajax/libs/knockout/3.2.0/knockout-min.js'></script>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<!-- This script got from www.frontendfreecode.com -->

<link rel='stylesheet' href='https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css'>
<script src='https://cdnjs.cloudflare.com/ajax/libs/knockout/3.2.0/knockout-min.js'></script>
</head>
<body>
Videos Per Row: <input data-bind="value: videosPerRow"/> 
<button data-bind="click: addVid">Add vid</button>
<div data-bind="foreach: new Array(rowCount())" class="container">
  <div data-bind="foreach: $root.getVideosInRow($index())" class="row">  
    <div data-bind="attr: { 'class': $root.bootstrapColClass }">
      <a href="javascript:;" class="thumbnail">                
        <img data-bind="attr: { src: thumbnail }" />
        <div class="caption">
          <span data-bind="text: title"></span>
        </div>
      </a>
    </div>
  </div>
</div>

rowCount: <span data-bind="text: rowCount"></span><br/>
videosPerRow: <span data-bind="text: videosPerRow"></span><br/>
videos in row: <span data-bind="text: getVideosInRow(1)().length"></span><br/>
bootstrapColClass: <span data-bind="text: bootstrapColClass"></span><br/><div id="bcl"><a style="font-size:8pt;text-decoration:none;" href="http://www.devanswer.com">Frontend Free Code</a></div>
<script>
var video = function (title) {
  var self = this;
  self.thumbnail = ko.observable("http://placehold.it/250x150");
  self.title = ko.observable(title);
};

var vm = function () {
  var self = this;

  self.videos = ko.observableArray([
  new video("1. Lorem ipsum dolor sit amet."),
  new video("2. Vivamus dictum lacus ac pharetra lacinia."),
  new video("3. Nunc consequat, sapien sed pentesque feugiat, nunc ligula."),
  new video("4. Etiam nec ligula commodo lacus."),
  new video("5. Nunc consequat, feugiat, nunc ligula."),
  new video("6. Etiam nec ligula commodo lacus."),
  new video("7. Etiam nec ligula commodo lacus."),
  new video("8. Etiam nec ligula sapien sed pelltesque lacus.")]);


  self.addVid = function () {
    var newIndex = self.videos().length + 1;
    self.videos.push(new video(newIndex + ". Etiam nec ligula sapien sed pelltesque lacus."));
  };

  self.videosPerRow = ko.observable(3);
  self.bootstrapColClass = ko.computed(function () {
    var colNum = 12 / self.videosPerRow();
    return "col-xs-" + colNum;
  });

  self.rowCount = ko.computed(function () {
    return Math.ceil(self.videos().length / self.videosPerRow());
  });

  self.getVideosInRow = function (rowNumber) {
    return ko.computed(function () {
      // Row Number starts at 0
      var startIndex = rowNumber * self.videosPerRow();
      var endIndex = startIndex + self.videosPerRow();
      return self.videos().slice(startIndex, endIndex);
    });
  };
};

ko.applyBindings(new vm());
</script>

</body>
</html>
Preview