本文转自:http://stackoverflow.com/questions/21644493/how-to-split-the-ng-repeat-data-with-three-columns-using-bootstrap

The most reliable and technically correct approach is to transform the data in the controller. Here's a simple chunk function and usage.

  1. function chunk(arr, size) {
  2. var newArr = [];
  3. for (var i=0; i<arr.length; i+=size) {
  4. newArr.push(arr.slice(i, i+size));
  5. }
  6. return newArr;
  7. }
  8. $scope.chunkedData = chunk(myData, 3);

Then your view would look like this:

  1. <div class="row" ng-repeat="rows in chunkedData">
  2. <div class="span4" ng-repeat="item in rows">{{item}}</div>
  3. </div>

If you have any inputs within the ng-repeat, you will probably want to unchunk/rejoin the arrays as the data is modified or on submission. Here's how this would look in a $watch, so that the data is always available in the original, merged format:

  1. $scope.$watch('chunkedData', function(val) {
  2. $scope.data = [].concat.apply([], val);
  3. }, true); // deep watch

Many people prefer to accomplish this in the view with a filter. This is possible, but should only be used for display purposes! If you add inputs within this filtered view, it will cause problems that can be solved, but are not pretty or reliable.

The problem with this filter is that it returns new nested arrays each time. Angular is watching the return value from the filter. The first time the filter runs, Angular knows the value, then runs it again to ensure it is done changing. If both values are the same, the cycle is ended. If not, the filter will fire again and again until they are the same, or Angular realizes and infinite digest loop is occurring and shuts down. Because new nested arrays/objects were not previously tracked by Angular, it always sees the return value as different from the previous. To fix these "unstable" filters, you must wrap the filter in a memoize function. lodash has a memoize function and the latest version of lodash also includes a chunk function, so we can create this filter very simply using npm modules and compiling the script with browserify or webpack.

Remember: display only! Filter in the controller if you're using inputs!

Install lodash:

  1. npm install lodash-node

Create the filter:

  1. var chunk = require('lodash-node/modern/array/chunk');
  2. var memoize = require('lodash-node/modern/function/memoize');
  3. angular.module('myModule', [])
  4. .filter('chunk', function() {
  5. return memoize(chunk);
  6. });

And here's a sample with this filter:

  1. <div ng-repeat="row in ['a','b','c','d','e','f'] | chunk:3">
  2. <div class="column" ng-repeat="item in row">
  3. {{($parent.$index*row.length)+$index+1}}. {{item}}
  4. </div>
  5. </div>

Order items vertically

  1. 1 4
  2. 2 5
  3. 3 6

Regarding vertical columns (list top to bottom) rather than horizontal (left to right), the exact implementation depends on the desired semantics. Lists that divide up unevenly can be distributed different ways. Here's one way:

  1. <div ng-repeat="row in columns">
  2. <div class="column" ng-repeat="item in row">
  3. {{item}}
  4. </div>
  5. </div>
  1. var data = ['a','b','c','d','e','f','g'];
  2. $scope.columns = columnize(data, 3);
  3. function columnize(input, cols) {
  4. var arr = [];
  5. for(i = 0; i < input.length; i++) {
  6. var colIdx = i % cols;
  7. arr[colIdx] = arr[colIdx] || [];
  8. arr[colIdx].push(input[i]);
  9. }
  10. return arr;
  11. }

However, the most direct and just plainly simple way to get columns is to use CSS columns:

  1. .columns {
  2. columns: 3;
  3. }
  1. <div class="columns">
  2. <div ng-repeat="item in ['a','b','c','d','e','f','g']">
  3. {{item}}
  4. </div>
  5. </div>

I fix without .row

  1. <div class="col col-33 left" ng-repeat="photo in photos">
  2. Content here...
  3. </div>

and css

  1. .left {
  2. float: left;
  3. }
  4.  

[转]how to split the ng-repeat data with three columns using bootstrap的更多相关文章

  1. Part 6 AngularJS ng repeat directive

    ng-repeat is similar to foreach loop in C#. Let us understand this with an example. Here is what we ...

  2. kaggle Data Leakage

    What is Data Leakage¶ Data leakage is one of the most important issues for a data scientist to under ...

  3. MySQL vs. MongoDB: Choosing a Data Management Solution

    原文地址:http://www.javacodegeeks.com/2015/07/mysql-vs-mongodb.html 1. Introduction It would be fair to ...

  4. HTML save data to CSV or excel

    /********************************************************************************* * HTML save data ...

  5. Anomaly Detection for Time Series Data with Deep Learning——本质分类正常和异常的行为,对于检测异常行为,采用预测正常行为方式来做

    A sample network anomaly detection project Suppose we wanted to detect network anomalies with the un ...

  6. [C4] Andrew Ng - Improving Deep Neural Networks: Hyperparameter tuning, Regularization and Optimization

    About this Course This course will teach you the "magic" of getting deep learning to work ...

  7. Using python to process Big Data

    Pandas is a great lib to process BIg Data. 1) pandas.pivot_table(data,values=None,columns=None,aggfu ...

  8. [Hive - Tutorial] Data Units 数据存储单位

    Data Units In the order of granularity - Hive data is organized into: 数据库.表.分区.桶 Databases: Namespac ...

  9. Python pandas 0.19.1 Intro to Data Structures 数据结构介绍 文档翻译

    官方文档链接http://pandas.pydata.org/pandas-docs/stable/dsintro.html 数据结构介绍 我们将以一个快速的.非全面的pandas的基础数据结构概述来 ...

随机推荐

  1. spring常用注解使用解析

    spring没有采用约定优于配置的策略,spring要求显示指定搜索哪些路径下的Java文件.spring将会把合适的java类全部注册成spring Bean.   问题:spring怎么知道把哪些 ...

  2. github添加ssh key报错Key is invalid. Ensure you've copied the file correctly

    github添加ssh key的时候报错:Key is invalid. Ensure you've copied the file correctly 将秘钥复制粘贴到文本编辑器中,再粘贴复制到

  3. go语言 类型:基础类型和复合类型

    Go 语言中包括以下内置基础类型:布尔型:bool整型:int int64 int32 int16 int8 uint8(byte) uint16 uint32 uint64 uint浮点型:floa ...

  4. SubSonic2.2框架的使用方法和配置说明

    网上.net ORM框架也不少,但是我感觉这个框架配置很简单的,前几年貌似用的人很多,现在好像用得比较少了,随着它官方的升级现在已经到3.0了, 并且采用T4 模板生成的方式,代码量好像减少了.不过我 ...

  5. WPF中的Invoke

    今天帮同事看一个问题,她用为了实现动画效果用主线程执行Thread.Sleep,然后界面就卡死了. 这个问题好解决,new 一个Thread就行了,但是更新WPF的界面需要主线程的操作,然后习惯性的打 ...

  6. SAP学习日志--RFC REMOTE FUNCTION CALL

    RFC  Remote function Call 远程功能调用, 是SAP系统之间以及非SAP系统之间程序通信的基本接口技术. 例如BAPI , ALE都是基于RFC实现的 SAP系统提供了三种外部 ...

  7. crm2011 使用SOAP 查询单个记录 Retrieve

    function getServiceUrl() {     var serverUrl = Xrm.Page.context.getServerUrl();     if (serverUrl.ma ...

  8. SharePoint 2013 Error - File names can't contain the following characters: & " ? < > # {} % ~ / \.

    错误截图: 错误信息: --------------------------- Message from webpage --------------------------- File names ...

  9. ZeroMq安装包的生成【ubuntu10】

    生成方法添加源sudo add-apt-repository ppa:chris-lea/zeromqsudo add-apt-repository ppa:chris-lea/libpgmsudo ...

  10. 百度地图SDK 遇到的问题及解决方案

    目前项目工作中用到了百度地图sdk,遇到了不少问题,在此记录一下,顺便吐槽下希望百度能把这地图sdk做的更好用一点. 1,开发环境, Xcode6.0 (6A313) + 百度地图 iOS SDK v ...