I have grid-ui where cell is angular material md-autocomplete
.
I wrote demo: codepen where I run (for simulate only) ng-repeat
and try to select different value per row.
However whenever I do when I change one item, all other rows change as well. What am I doing wrong?
My HTML:
<tr layout="row" ng-repeat="item in ctrl.items" flex>
<td class="sc_color" flex>{{$index+1}}.color</td>
<td flex>
<md-autocomplete style="margin-bottom:10px;"
md-selected-item="item.selectedItem"
md-search-text="ctrl.searchText"
md-items="item in ctrl.querySearch(ctrl.searchText)"
md-item-text="item.display"
md-min-length="0"
placeholder="Pick a color">
<md-item-template>
<span md-highlight-text="ctrl.searchText" md-highlight-flags="^i">{{item.display}}</span>
</md-item-template>
</md-autocomplete>
</td>
</tr>
My items
:
self.items = [
{
selectedItem: 'aa'
},
{
selectedItem: 'bbb'
}
];
I use md-selected-item
but sounds like its the same for all items.
It's just because all the field are linked to the same variable : ctrl.searchtext
.
Just have to change this :
md-search-text="ctrl.searchText"
md-items="item in ctrl.querySearch(ctrl.searchText)"
By this :
md-search-text="ctrl['searchText' + $index]"
md-items="item in ctrl.querySearch(ctrl['searchText' + $index])"
Codepen : http://codepen.io/anon/pen/dMPLMb
Instead of using you can use scope variable like this
md-search-text="searchQuery"
md-items="item in ctrl.querySearch(searchQuery)"
In this case it use md-autocomplete scope and you don't need to worry about query variable anymore.
The @pbenard solution is ok for the list which does not have pagination or you don't insert new item at first index
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With