Angularjs 如何防止select2在单击下一行的select2时清除所选值
我的web应用程序使用angularjs和php。在该网站中,有一个部分可以添加用户的估算。它已选择2作为下拉列表,用于填充库存中的现有项目Angularjs 如何防止select2在单击下一行的select2时清除所选值,angularjs,angularjs-directive,angularjs-ng-repeat,jquery-select2,jquery-select2-4,Angularjs,Angularjs Directive,Angularjs Ng Repeat,Jquery Select2,Jquery Select2 4,我的web应用程序使用angularjs和php。在该网站中,有一个部分可以添加用户的估算。它已选择2作为下拉列表,用于填充库存中的现有项目 <tr class="valign-top" ng-repeat="estimate in workorder.steps[1].estimates track by $index" ng-init="item_key = $index"> <div id="material_div{{$index}}"
<tr class="valign-top"
ng-repeat="estimate in workorder.steps[1].estimates track by $index"
ng-init="item_key = $index">
<div id="material_div{{$index}}"
ng-if="estimate.type == 'material'"
class="create_Wo_forms inspec_crt_forms margin-0">
<select
style="width: 100%!important;"
id="parts_selected{{$index}}"
stepIndex="{{estimatesselectedStep}}"
itemIndex="{{$index}}"
class="crt-wo-input inspec-input"
item_wo_create_select>
</select>
</div>
</tr>
整个部分位于一个ng repeat in HTML文件中
directive('itemWoCreateSelect', function () {
return {
restrict: 'A',
link: function (scope, elem, attrs) {
var stepIndex = attrs.stepindex;
var elemIndex = attrs.itemindex;
var item = scope.workorder.steps[stepIndex]['estimates'][elemIndex];
$(elem).append('<option>' + item['name'] + '</option>');
scope.selected_items = [];
angular.forEach(scope.workorder.steps[stepIndex]['estimates'], function (v) {
if (v['item_id'] && v['item_id'] != null && !(v['item_id'] == item['item_id'] && v['unit_cost'] == item['unit_cost'])) {
scope.selected_items.push({ 'item_id': v['item_id'], 'unit_price': v['unit_cost'] });
}
});
var formatRepo = function (repo) {
if (repo.loading)
return repo.text;
var markup = '<span class="label">';
if (repo.hasOwnProperty('item_name') && repo.item_name.trim()) {
markup += repo.item_name + ' (' + repo.unit_price + ')' + '';
} else {
markup += repo.text + '';
}
markup += '</span>';
return markup;
};
var formatRepoSelection = function (repo) {
if (repo.hasOwnProperty('item_name')) {
return repo.item_name + ' (' + repo.unit_price + ')';
} else {
item['name'] = repo.id;
return repo.id;
}
};
$(elem).select2({
placeholder: "Search for item",
tags: true,
ajax: {
type: 'POST',
url: version5Url + 'inventory/getMasterStocksForInvoice',
tags: true,
data: function (params) {
return {
token: scope.token,
user_id: scope.user_id,
client_id: scope.client_id,
search: params.term,
selected_items: scope.selected_items
};
},
dataType: 'json',
delay: 250,
processResults: function (data, params) {
$(elem).find('option').remove();
if (item['item_id']) {
delete item['item_id'];
}
item['unit_cost'] = 0;
item['applied_taxes'] = [];
item['selected_tax'] = [];
item['amount'] = 0;
item['quantity'] = 0;
item['description'] = '';
scope.details = data.parts;
scope.$apply();
return {
results: data.parts,
pagination: false
};
},
cache: false
},
escapeMarkup: function (markup) {
return markup;
},
minimumInputLength: 0,
templateResult: formatRepo,
templateSelection: formatRepoSelection
});
$(elem).on("select2:select", function () {
var index = parseInt($(elem).val());
console.log("Slect", index)
if (!isNaN(index)) {
angular.forEach(scope.details, function (value, k) {
if (value['id'] == index) {
item['quantity'] = 1;
item['lot_number'] = value.lot_number;
item['item_id'] = value.item_id;
item['name'] = value.item_name + ' (' + value.unit_price + ')';
item['description'] = value.item_description;
item['unit_cost'] = value['unit_price'];
item['applied_taxes'] = [];
item['selected_tax'] = [];
if (value['taxes'] != '' && value['taxes'] != null) {
var tax_array = value['taxes'].split(',').map(function (x) {
return parseInt(x);
});
angular.forEach(scope.newTaxes, function (value, key) {
angular.forEach(tax_array, function (v1, k1) {
if (value['tax_id'] == v1) {
tax_array.splice(k1, 1);
}
});
});
item['applied_taxes'] = tax_array;
angular.forEach(tax_array, function (value) {
var result = scope.taxes.filter(function (obj) {
return obj.tax_id == value;
});
if (result.length > 0) {
item['selected_tax'].push(result[0].tax_name + ' (' + result[0].rate + '%)');
}
});
}
}
});
} else {
if (item['item_id']) {
delete item['item_id'];
}
item['unit_cost'] = 0;
item['applied_taxes'] = [];
item['selected_tax'] = [];
item['amount'] = 0;
item['quantity'] = 0;
item['description'] = '';
}
scope.$apply();
});
}
};
})
指令('itemWoCreateSelect',函数(){
返回{
限制:“A”,
链接:功能(范围、要素、属性){
var stepIndex=属性stepIndex;
var elemIndex=attrs.itemindex;
var item=scope.workorder.steps[stepIndex]['estimates'][elemIndex];
$(elem).append(“”+item['name']+“”);
scope.selected_items=[];
角度.forEach(范围.workorder.steps[stepIndex]['estimates'],函数(v){
如果(v['item\u id']&&v['item\u id']!=null&&!(v['item\u id']==item['item\u id']&&v['unit\u cost']==item['unit\u cost'])){
作用域。选定的项目。推送({“项目id”:v['项目id'],'unit\u price':v['unit\u cost']});
}
});
var formatRepo=函数(repo){
if(回购加载)
返回repo.text;
var标记=“”;
if(repo.hasOwnProperty('item_name')&&repo.item_name.trim()){
加价+=回购项目名称+”(“+回购单价+”)“+”;
}否则{
标记+=repo.text+“”;
}
标记+='';
返回标记;
};
var formatRepoSelection=函数(repo){
if(回购hasOwnProperty(“项目名称”)){
返回回购项目名称+'(“+回购单价+”);
}否则{
项目['name']=repo.id;
返回回购id;
}
};
$(元素)。选择2({
占位符:“搜索项目”,
标签:是的,
阿贾克斯:{
键入:“POST”,
url:version5Url+'inventory/getMasterStocksForInvoice',
标签:是的,
数据:函数(参数){
返回{
令牌:scope.token,
user\u id:scope.user\u id,
client\u id:scope.client\u id,
搜索:params.term,
选定的\u项:范围。选定的\u项
};
},
数据类型:“json”,
延误:250,
processResults:函数(数据、参数){
$(elem).find('option').remove();
如果(项目['item_id']){
删除项目['item_id'];
}
项目['单位成本]]=0;
项目[‘适用税费’]=[];
项目['selected_tax']=[];
项目['amount']=0;
物料['quantity']=0;
项目['description']='';
scope.details=data.parts;
作用域:$apply();
返回{
结果:数据部分,
分页:错误
};
},
缓存:false
},
转义标记:函数(标记){
返回标记;
},
最小输入长度:0,
模板结果:formatRepo,
templateSelection:formatRepoSelection
});
$(elem).on(“select2:select”,函数(){
var index=parseInt($(elem.val());
console.log(“Slect”,索引)
如果(!isNaN(索引)){
角度.forEach(范围.细节,函数(值,k){
if(值['id']==索引){
项目['数量]]=1;
项目['lot_number']=value.lot_number;
item['item_id']=value.item_id;
项目['name']=value.item_name+'('+value.unit_price+');
项目['description']=value.item_description;
项目[‘单位成本’]=价值[‘单位价格’];
项目[‘适用税费’]=[];
项目['selected_tax']=[];
if(值['taxes']!=''&&value['taxes']!=null){
var tax_数组=值['tax'].split(',').map(函数(x){
返回parseInt(x);
});
角度.forEach(scope.newt轴、函数(值、键){
角度.forEach(tax_数组,函数(v1,k1){
如果(值['tax_id']==v1){
tax_阵列拼接(k1,1);
}
});
});
项目['applied_taxes']=tax_数组;
角度.forEach(tax_数组,函数(值){
var结果=范围.税.过滤器(函数(obj){
返回obj.tax_id==值;
});
如果(result.length>0){
项目['selected_tax'].push(结果[0]。tax_name+'('+结果[0]。税率+'%));
}
});
}
}
$timeout(function() {
$(elem).select2({
placeholder: "Search for item",
tags: true,
ajax: {
type: 'POST',
url: version5Url + 'inventory/getMasterStocksForInvoice',
tags: true,
data: function (params) {
return {
token: scope.token,
user_id: scope.user_id,
client_id: scope.client_id,
search: params.term,
selected_items: scope.selected_items
};
},
dataType: 'json',
delay: 250,
processResults: function (data, params) {
$(elem).find('option').remove();
if (item['item_id']) {
delete item['item_id'];
}
item['unit_cost'] = 0;
item['applied_taxes'] = [];
item['selected_tax'] = [];
item['amount'] = 0;
item['quantity'] = 0;
item['description'] = '';
scope.details = data.parts;
scope.$apply();
return {
results: data.parts,
pagination: false
};
},
cache: false
},
escapeMarkup: function (markup) {
return markup;
},
minimumInputLength: 0,
templateResult: formatRepo,
templateSelection: formatRepoSelection
});
});