Select2 Multiple: Add several items with one copy+paste

会有一股神秘感。 提交于 2019-12-08 07:44:34

问题


I want to add several items in a select2 input field at once.

If I copy+paste "Hawaii Alaska" into the select-multiple example here, then I get:

No results found

In my case spaces are not allowed in the items.

Is there a way to insert N space sperated items via copy+paste?

Desired result: (x)Hawaii (x)Alaska


回答1:


you can add token separators in your select2 to identify characters as stopping points for your tags/choices, but unfortunately it bugs on the last copy pasted item

//try copy pasting bug,invalid, enhancement, wontfix
//then try bug,invalid, enhancement
//you will see the problem
var data = [{ id: 0, text: 'enhancement' }, { id: 1, text: 'bug' }, { id: 2, text: 'duplicate' }, { id: 3, text: 'invalid' }, { id: 4, text: 'wontfix' }];

var placeholder = "select";
$(".mySelect").select2({
    tokenSeparators: [',', ', ', ' '],
    data: data,
    placeholder: placeholder,
    allowClear: false,
    minimumResultsForSearch: 5
});

here is the codepen http://codepen.io/anon/pen/dMWQbd

its opened as a bug in github on the library https://github.com/select2/select2/issues/3458



来源:https://stackoverflow.com/questions/36181295/select2-multiple-add-several-items-with-one-copypaste

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!