public ArrayList getConsignmentsID(final DB database, String searchValue) {
System.out.println("inside getConsignmentsID function");
Consignment consignment = new Consignment();
DBConnection dbConnection = new DBConnection("mongodb://localhost", "transport");
dbConnection.open();
DBCollection conCollection = dbConnection.getDatabase().getCollection("consignment");
List dbCursor = null;
if (searchValue != null) {
BasicDBObject searchObj = new BasicDBObject("_id", new BasicDBObject("$regex", "^" + searchValue));
dbCursor = conCollection.find(searchObj, new BasicDBObject("_id", "true")).toArray();
dbConnection.close();
ArrayList consignmentList = new ArrayList();
for (DBObject x: dbCursor) {
System.out.println(x);
consignmentList.add(x.get("_id"));
}
return consignmentList;
} else {
return null;
} //System.out.println(new > BasicDBObject("consignmentList",consignmentList)); }
Through an Ajax call, I am receiving this response from the JSON string in JavaScript
{ "consignmentList" : [ "" , "AAA" , "ABC" , "BHU" , "MAN" , "WER" , "ZXC"]}
I wish to parse this array string in JavaScript and display its values in an unordered list. Here is the backend response from the database.
{ "_id" : ""} { "_id" : "AAA"} { "_id" : "ABC"} { "_id" : "BHU"} { "_id" : "MAN"} { "_id" : "WER"} { "_id" : "ZXC"}
function autocomplet() {
var min_length = 0; // minimum characters to display the autocomplete
var consignmentID = $('#consignmentID').val();
var consignmentList = $('#consignmentList');
if (consignmentID.length >= min_length) {
$.ajax({
url: '/jqueryreturn',
type: 'POST',
datatype: JSON,
data: {
consignmentID: consignmentID
},
success: function(data) {
consignmentList.show();
consignmentList.html(data);
}
});
} else {
$('#consignmentList').hide();
}
}
// set_item : this function will be executed when we select an item
function set_item(item) {
// change input value
$('#consignmentID').val(item);
// hide proposition list
$('#consignmentList').hide();
}
<div ><label style="margin:15px 0 0 0;" >Consignment:</label><input onkeyup="autocomplet()" id="consignmentID" type="text" class="inputlt" name="consignmentId" value="${(consign._id)!""}" id="c" style="font-size: 16px " onclick="clearInput(this)">
<ul id="consignmentList"></ul>
function autocomplet() {
var min_length = 0; // minimum characters to display the autocomplete
var consignmentID = $('#consignmentID').val();
var consignmentList = $('#consignmentList');
if (consignmentID.length >= min_length) {
$.ajax({
url: '/jqueryreturn',
type: 'POST',
datatype: JSON,
data: {
consignmentID: consignmentID
},
success: function(data) {
consignmentList.show();
consignmentList.html(data);
}
});
} else {
$('#consignmentList').hide();
}
}
// set_item : this function will be executed when we select an item
function set_item(data) {
// change input value
$('#consignmentID').val(data);
// hide proposition list
$('#consignmentList').hide();
}
<div ><label style="margin:15px 0 0 0;" >Consignment:</label><input onkeyup="autocomplet()" id="consignmentID" type="text" class="inputlt" name="consignmentId" value="${(consign._id)!""}" id="c" style="font-size: 16px " onclick="clearInput(this)">
<ul id="consignmentList"></ul>