Select option does not set the selected value from a response - node.js

i have 2 select dropdowns which i'm populating with values from a server. i can retrieve the values and populate the options of the select dropdown with no problems, however, when i try to set one of the values of the second select dropdown as the selected value, it does not display.
here's my code:
$.ajax({
type: "GET",
url: "/someUrl",
data: { nameType: 'Shop', item: itemName},
success: function (data)
{
$.each(data, function (i, obj) {
var div_data = "<option>" + obj.Shop + "</option>";
$(div_data).appendTo('#shop_name');
});
$.ajax
({
url: '/get_shop_item',
type: 'POST',
data: { shop_id: shop_id },
success: function (response)
{
$('#area_name option').filter(function() {
return this.text == response.result[0].area;
}).attr('selected', true);
$('#shop_name option').filter(function() {
return this.text == response.result[0].name;
}).attr('selected', true);
}
});
}
});

The code you posted only populates one select element #shop_name
I would probably set the name property on the options then you don't have to use filter and go through all options.
Try this
$.ajax({
type: "GET",
url: "/someUrl",
data: { nameType: 'Shop', item: itemName},
success: function (data) {
$.each(data, function (i, obj) {
var div_data = '<option name="' + obj.Shop + '">' + obj.Shop + '</option>';
$(div_data).appendTo('#shop_name');
// append to #area_name as well
});
$.ajax({
url: '/get_shop_item',
type: 'POST',
data: { shop_id: shop_id },
success: function (response) {
$('#area_name option[name="' + response.result[0].area + '"]').attr('selected', true);
$('#shop_name option[name="' + response.result[0].name + '"]').attr('selected', true);
}
});
}
});

Related

SHAREPOINT -Restrict duplicate Entry

Guys Required support for below scenario
i have one list for booking with date and time . i want to restrict the duplicate entry by date and time
i have tried the rest Api code but it's not working.. please support find below my code
i tried to check with one condition .. i need to check with two conditions
Please support
<script src="/Departments/WDCI/UIJP/PublishingImages/jquery-1.12.4.js" type="text/javascript"></script><script type="text/javascript">
function PreSaveAction() {
var check = false;
var listid = _spPageContextInfo.pageListId.replace('{', '').replace('}', '');
var DateId = $('input[id*="Date_Req"]');
$.ajax({
//replace the rest api to filter items based on your files
url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists(guid'" + listid + "')/items?$select=ID,Req_Date,Req_Time,Title&$orderby=ID desc&$filter=Date_Req eq + DateId + ",
type: 'GET',
async: false,
headers: {
"accept": "application/json;odata=verbose",
"content-type": "application/json;odata=verbose",
},
success: function (data, textStatus, jqXHR) {
var count = data.d.results.length;
if (count < 2) {
check = true;
} else {
alert('Dear Employee,This Calander Year you have already applied for two Job Positions.So you are eligible to apply next year only');
ddwrt:GenFireServerEvent('__commit;__redirect={/SiteAssets/Thank%20You.aspx}')
}
},
error: function (jqXHR, textStatus, errorThrown) {
alert(textStatus);
}
})
return check;
}
</script>
<script src="jquery-1.12.4.js" type="text/javascript">
</script><script type="text/javascript">
function PreSaveAction() {
var check = false;
var listid = _spPageContextInfo.pageListId.replace('{', '').replace('}', '');
var DateId = $('input[id*="Date_Req"]');
$.ajax({
//replace the rest api to filter items based on your files
url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists(guid'" + listid + "')/items?$select=ID,Req_Date,Req_Time,Title&$orderby=ID desc&$filter=Date_Req eq + DateId + ",
type: 'GET',
async: false,
headers: {
"accept": "application/json;odata=verbose",
"content-type": "application/json;odata=verbose",
},
success: function (data, textStatus, jqXHR) {
var count = data.d.results.length;
if (count < 1) {
check = true;
} else {
alert('Dear Employee,there is no slot available during this period');
ddwrt:GenFireServerEvent('__commit;__redirect={/SiteAssets/Thank%20You.aspx}')
}
},
error: function (jqXHR, textStatus, errorThrown) {
alert(textStatus);
}
})
return check;
}
</script>
<script type="text/javascript">
function PreSaveAction() {
var check = false;
//var listid = _spPageContextInfo.pageListId.replace('{', '').replace('}', '');
var DateId = $("input[Title*='Req_Date']").val();
var TimeID= $("select[Title*='Req_Time'] option:selected").text();
alert(DateId);
alert(TimeID);
$.ajax({
//replace the rest api to filter items based on your files
url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists//GetByTitle('Discussion2')/items?$select=ID,Req_Date,Req_Time,Title&$orderby=ID desc&$filter= Req_Date eq '" + DateId + "' and Req_Time eq '" + TimeID + "'", type: 'GET',
async: false,
headers: {
"accept": "application/json;odata=verbose",
"content-type": "application/json;odata=verbose",
},
success: function (data, textStatus, jqXHR) {
var count = data.d.results.length;
if (count < 1) {
check = true;
} else {
alert('Dear Employee,there is no slot available during this period');
}
},
error: function (jqXHR, textStatus, errorThrown) {
alert(textStatus);
}
})
return check;
}
</script>```
You can use the following code to restrict duplicate entry:
<script type="text/javascript">
function PreSaveAction() {
var check = false;
//var listid = _spPageContextInfo.pageListId.replace('{', '').replace('}', '');
var DateId = $("input[title='Date_Req']").val();
alert(DateId);
$.ajax({
//replace the rest api to filter items based on your files
url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists//GetByTitle('List0325')/items?$select=ID,Date_Req,Title&$orderby=ID desc&$filter=Date_Req eq '" + DateId + "'",
type: 'GET',
async: false,
headers: {
"accept": "application/json;odata=verbose",
"content-type": "application/json;odata=verbose",
},
success: function (data, textStatus, jqXHR) {
var count = data.d.results.length;
if (count < 1) {
check = true;
} else {
alert('Dear Employee,there is no slot available during this period');
}
},
error: function (jqXHR, textStatus, errorThrown) {
alert(textStatus);
}
})
return check;
}
</script>
Note: you need to change the followings:
In var DateId = $("input[title='Date_Req']").val(), change "Date_Req" to your date column name;
In $.ajax(), change List title and columns' names to yours in url part
The result is like:

Search keyword from Dbpedia using nodejs

I am new in node.js.I downloaded and executed a project from GitHub from this link. Here on this link there is live demo of this app. It is running rdf search using wikidata. This is the function that I found for the search of keyword from wikidata written in file public/scripts/controllers/main.js.
function search () {
//TODO: fix when null;
if (vm.searchInput != vm.lastSearch) {
var input = vm.searchInput;
vm.lastSearch = input;
vm.searchWait = true;
vm.noResults = false;
//$http.get('https://www.wikidata.org/w/api.php?action=wbsearchentities&format=json&language=en&limit=20&uselang=en&type=item&continue=0&search='+input).then(
$http({
method: 'GET',
url: 'https://www.wikidata.org/w/api.php',
params: {
action: 'wbsearchentities',
format: 'json',
language: 'en',
uselang: 'en',
type: 'item',
continue: '0',
limit: '20',
search: input,
origin: '*',
}
}).then(
function onSuccess (response) {
onSearch(response.data.search);
},
function onError (response) { onSearchErr(); console.log('Error: ' + response.data); }
);
//request.execQuery(query.search(input), onSearch, onSearchErr);
}
vm.searchActive = true;
}
I have changed the above function for DBpedia but it not search the keyword from DBpedia
function search2 () {
//TODO: fix when null;
if (vm.searchInput != vm.lastSearch) {
var input = vm.searchInput;
vm.lastSearch = input;
vm.searchWait = true;
vm.noResults = false;
//$http.get('https://www.wikidata.org/w/api.php?action=wbsearchentities&format=json&language=en&limit=20&uselang=en&type=item&continue=0&search='+input).then(
$http({
method: 'GET',
url: 'http://dbpedia.org/sparql',
params: {
action: 'wbsearchentities',
format: 'json',
language: 'en',
uselang: 'en',
type: 'item',
continue: '0',
limit: '20',
search: input,
origin: '*',
}
}).then(
function onSuccess (response) {
onSearch(response.data.search);
},
function onError (response) { onSearchErr(); console.log('Error: ' + response.data); }
);
//request.execQuery(query.search(input), onSearch, onSearchErr);
}
vm.searchActive = true;
}
how I can change this above function for search in DBpedia?Please help

Why is the body of request empty?

I'm using a pattern from another project for sending JSON-data to node. In the other project it works, in this project not.
The req-object is valide, but the body is empty. Why?
Client-side:
json = { "auth_user_pkref": 2 }
json.test_id = "ANCA"
json.questions_count = 3
json.right_answers = 2
json.wrong_answers = 1
json.seconds_spent = 180
console.log('/api/answer/add', json);
$.ajax({
url: "/api/answer/add",
type: "post",
data: JSON.stringify(json),
contentType: "application/json",
success: function (data) {
},
error: function (jqXHR, textStatus, errorThrown) {
console.log("ERROR, DB error");
}
});
Server-side:
router.post('/api/answer/add', function (req, res) {
console.log('/api/answer/add: ', req.body)
Server-log:
/api/answer/add: undefined
json = { "auth_user_pkref": 2 }
json.test_id = "ANCA"
json.questions_count = 3
json.right_answers = 2
json.wrong_answers = 1
json.seconds_spent = 180
console.log('/api/answer/add', json);
$.ajax({
url: "/api/answer/add",
type: "post",
data: json,
contentType: "application/json",
success: function (data) {
},
error: function (jqXHR, textStatus, errorThrown) {
console.log("ERROR, DB error");
}
});
Try this

JQuery-UI copy paste issue works on second attempt

We are using following JQuery code (within JSF) to invoke Autocomplete. Everything seems to work fine when user keys in data but when user copies and pastes Autocomplete will be invoked on second attempt. I am testing following code in IE 8. Any inputs are appreciated
<pre>
<code>
<script type="text/javascript">
$(function () {
$(document).on('keyup.autocomplete','##{resp.workItemResponse.wiResponseId}', function() {
$('##{resp.workItemResponse.wiResponseId}').autocomplete({
minLength: 3,
source: function( request, response){
$.ajax({
type: 'GET',
url: "/iaportal/autoCompleteServlet",
cache: false,
delay: 200,
data: {
'respId':'#{resp.workItemResponse.wiResponseId}',
'type': '#{autoCompleteType}',
'term':request.term
},
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(data){
response($.map(data.acctNum, function (item) {
return {
label: item.label,
value: item.label
}
}));
},
error: function(message){
alert("error "+message);
}
});
},
select: function( event, ui ) {
var selectedObj = ui.item;
var text = selectedObj.value;
$('.autoComplete#{resp.workItemResponse.wiResponseId}').attr('value', text);
$('.autoComplete#{resp.workItemResponse.wiResponseId}').trigger('change');
}
});
$('##{resp.workItemResponse.wiResponseId}').keyup(function(){
var text = this.value;
if(text == '' || text.length == 0){
$('.autoComplete#{resp.workItemResponse.wiResponseId}').attr('value', "");
$('.autoComplete#{resp.workItemResponse.wiResponseId}').trigger('change');
}
});
}); // on Ends
var textElem;
$(document).on('paste','##{resp.workItemResponse.wiResponseId}',function() {
textElem = this;
setTimeout(invokeOnPaste , 100);
});
function invokeOnPaste(){
var text = $(textElem).val();
if(text.length == 14){
$('.autoComplete#{resp.workItemResponse.wiResponseId}').attr('value', text);
$('.autoComplete#{resp.workItemResponse.wiResponseId}').trigger('change');
} else if(text.length != 0){
$('##{resp.workItemResponse.wiResponseId}').trigger('search','autocomplete');
}
}
});
</script>
</code>
</pre>

Dynamics CRM JavaScript: Can't update record because of CreatedOn?

I have the following to update a record:
var currentTimeBeingTracked = getCurrentTimeTracked();
if (currentTimeBeingTracked != null) {
currentTimeBeingTracked.dagency_EndDateTime = new Date();
var jsonTrackedTime = window.JSON.stringify(currentTimeBeingTracked);
$.ajax({ type: "POST",
contentType: "application/json; charset=utf-8",
datatype: "json",
url: ODataPath + "/dagency_trackedtimeSet",
data: jsonTrackedTime,
beforeSend: function (XMLHttpRequest) {
XMLHttpRequest.setRequestHeader("Accept", "application/json");
},
success: function (data, textStatus, XmlHttpRequest) {
var newTimeTracked = data.d;
RetrieveTrackedTimes();
alert("Stopped tracking your time successfully.");
window.top.document.getElementById("contentIFrame").contentWindow.location.reload();
},
error: function (XmlHttpRequest, textStatus, errorThrown) {
debugger;
alert("Couldn't stop tracking your time. " + XmlHttpRequest.responseText);
}
});
}
But I keep getting this message:
Error processing request stream. Error encountered in converting the
value from request payload for property 'CreatedOn' to type
'DateTime', which is the property's expected type. See inner exception
for more detail.
To create the record, I use the following code that works perfectly:
var trackedTime = new Object();
var timeEntryName = prompt("Please enter a name for the tracked time:", "New time entry");
if (!timeEntryName) {
return false;
}
var isBillable = confirm("Is this task billable? If Yes, press OK.");
trackedTime.dagency_name = timeEntryName;
trackedTime.dagency_StartDateTime = new Date();
trackedTime.dagency_Billable = isBillable;
var slot = new Object();
slot.Id = crmForm.ObjectId;
trackedTime.dagency_Slot = slot;
var jsonTrackedTime = window.JSON.stringify(trackedTime);
$.ajax({ type: "POST",
contentType: "application/json; charset=utf-8",
datatype: "json",
url: ODataPath + "/dagency_trackedtimeSet",
data: jsonTrackedTime,
beforeSend: function (XMLHttpRequest) {
XMLHttpRequest.setRequestHeader("Accept", "application/json");
},
success: function (data, textStatus, XmlHttpRequest) {
var newTimeTracked = data.d;
RetrieveTrackedTimes();
alert("Started tracking your time successfully.");
window.top.document.getElementById("contentIFrame").contentWindow.location.reload();
},
error: function (XmlHttpRequest, textStatus, errorThrown) {
alert("Couldn't start tracking your time. " + XmlHttpRequest.responseText);
}
});
What is the problem? Where can I find the inner exception the message refers to?
Changed to this and it worked correctly:
if (currentTimeBeingTracked != null) {
var trackedTimeUpdated = new Object();
trackedTimeUpdated.dagency_EndDateTime = new Date();
var jsonTrackedTime = window.JSON.stringify(trackedTimeUpdated);
$.ajax({ type: "POST",
contentType: "application/json; charset=utf-8",
datatype: "json",
url: ODataPath + "/dagency_trackedtimeSet(guid'" + currentTimeBeingTracked.dagency_trackedtimeId + "')",
data: jsonTrackedTime,
beforeSend: function (XMLHttpRequest) {
XMLHttpRequest.setRequestHeader("Accept", "application/json");
XMLHttpRequest.setRequestHeader("X-HTTP-Method", "MERGE");
},
success: function (data, textStatus, XmlHttpRequest) {
var newTimeTracked = data.d;
RetrieveTrackedTimes();
alert("Stopped tracking your time successfully.");
window.top.document.getElementById("contentIFrame").contentWindow.location.reload();
},
error: function (XmlHttpRequest, textStatus, errorThrown) {
debugger;
alert("Couldn't stop tracking your time. " + XmlHttpRequest.responseText);
}
});
}
What was wrong was that I need to set the X-HTTP-Method to MERGE and specify the guid in the URL.

Resources