Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.0k views
in Technique[技术] by (71.8m points)

html - JavaScript Array is filtered but select option are not filtered duplicate IDs?

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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I can't see the purpose of updating articles array then filter it to be uniqueArray while you can update both arrays in first .each() by using a simple function to prevent duplicate values .. Try the next code it should work

function addArray() {
   let articles = uniqueArray = [];
   $('#drop').children().each(function () {
      articles.push($(this).data('pk'));
      uniqueArray = stop_duplicate_in_array(uniqueArray , $(this).data('pk'));
   });
   console.log(uniqueArray)
   let options = '';
   $.each(uniqueArray, function (index, value) { // Create Option Element using Each ID form Array
     options += `<option value="${value}" selected="">${value}</option>`;
   }); 
   $('#id_articles').html(options);   
}
function stop_duplicate_in_array(array , value){
   var index = array.indexOf(value);
   if(index > -1){array.splice(index, 1);}
   array.push(value);
   return array;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...