1

I have to update the value of multiple Inputs using ng-repeat. i usually get the value with JQuery $(class/id).val() and get its value. but now i have no idea how to access the input values since i only have one id for each. (i have like 20 input in the table)

View:

<tr ng-repeat="i in list">

        <td><input list="itemNames" class="item_name" ng-model="i.item_name" value="{{i.item_name}}" type="text"/></td>

        <datalist id="itemNames">
           <option ng-repeat="ii in list" class="idI" ng-model="ii.idI"  data-item="{{ii.idI}}" value="{{ii.item_name}} {{ii.idI}}">
        </datalist>

        <td><input class="quantity" ng-model="i.quantity" value="{{i.quantity}}"  type="number"/></td>

        <td><input class="price" ng-model="i.price" value="{{i.price}}" type="number"/></td>
    <tr>
        <td ng-click="updateAll()">UPDATE</td>
    </tr>

</tr>

I expect to store all values in an arrays, but what i got is only values of the first row.

JS:

$scope.updateAll=function(){
    // getting vallues
    var item_name=$(".item_name").val();
    var quantity=$(".quantity").val();
    var price=$(".price").val();
}

1 Answer 1

2

I think your list is a scope variable. So, in controller its defined as $scope.list. You need not to think about id as you are using angular code, angular framework will do it for you.

As you used the two way data binding with ng-model, so any change to the bind variable will be immediately visible to controller and html view. The use of ng-model ensures the immediate update to the $scope.list variable.

For example, if you add any text for "item_name" in the control index 0 from html view, the variable $scope.list[0].item_name will be automatically updated, also the change in $scope.list[0].item_name = "Some name" in controller will be automatically reflected in the view.

So, your given code can be re-written as following:

<tr ng-repeat="i in list">
    <td><input list="itemNames_{{$index}}" class="item_name" id="txt_name_{{$index}}" ng-model="selectedValue" ng-change = "getSelectedId(selectedValue, $index)"  type="text"/></td>
    <datalist id="itemNames_{{$index}}">
       <option ng-repeat="ii in list" class="idI" ng-model="ii.idI"  data-item="{{ii.idI}}" value="{{ii.item_name}} {{ii.idI}}">
    </datalist>
    <td><input class="quantity" ng-model="i.quantity" value="{{i.quantity}}"  type="number"/></td>
    <td><input class="price" ng-model="i.price" value="{{i.price}}" type="number"/></td>
<tr>
    <td ng-click="updateAll()">UPDATE</td>
</tr>

The javascript method can be written as:

var getSelectedId = function(selectedValue, index) {
      var val = document.getElementById('txt_name_' + index).value;
      var text = $('#itemNames_' + index).find('option[value="' + val + '"]').attr('data-item');
      alert(text);
}
Sign up to request clarification or add additional context in comments.

4 Comments

This is awesome! but my problem is that i have a datalist, i am getting the item name correctly but how to get the id? i'll update the the question
You want get Id of data list?
yes i want to get the id of the selected item_name, i sotred it in the option as data-item=ii.idi
Updated code added, if you need any more support provide the plnkr.co link.

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.