0

I am retrieving a search query from php mysql into my angularjs app. The results are in the following format:enter image description here

This object is then displayed in my angularjs app and displayed using ng-repeat. The thing is I would like to limit the results to 10 only. However for some reason it does not work. I did find a possible solution limiTo not working. Here it states LimitTo does not work if it is an object in an object. If this is the solution how would I convert srchResults to an object in an array in javascript?? If it is not what would be the issue?

I'm using Angular 1.3.11.

My code:

<md-list-item class="md-3-line" ng-repeat="result in srchResults|limitTo:10" >
    <div class="md-list-item-text" ng-click= "getEmployee(result.employee_id)">
        <h3>{{result.last_name}}, {{result.first_name}}</h3>
        <h4>{{result.name}}</h4>
        <p>{{result.fk_cs_type}}, {{result.is_active==1?"Active":"In-Active"}}</p>
        <md-divider ng-if="!$last"></md-divider>
    </div>
</md-list-item>
1

3 Answers 3

2

To answer your query about

how would I convert srchResults to an object in an array in javascript

to convert an "object of objects" to an "array of objects" you can do the following:

var obj; //your parent object
var res = [] //will stor result here

for(var i in obj) {
   res.push(obj[i]);
}

after loop ends, you will have res as your desired array

Sign up to request clarification or add additional context in comments.

Comments

1

Yet another variant, if you know count fields you can use Array.prototype.slice

var data = {0:{a:1},1:{a:1},2:{a:1},3:{a:1},4:{a:1}}

we know that here 5 fields, so we can do

data.length = 5;
var arr = Array.prototype.slice.call(data);

var data = {
   0: {
     a: 1
   },
   1: {
     a: 1
   },
   2: {
     a: 1
   },
   3: {
     a: 1
   },
   4: {
     a: 1
   }
 }


 data.length = 5;
 var arr = Array.prototype.slice.call(data);

 console.log(data, arr);

Or we can a bit automate this

var data = {
   0: {
     a: 1
   },
   1: {
     a: 1
   },
   2: {
     a: 1
   },
   3: {
     a: 1
   },
   4: {
     a: 1
   }
 }

 var keys = Object.keys(data);
 data.length = 1+ +keys[keys.length-1];
 var arr = Array.prototype.slice.call(data);

 console.log(data, arr);

Comments

0

Try this code

<md-list-item class="md-3-line" ng-repeat="(key,value) in srchResults|limitTo:11" >
    <div class="md-list-item-text" ng-click= "getEmployee(value.employee_id)">
        <h3>{{value.last_name}}, {{value.first_name}}</h3>
        <h4>{{value.name}}</h4>
        <p>{{value.fk_cs_type}}, {{value.is_active==1?"Active":"In-Active"}}</p>
        <md-divider ng-if="!$last"></md-divider>
    </div>
</md-list-item>  

3 Comments

in your fiddle srchResults is array, in OP - is object and limitTo work only with array
Since his screenshot showed an object, i made the code.That's why i made my answer that fits for object. But my jsfiddle will show both.
Thanks for the knowledge that limitTo will not work with object.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.