0

I am using ng-repeat in my jsp to loop and display the values.if i have the value more then one time in the list i need to display only once.

Display the value only if the current value in the loop is not same as the previous value in jsp using ng- repeat

please find my code:

           <td> 
            // obj.vm is name. name should be displayed only if it is diferrent then the previous name. otherwise it should be blank 
            <div  obj.vm> </div>
            </td>

           // obj.dm is value. all the values should be displayed 
           <td>  <div obj.dm>  </div>
           </td>
           </tr>

<tr ng-repeat="obj in selItem.surveyRefData">
  <td nowrap> // need to display ob.vm only if the current value is not same as previous value 
    <div style="float: left;" class="form-actions" obj.vm"> 
    </div> 
  </td> 
  <td> 
    <div style="float: left;" class="form-actions"
         ng-bind-html="(!validSeriesIdReqPending ?(obj.dm | seriesText):'')"> 
    </div>
    <br/>
    <br/>

<tr ng-repeat="obj in selItem.surveyRefData|unique: obj"> 
  <td> // obj.vm is name. name should be displayed only if it is diferrent then the previous name. otherwise it should be blank 
    <div obj.vm> 
    </div> </td> // obj.dm is value. all the values should be displayed 
  <td> 
    <div obj.dm> </div> 
  </td> 
</tr>
georgeawg
  • 48,608
  • 13
  • 72
  • 95
user1015388
  • 1,283
  • 4
  • 25
  • 45
  • my code : // need to display ob.vm only if the current value is not same as previous value


    – user1015388 Oct 30 '18 at 18:43
  • 2
    Please [edit] the question and include to code in the body :) – Alon Eitan Oct 30 '18 at 18:44
  • https://stackoverflow.com/questions/49247681/remove-duplicate-from-ng-repeat This will solve your issue. – Gaurav Singh Oct 30 '18 at 18:46
  • i have modified the code like below: – user1015388 Oct 30 '18 at 19:00
  • But it is not working. it is displaying all the records. my obj has 2 columns like name and value. value should be displayed always. but name should be displayed only if the current name is different then the previous name – user1015388 Oct 30 '18 at 19:02
  • Please find my code: // obj.vm is name. name should be displayed only if it is diferrent then the previous name. otherwise it should be blank
    // obj.dm is value. all the values should be displayed
    – user1015388 Oct 30 '18 at 19:09
  • create method in controller which will take name as parameter and method will have a list in which it will add the name if it is not present in it already.and return true or false if name is already prseent in list or not. show the name tag in html in ng-if="method(name)" like this. – Nawnit Sen Oct 31 '18 at 06:42

1 Answers1

0

The ideal solution is to preprocess the data and group it by the vm attribute, so you would have some structure like this:

name1
  value1
  value2
  value3
name2
  value4
  value5
name3
  value5
...

Then you would have a nested ng-repeat that loops over the values within each name. If, for some reason, you can't preprocess the data, you can refer to the previous item in your ng-repeat using the $index variable. Here is a simple example:

<tr ng-repeat="obj in selItem.surveyRefData">
  <td>{{obj.vm !== selItem.surveyRefData[$index-1].vm ? obj.vm : ''}}</td>
  <td>{{obj.dm}}</td>
</tr>

Keep in mind that this will only work if your data is ordered based on the vm attribute... if you had name1 -> name1 -> name2 -> name1, for example, you would see name1 twice in your output table.

Here is a simple example plunker: https://plnkr.co/edit/r5yxYk4Rr7mXaBeqoJNs

james00794
  • 1,137
  • 7
  • 14