//DataSet Name
var dbCacheName = 'DataSet1';
//Field in dataset, used to replace current field value
var fieldNameInCache = 'Field2';
var self = this;
var styles = {
display: 'block',
position: 'absolute',
top: '0px',
bottom: '0px',
left: '0px',
right: '0px',
width: '100%',
height: '100%',
border: '0',
overflow: 'hidden',
'overflow-x': 'hidden',
'overflow-y': 'hidden',
'z-index': '10000'
}
var newValueToInsert = '';
// Sets iframe styles
setStyles(
styles,
function() {
//Dialog markup
var windowMarkup = "<div class='frame'><div class='searchField'><input id='searchValue' /><button id='searchButton'>Search</button></div><div id='recordspanel'><table id='resultspreview'></table><div id='norecord'>No records found</div></div><div class='buttonsfield'><button id='confirm'>Ok</button><button id='cancel'>Cancel</button></div></div>";
//Dialog styles
var css = "<style>body{width:100vw;height:100vh;background:rgba(0,0,0,0.1);margin:0;padding:0;display:flex;align-items:center;justify-content:center;}.frame{width:400px;max-height:80vh;background:white;min-height:200px;padding:50px;}#norecord{display:none;}#searchValue{width:calc(100% - 60px);}#cancel{margin-left:20px;}.buttonsfield{display:flex;justify-content:flex-end;height:25px;}.bolded{font-weight:bold;}button{width:60px;}#confirm{display:none;}#recordspanel{min-height:145px;padding: 10px 0;}#searchfield{height:30px;}</style>";
//Set styles
$('head').append(css);
//Creates dom in iframe
$("body").append(windowMarkup);
// Search button click
$('#searchButton').click(function () {
//Look in Dictionary
getDict(
{
cacheName: dbCacheName, //dataset name
filters: [{ FieldName: "", FieldValue: $('#searchValue').val() }] //fields to search
},
function (result) {
//Records rows from dataset
var records = result.detail.result;
//First row description
var firstRecord = records[0];
//hide 'table' and 'ok' button if no result is found
if (!firstRecord && !firstRecord.BoldMask){
$('#resultspreview').hide();
$('#norecord').show();
$('#confirm').hide();
} else {
$('#resultspreview').show();
$('#confirm').show();
$('#norecord').hide();
$('#resultspreview').html('');
firstRecord.forEach(function (recordData) {
if (fieldNameInCache === recordData.FieldName) {
newValueToInsert = recordData.FieldValue;
}
$('#resultspreview')
.append('<tr class="' +
(fieldNameInCache === recordData.FieldName ? 'bolded' : '') +
'"><td>' +
recordData.FieldName +
'</td><td>' +
recordData.FieldValue +
'</td></tr>');
});
}
});
});
//On 'ok' button click
$('#confirm').click(function () {
if (newValueToInsert) {
//Set value to current field
setCurrentFieldValue(newValueToInsert);
//Save and close iframe
save();
}
});
//On 'cancel' button click
$('#cancel').click(function () {
cancel();
});
});