gpt4 book ai didi

javascript - 使用 Angular js 在编辑操作期间设置多个 dropdownist 值

转载 作者:行者123 更新时间:2023-12-03 04:42:46 25 4
gpt4 key购买 nike

我正在使用带有 Angular js 的 ASP.NET MVC。

我有一个页面说 hello.cshtml。

其中有一个对话框,其中有按钮“添加”,单击该按钮后,我会在对话框本身具有两列的表中添加新行。

1) 用于从下拉列表中选择值

2) 用于输入值的文本框。

在这里,当我在表中添加新值并选择列表并输入运行完美的值时,只是在编辑操作期间在下拉列表中设置选定值时出现问题。

enter image description here

我正在创建该添加行的值的列表来存储信息,并且在更改下拉列表时我当场存储值,以便运行良好,但在编辑过程中我如何将下拉列表值设置回设置,因为 ng-model是在下拉列表中分配一次的一个!

我有以下代码来使用正确运行的 jquery 在更改 dropdowlist 时设置数组对象中的值:

    $scope.changeLocationRack = function (receiptlocationid, index) {
for (var i = 0; i < $scope.ReceiptDetail.ReceiptLocationList.length; i++) {
if ($scope.ReceiptDetail.ReceiptLocationList[i].ReceiptLocationId == receiptlocationid) {
$scope.ReceiptDetail.ReceiptLocationList[i].LocationRackId = $("#ddllocationrack_" + index).val();
$scope.ReceiptDetail.ReceiptLocationList[i].LocationName = $("#ddllocationrack_" + index + " :selected").text();
break;
}
}
}

现在要在编辑功能中设置那些 dropdowlist 值,我应该做什么?添加了下拉列表和新行的 HTML 代码:

<div class="form-group row gutter">
<button class="btn btn-primary pull-right" type="button" style="margin-right: 10px" ng-click="AddLocationRack()">Add Location Rack</button>
</div>
<div class="form-group gutter">
<fieldset>
<legend>
<b>Receipt Location Racks</b>
</legend>
<div class="form-group gutter">
<table id="tblReceiptLocationRack" class="table table-striped table-condensed table-bordered no-margin">
<thead>
<tr style="background-color:#357CBA; color: white;">
<th>Location Rack</th>
<th>Quantity</th>
<th></th>
</tr>
</thead>
<tbody>
<tr ng-repeat="r in ReceiptDetail.ReceiptLocationList track by $index">
<td>
<select class="form-control dropdownSelect2" ng-model="SelectedLocationRack" title="Choose Location Rack from List" ng-options="s as s.RackName for s in EnveeFee.LocationRackList track by s.LocationRackId" ng-change="changeLocationRack(r.ReceiptLocationId,r.ReceiptLocationId)" id="ddllocationrack_{{r.ReceiptLocationId}}" required>
<option value="">Select Location Rack</option>
</select>
</td>

<td>
<input type="number" class="form-control" style="text-align:right" id="qtyreceived{{$index}}" ng-model="r.QtyReceived" required />
</td>
<td class="text-center" style="vertical-align:middle"><span title="Remove" style="font-size:18px;color:red;cursor:pointer;" class="icon-cross2" ng-click="removelocationrack(r.ReceiptLocationId)"></span></td>
</tr>
</tbody>
</table>
</div>
</fieldset>
</div>

Angular 代码:

    $scope.ReceiptDetail = {
ReceiptLineId: 0,
ReceiptId: 0,
SizeId: 0,
ReceiptQty: '',
UnitRate: '',
Amount: '',
ReceiptLocationList: [],
cadrate: '',
hst: '',
Size: '',
LocationName: '',
SizeObject: {}
}

$scope.ReceiptLocation = {
ReceiptLocationId: 0,
LocationRackId: 0,
QtyReceived: '',
QtyIssued: '',
LocationName: ''
}

$scope.AddLocationRack = function () {
$scope.ReceiptDetail.ReceiptLocationList.push($scope.ReceiptLocation);
}

最佳答案

首先,您不应该将 $scope 作为对象推送到 ReceiptLocationList 中,因为这将导致唯一的对象处理,其中所有选定的对象属性都将被覆盖。只需将一个唯一的对象插入您的列表即可:

$scope.ReceiptDetail = {
ReceiptLineId: 0,
ReceiptId: 0,
SizeId: 0,
ReceiptQty: '',
UnitRate: '',
Amount: '',
ReceiptLocationList: [],
cadrate: '',
hst: '',
Size: '',
LocationName: '',
SizeObject: {}
};

$scope.AddLocationRack = function () {
$scope.ReceiptDetail.ReceiptLocationList.push({
ReceiptLocationId: 0,
LocationRackId: 0,
QtyReceived: '',
QtyIssued: '',
LocationName: ''
});
};

在 View 中,您需要为选择的 ng-model 属性设置正确的模型属性,例如:

<tr ng-repeat="r in ReceiptDetail.ReceiptLocationList track by $index">
<td>
<select class="form-control dropdownSelect2"
ng-model="r.ReceiptLocationId"
title="Choose Location Rack from List"
ng-options="s.LocationRackId as s.RackName for s in EnveeFee.LocationRackList track by s.LocationRackId"
id="ddllocationrack_{{r.ReceiptLocationId}}" required>
<option value="">Select Location Rack</option>
</select>
</td>
<td>
<input type="number"
class="form-control"
style="text-align:right"
id="qtyreceived{{$index}}"
ng-model="r.QtyReceived" required />
</td>
<td class="text-center"
style="vertical-align:middle">
<span title="Remove"
style="font-size:18px;color:red;cursor:pointer;"
class="icon-cross2"
ng-click="removelocationrack(r.ReceiptLocationId)"></span>
</td>
</tr>

最后,您可以在 Controller 中访问您选择的 ReceiptLocationId,如本演示输出所示:

angular.forEach($scope.ReceiptDetail.ReceiptLocationList, function (receiptLocationListItem) {
console.log(receiptLocationListItem.ReceiptLocationId);
});

关于javascript - 使用 Angular js 在编辑操作期间设置多个 dropdownist 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43014160/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com