GithubHelp home page GithubHelp logo

angular-toarrayfilter's Introduction

AngularJS toArray Filter

Although AngularJS 1.x supports iterating over an object (keys and values), it is not the preferred way of doing things. Moreover, filters like filter and orderBy just don't work with objects; they are designed to work with arrays.

This filter can convert your objects into stable arrays that can then be filtered and sorted using the standard AngularJS filters.

Get It

The easiest way to install is using bower:

bower install --save angular-toArrayFilter

You can also install via [npm][npm]

npm install --save angular-toarrayfilter

Alternatively you can download from the GitHub project: https://github.com/petebacondarwin/angular-toArrayFilter

Load It

Load the toArrayFilter.js file into your web app after loading angular.js

<html>
  ...
  <head>
    ...
    <script src="angular.js"></script>
    <script src="bower_components/angular-toArrayFilter/toArrayFilter.js"></script>
    ...
  </head>
  ...
</html>

Use It

Make sure that your AngularJS application references the angular-toArrayFilter module:

angular.module('myApp', ['angular-toArrayFilter']);

Now if you have an object that you wish to repeat over, just slip the toArray filter in before you try sorting or filtering:

<div ng-repeat="item in itemObj | toArray | orderBy : 'someProp'">
  {{ item.$key }} - {{ item.someProp }}
</div>

How it works

The filter iterates over the object's keys and creates an array containing the value of each property. By default, the filter also attaches a new property $key to the value containing the original key that was used in the object we are iterating over to reference the property.

Not adding the $key property

If you don't want the $key property to be attached to each of the property values, you simply put an additional parameter on the toArray filter:

<div ng-repeat="item in itemObj | toArray : false | orderBy : 'someProp'">
  {{ item.someProp }} (no $key available now)
</div>

Using $key with non-objects

Non-objects such as strings and numbers cannot have a new $key property attached to them. If the object properties you are iterating over are not objects then you must either disable the $key property or the filter will replace the non-object with a new object of the form: { $key: key, $value: value }.

Caveats

There are always issues when trying to iterate over properties in JavaScript and the toArray filter has its own set of things to be aware of when using it:

  • It only works with plain Objects - don't try to filter arrays and strings with it.
  • If you don't disable it, the filter will modify each property value with a new $key property.
  • This filter is not compatible with IE8. (It uses Object.keys and Object.defineProperty which don't work well or at all in Internet Explorer 8 (IE8).

Example

Here is a fuller example of using the toArray filter on an object, to allow sorting and filtering by a date property on each property of the original object. It also demonstrates how you can easily update the original object and the array will stay in sync.

Check out the Live Demo

index.html:

<div ng-app="app">
  <div ng:controller="Main">
    <div ng:repeat="item in items | toArray | orderBy: 'date'">
      {{item.$key}}: {{item.date | date}}
      <button ng-click="remove(item)">Remove</button>
    </div>
    <button ng-click="add()">Add</button>
  </div>
</div>

app.js

angular.module('app', [])

.controller('Main', function Main($scope) {
  $scope.nextKey = 4;
  $scope.items = {
    0: {date: new Date('12/23/2013')},
    1: {date: new Date('12/23/2011')},
    2: {date: new Date('12/23/2010')},
    3: {date: new Date('12/23/2015')}
  };

  $scope.remove = function(item) {
    delete $scope.items[item.$key];
  };

  $scope.add = function() {
    $scope.items[$scope.nextKey] = { date: new Date() };
    $scope.nextKey += 1;
  };
});

angular-toarrayfilter's People

Contributors

jonespen avatar petebacondarwin avatar zoltanradics avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

angular-toarrayfilter's Issues

Missing Licence

It would be really cool, if you could provide a licence (preferably MIT) for your repo.

Order by using nested property

Should this filter work on nested properties as well? I'm trying to do the following:

<div ng-repeat="rest in menu | toArray | orderBy : 'rest.info.order' " ng-init="id = rest.$key">

which is not working. Any suggestions? I would not want to modify the structure either.

Not working on some types of ngRepeat expression

Hello,

I guess there's a pretty simple reason to this filter not work with the ngRepeat below:

// Object
var collection = {
    'AA': 'Value 2',
    'BB': 'Value 1'
}
<!-- directive ngRepeat expression -->
<div ng-repeat="(key, value) in collection | toArray | orderBy:'value'">
    {{ key }} - {{ value }}
</div>

But I cannot see what I'm doing wrong. Is this an expected problem with this type of ngRepeat with this object structure or there's something that I need to change?

Thanks!

Bower does not install the latest version

bower install --save angular-toArrayFilter
does not install the latest version (the line
if (!obj) return obj;
is missing).

Am I doing something wrong?
Thank you

The filter is not working for a single depth object

Hi,
The filter code checks whether the input provided is object or not and returns from there itself. Now for ng-repeat over an object like:
{"panel":"red","window":"blue","carpet":"green","doors":"white"} with the toArray Filter it doesn't work as the repeat returns the value and not the object itself and when it reaches the Filter, it checks for object in the first step itself and returns as the value provided is not object. Can you add support for such an object too?

Should support non-object values of properties inside objects

Hi,

I need same filter to convert this type of object as well:

var someObject = {
"id1" : "title1",
"id2" : "title2"
}

should return back as:
//converted to Array
[
{
$key: "id1",
value: "title1"
},
{
$key: "id2",
value: "title2"
},
]

Note: If any property gets a value type non-object (e.g String) , filter should only then add a new property as "value", this would contain that non-object or string value.

If you need me to work volunteer, would love to do :)
Cheers !

Do you need help maintaining this repo?

There are a few open issues, 1 PR, but I don't see any code-related activity. The bower package isn't up to date and no .min version of the file is provided. Do you need some help maintaining this repo? If so, I can help you @petebacondarwin

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.