i have two datatable with drag and drop functionality. each row of datatable contain the IDs. when i drag the row of table 1 into table 2. the data stored into the Array. i have the addArray function for push the IDs into an Array there also filtering the duplicate IDs and make it uniqueArray. now i want to create the Select option Element which containing the iDs as a value. i want the select option to reCreate whenever i drag and drop. I would be grateful for any help.
JS
$(document).ready(function () {
new Sortable(drag, {
group: 'sortables',
onChange: function (e, tr) {
addArray();
},
});
new Sortable(drop, {
group: "sortables",
onChange: function (event, tr) {
addArray();
},
});
function addArray() {
let articles = [];
$('#drop').children().each(function () {
articles.push($(this).data('pk'))
});
let uniqueArray = articles.filter((item, index, array) => { // filter Duplicate ID
return array.indexOf(item) === index
})
$.each(uniqueArray, function (index, value) { // Create Option Element using Each ID form Array
$('#id_articles').append($('<option/>', { value: value, selected: '' }));
});
console.log(uniqueArray)
};
});
OUTPUT
<select name="articles" id="id_articles" multiple="" style="display: none;">
<option value="67" selected="selected"></option>
<option value="66" selected="selected"></option>
<option value="67" selected="selected"></option>
<option value="67" selected="selected"></option>
<option value="66" selected="selected"></option>
<option value="66" selected="selected"></option>
<option value="67" selected="selected"></option>
<option value="67" selected="selected"></option>
<option value="66" selected="selected"></option>
</select>
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…