Send ng-repeat values that are selected (checkbox is true) into a javascript function as an array parameter

Issue

I want to add a Cancel button that will update the status from open to cancelled for all the selected checkboxes.

The cancel button should change the status to Cancelled.

Below is the code for the cancel button.

                    <div>
                        <button type="button" class="btn btn-danger btn-lg text-left" id="btnCancelSelectedOrderLines" name="btnCancelSelectedOrderLines" ng-click="dsoedit.OnCancelSelectedOrderLines()">Cancel Lines</button>
                    </div>

The checkboxes are displayed using ng-repeat.

                            <tr ng-repeat="o in dsoedit.OrdLines">
                                <td>
                                    <md-checkbox aria-label="Select Order Line" ng-model="o.IsSelected" ng-change="selectEntity()"></md-checkbox>
                                </td>
                                <td>
                                    <H5 class="btn-h-spacer">{{o.LineNbr}}</H5>
                                </td>
                                <td>
                                    <H5 class="btn-h-spacer">{{o.Status}}</H5>
                                </td>
…

My goal here is to cancel the orders where the checkboxes are selected. I’m trying to send the selected values to the ng-click function OnCancelSelectedOrderLines as an array parameter. Is there a way to send the o.LineNbr values as an array for the selected checkboxes to the OnCancelSelectedOrderLines as a parameter?
(something like this OnCancelSelectedOrderLines(o.LineNbr))

Solution

I was able to achieve my solution by passing in the entire ng-model
dsoedit.OrdLines as an parameter.

Updated by ng-click Cancel button to

ng-click="dsoedit.OnCancelSelectedOrderLines(dsoedit.OrdLines)"

And in my javascript function, I used filter to select the selected checkboxes.

var selectedOrderLines = OrdLines.filter(ordLine => ordLine.IsSelected === true);

Answered By – Anup Deuja

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published