0

How to split and display from string to array for Select2 jquery? Current string that I get from DB is like this Michael,Anna,Sofea, hence how to split into array as Select2 format will read array to display the information, like this ["Michael","Anna","Sofea"]

Console.log from API reading

enter image description here

Current console.log after split is like below:

enter image description here

But why the selected that come from DB does not appear in Select2 <select> selection?

enter image description here

<div class="col-lg-10 col-sm-8">
    <select type="text" class="form-control myClass" id="editOwner" multiple="multiple"></select>
</div>

// To read all users fullname from API
$.ajax ({
    ...
    success: function(response){
        for (var i = 0; i < response.data.length; i++) { 
            $("#editOwner").append($("<option>", {
                response: response.data[i].fullname,
                text: response.data[i].fullname
            }));
        }
    }
});

// To read current selected user based on what had been stored from DB
function editLayer3(id){
    $.ajax({
        ...
        success: function(response){            
                if (response.status == "Success"){                    
                    $("#editOwner").val(response.data[0]["task_owner"]).attr("disabled",false);

                    var test = response.data[0]["task_owner"];
                    var results = test.split(",");

                    $("#editOwner").select2();                    
                }
                else {}            
        },
        error: function(e){}
    });

    // Will redirect to Edit form that contain Submit button
}
3
  • I read from API which comes from DB (another guy), they are sending the information in String, then I need to convert it into Array since as I know Select2 will only appear at the <select> for selection in Array. Sorry if I am wrong. Commented Mar 28, 2020 at 11:57
  • ok, i asked you to show us value of console.log(response); for first ajax Commented Mar 28, 2020 at 11:58
  • ok update some images Commented Mar 28, 2020 at 12:05

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.