简体   繁体   English

Select2 v4:从Excel工作表粘贴时在多选中选择选项

[英]Select2 v4: select option in multiselect on paste from Excel Sheet

I have scenario where our back-end team will select pincodes from Excel and paste it on pincode multiselect select2 dropdown.我有一个场景,我们的后端团队将从 Excel 中选择 pincodes 并将其粘贴到 pincode multiselect select2 下拉列表中。

I need to select option which are exactly matched with option dropdown present and remove not match inputs pasted.我需要选择与选项下拉列表完全匹配的选项,并删除粘贴的不匹配输入。

I have refer given examples but no luck.我参考了给出的例子,但没有运气。
1- Is it possible to paste a list to a select2 field and match each item in the list? 1- 是否可以将列表粘贴到 select2 字段并匹配列表中的每个项目?
2- Select2 Multiple: Add several items with one copy+paste 2- Select2 Multiple:通过一次复制+粘贴添加多个项目

Given are the excel copied inputs, all pincode should matched except 401203 and 40008鉴于是 excel 复制的输入,除 401203 和 40008 外,所有密码都应匹配

 380001
 401203
 396472
 421301
 382418
 400002
 400008

Given is the fiddle.给出的是小提琴。 jsFiddle for pasting excel inputs用于粘贴 excel 输入的 jsFiddle

after doing mix of RnD and tweaking I reached to a solution which perfectly does what we wanted.在混合了 RnD 和调整之后,我找到了一个完美的解决方案。

I have added id on parent div of select2 then bind paste event to input.我在 select2 的父 div 上添加了id ,然后将paste事件绑定到输入。

$('#pincode_div').on('paste', function (e) {

    var pastedData = e.originalEvent.clipboardData.getData('text').trim();
    tokens = pastedData.split(/\r\n|\r|\n/g); 

    /* print_console('pastedData', pastedData) */;
    /* print_console('tokens', tokens) */;

    $('#pincode > option').each(function(){
       current_pin.push(this.value);
    });

    found_pin = tokens.filter(value => current_pin.includes(value));

    /* print_console('found_pin', found_pin) */;
    /* print_console('current_pin', current_pin) */;

    //print_console('current input', $('#pincode_div').find('input').val());
    $('.select2-search__field').val('');
    $('#pincode_div').find('input').attr('class')
    $('#pincode').val('');
    $("#pincode").val(found_pin).trigger("change");


});

Complete code here Solved jsFiddle完整的代码在这里解决了jsFiddle

You guys can update my answer or share better solution.你们可以更新我的答案或分享更好的解决方案。

I had same need and I found exact solution for this question.我有同样的需求,我找到了这个问题的确切解决方案。 Use following HTML and script for multiple select and css given below:对下面给出的多个选择和 css 使用以下 HTML 和脚本:

<select class="mySelect for" multiple="multiple" style="width: 100%;" ng-model="">

 var placeholder = "Type or paste numbers"; $(".mySelect").select2({ tokenSeparators: [',', ', ', ' '], data: [], placeholder: placeholder, allowClear: false, maximumSelectionSize: 1, });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.13/js/select2.min.js"></script> <link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.13/css/select2.min.css" rel="stylesheet"/> <div class="col col-3 col-xs-12"> <label>Type or paste numbers</label> <select class="mySelect for" multiple style="width: 100%;"></select> </div>

CDN for select2 multiple 用于 select2 多个的 CDN

CDN style sheet for select2 multiple select2 多个的 CDN 样式表

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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