简体   繁体   中英

Changing the value of select box changes the values of other select boxes which are dynamically generated in angularjs

I'm new to angularjs. I have a table where the data in the form of json array is loading from scope object as shown below.

<table class="table table-bordered">
    <thead>
        <tr style="background-color= #A4C831;background-color: #A4C831;color: #fff;border-color: 1px solid #fff !important;">
                <th>Date</th>
                <th>Order Number</th>
                <th>Refered By</th>
                <th>ID</th>
                <th>Name</th>
                <th>Phone</th>
                <th>Product Name</th>
                <th>Quantity</th>
                <th>Description</th>
                <th>Status</th>
                <th>Delete</th>
                </tr>
                </thead>
                <tbody>
                    <tr ng-repeat="orders in ordersData">
                              <td><span ng-bind="orders.createdDate"  class="td-wrap"></span></td>
                              <td><span ng-bind="orders.ordernumber"  class="td-wrap"></span></td>
                              <td><span ng-bind="orders.referedby"  class="td-wrap"></span></td> 
                              <td><span ng-bind="orders.id"  class="td-wrap"></span></td>
                              <td><span ng-bind="orders.name"  class="td-wrap"></span></td>
                              <td><span ng-bind="orders.phone"  class="td-wrap"></span></td>
                              <td><span ng-bind="orders.productname"  class="td-wrap"></span></td>
                              <td><span ng-bind="orders.quantity"  class="td-wrap"></span></td>
                              <td><span ng-bind="orders.description"  class="td-wrap"></span></td>
                              <td><!--<span ng-bind="orders.status"  class="td-wrap"></span>-->
                                <select class="form-control" name="status" ng-model="manageorders.status" ng-change="setStatus(orders)">
                                        <option value="" disabled="disabled" selected="selected">Please select a role type</option>
                                        <option value="Order Progress"><img src="img/navicons/rbusiness.png">Order Progress</option>
                                        <option value="Order Cancelled"><img src="img/navicons/rvisitor.png">Order Cancelled</option>
                                        <option value="Order Dispatched"><img src="img/navicons/rvisitor.png">Order Dispatched</option>
                                         <option value="Order on Hold"><img src="img/navicons/rvisitor.png">Order on Hold</option>
                                 </select>
                              </td>
                                <td>
                                    <input type='button' value="delete" class="btn btn-danger" ng-click="deleteOrders(orders)">
                                </td>
                            </tr>

                          </tbody>
                        </table>

and the json data as shown below.

 $scope.ordersData = 
[{id: 125, name: "manju", phone: "9215649870", productname: "vari vriddhi", quantity: "123", "description": "super product", "status": "dispatched"},
    {id: 124, name: "manju", phone: "9876543210", productname: "leafmate", quantity: "123", "description": "not good", "status": "dispatched"},
    {id: 121, name: "upi", phone: "9876543214", productname: "vari vriddhi", quantity: "13", "description": "awesome product", "status": "order cancelled"}]

Here i've given select box in the last column. where it is generated dynamically. but whenever i change the value of one select box, the values of other select boxes also getting changed. Please help me to get through this, Thanks in advance.. :)

Looks like you are using a variable called manageorders.status to keep track of the order status. The problem is that the variable that holds the status is the same for every dynamic-created row.

You should try adding a status to every order. Heres a working fiddle .

<tr ng-repeat="orders in ordersData">
    ...
    <td>
        <select class="form-control" name="status" ng-model="orders.status" ng-change="setStatus(orders)">
            <option value="" disabled="disabled" selected="selected">Please select a role type</option>
            <option value="Order Progress"><img src="img/navicons/rbusiness.png">Order Progress</option>
            <option value="Order Cancelled"><img src="img/navicons/rvisitor.png">Order Cancelled</option>
            <option value="Order Dispatched"><img src="img/navicons/rvisitor.png">Order Dispatched</option>
            <option value="Order on Hold"><img src="img/navicons/rvisitor.png">Order on Hold</option>
        </select>
    </td>
    ...
</tr>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM