javascript - using ng-repeat to print out all items sorted by different categories into rows -


so, i've managed print out items list in messy way desperately in need of refactoring

say have array of objects

var items = [{name: "toaster", price: 10, category: "appliance"},{name: "spade", price: 5, category: "tool"},{name: "hoe", price: 5, category: "tool"},{name: "microwave", price: 10, category: "appliance"}] 

using ng-repeat, print out name , price of each item row according category without having have lot of messy code. code looks similar this:

<section class="container">       <div class="row">      <h3>tool</h3>         <ul class="list-group col-lg-2 col-sm-3 col-xs-4" ng-repeat="item in ctrl.items | filter: { category:'tool'}">           <li class="list-group-item">             <p>name: {{item.name}} </p>             <p>price: £{{item.price}} </p>           </li>         </ul>       </div>   </section>        <section class="container">       <div class="row">      <h3>appliances</h3>         <ul class="list-group col-lg-2 col-sm-3 col-xs-4" ng-repeat="item in ctrl.items | filter: { category:'appliance'}">           <li class="list-group-item">             <p>name: {{item.name}} </p>             <p>price: £{{item.price}} </p>           </li>         </ul>       </div>   </section> 

is there anyway make lot less messier? have presort objects different arrays in controller? or there better way use filterby method?

edit

ideally, resultant html should this

<section class="container">           <div class="row">          <h3>appliance</h3>           <ul>               <li class="list-group-item">                 <p>name: toaster </p>                 <p>price: £10</p>               </li>              <li class="list-group-item">                 <p>name: microwave</p>                 <p>price: £10</p>               </li>             </ul>             <h3>tool</h3>  <ul>               <li class="list-group-item">                 <p>name: hoe</p>                 <p>price: £5</p>               </li>              <li class="list-group-item">                 <p>name: spade</p>                 <p>price: £5</p>               </li>             </ul>            </div>       </section> 

it should that

if have understood question correctly, looking want sort data according 'category' key.

for sorting purposes suppose use 'orderby' filter provided angularjs .

<section class="container">       <div class="row">      <h3>sort category</h3>         <ul class="list-group col-lg-2 col-sm-3 col-xs-4" ng-repeat="item in ctrl.items | orderby: 'category'">           <li class="list-group-item">             <p>name: {{item.name}} </p>             <p>price: £{{item.price}} </p>           </li>         </ul>       </div>   </section> 

this should trick.

for more information can refer link


Comments

Popular posts from this blog

javascript - Google App Script ContentService downloadAsFile not working -

javascript - Function overwritting -

c# - Exception when attempting to modify Dictionary -