gpt4 book ai didi

javascript - Angular 和 Select2 组合无法获取数组的值

转载 作者:可可西里 更新时间:2023-11-01 13:21:24 25 4
gpt4 key购买 nike

我正在做一个元素,必须制作一个产品 list 。但是当我将 select2 与搜索选项一起使用时,该值不会改变,如果我添加一个新行,我会得到一个无 select2 字段。有人可以帮我吗?或者,如果您有其他选择来获得一些真正感激的结果。对不起,我的英语不好。

$(document).ready(function() {
$(".js-example-basic-single").select2();
});

var invoice = angular.module("invoice", []);
invoice.controller("InvoiceController", function($scope) {
$scope.invoice = {
items: [
{
name: "item",
description: "item description",
qty: 5,
price: 5.5
}
]
};
($scope.add = function() {
$scope.invoice.items.push({
name: "",
description: "",
qty: 1,
price: 0
});
}),
($scope.remove = function(index) {
$scope.invoice.items.splice(index, 1);
}),
($scope.total = function() {
var total = 0;
angular.forEach($scope.invoice.items, function(item) {
total += item.qty * item.price;
});
return total;
});
});

参见 CodePen:

https://codepen.io/devtech_code/pen/bYBwaY

最佳答案

使用 DOMNodeInserted 事件处理程序。

$('body').on('DOMNodeInserted', 'select', function () {
$(this).select2();
});

$(document).ready(function() {
$(".js-example-basic-single").select2();
});
$('body').on('DOMNodeInserted', 'select', function () {
$(this).select2();
});
var invoice = angular.module("invoice", []);
invoice.controller("InvoiceController", function($scope) {
$scope.invoice = {
items: [
{
name: "item",
description: "item description",
qty: 5,
price: 5.5
}
]
};
($scope.add = function() {
$scope.invoice.items.push({
name: "",
description: "",
qty: 1,
price: 0
});
}),
($scope.remove = function(index) {
$scope.invoice.items.splice(index, 1);
}),
($scope.total = function() {
var total = 0;
angular.forEach($scope.invoice.items, function(item) {
total += item.qty * item.price;
});
return total;
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://rawgit.com/select2/select2/master/dist/js/select2.js"></script>
<link href="https://rawgit.com/select2/select2/master/dist/css/select2.min.css" rel="stylesheet"/>
<script src="http://select2.github.io/select2/select2-3.5.1/select2.js"></script>
<link href="http://select2.github.io/select2/select2-3.5.1/select2.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div class="container" ng-app="invoice">

<section class="row" ng-controller="InvoiceController">
<table class="table table-hover">
<thead>
<tr>
<th>Name</th>
<th>Description</th>
<th>Qty</th>
<th>Price</th>
<th>Total</th>
<th>Delete</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="item in invoice.items">
<td>
<select ng-model="item.name" class="js-example-basic-single">
<option selected disabled>Choose Product</option>
<option>Beef</option>
<option>Fish</option>
<option>Pork</option>
<option>Chicken</option>
<option>Duck</option>
</select>
</td>
<td><input type="text" ng-model="item.description" class="form-control" /></td>
<td><input type="number" ng-model="item.qty" class="form-control" /></td>
<td><input type="number" ng-model="item.price" class="form-control" /></td>
<td>{{item.qty * item.price}} €</td>
<td><button type="button" class="btn btn-danger" ng-click="remove($index)">Delete</button></td>
</tr>
<tr>
<td><button type="button" class="btn btn-primary" ng-click="add()">Add item</button></td>
<td></td>
<td></td>
<td>Total : </td>
<td>{{total()}} €</td>
</tr>
</tbody>
</table>

<pre>{{invoice.items}}</pre>
</section>
</div>

关于javascript - Angular 和 Select2 组合无法获取数组的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47174253/

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