我有一个html视图,其中填充了来自ajax调用的json数据。我试图运行函数pageCtrl()
,但是每次运行它时,我总是得到一个空的NodeList。
这可能是由pageCtrl()
函数在我的Ajax完成之前运行引起的吗?
<script>
var Utils = {
escapeHtml: function _escapeHtml(s) { // Prevent code injection and rendering issues
return String(s).replace(/&/g, '&').replace(/>/g, '>').replace(/</g, '<');
}
};
function Panel(opts) {
var theDiv = document.getElementsByClassName('res-' + opts.classcontrol);
var theArray = [
'<div class="panel">',
' <div class="panel-heading">',
' <h4>',
' <span class="status"><b class="fa fa-', opts.icon, '"></b></span>',
' <span class="name">', Utils.escapeHtml(opts.title), '</span>',
' </h4>',
' </div>',
' <div class="panel-body">',
(opts.trustedBody || Utils.escapeHtml(opts.body)),
' </div>',
'</div></div>'
];
if (theDiv.length == 0) {
theArray.splice(0,
0,
'<div class="res-',
opts.classcontrol,
'"><h1>Machine: ',
opts.classcontrol,
'</h1>');
theArray.push("</div>");
}
return theArray.join('');
}
function ConfigurationController(element) {
if (!element) {
throw new Error('Cannot create ConfigurationController: Element Required');
}
var self = this,
apiUrl = "http://api.mydomain.com/api/ShopViewDisplay/" + "@ViewBag.pathProc";
// Expose Public method(s)
this.load = load;
//this.render = render;
function load() {
// Attempt ajax call; on fail: use dummy data
reset();
$.ajax({
url: apiUrl,
type: 'GET',
dataType: 'json',
success: function(data) {
data.map(renderItem,
function(el) {
if (el != undefined) {
element.append(el);
}
});
},
error: function(request, error) {
alert("Request: " + JSON.stringify(request) + " Error: " + error);
}
});
}
function reset() {
element.children().remove();
return element;
}
function renderItem(obj) {
var callback = typeof this === 'function' ? this : false;
var statusIcon = "fire";
var itemPanel = Panel({
icon: statusIcon,
title: obj.WorkOrderNumber,
classcontrol: obj.PressNo,
trustedBody: [
' <div class="PressNo"><b></b> Machine: <br>', Utils.escapeHtml(obj.PressNo), '</br></div>',
' <br> ',
' <div class="Priority"><b></b> Priority: <br>', Utils.escapeHtml(obj.Priority),
'</br></div>',
' <br> ',
' <div class="PartNo"><b></b> Part: <br>', Utils.escapeHtml(obj.Part), '</br></div>',
' <br> ',
' <div class="Material"><b></b> Material: <br>', Utils.escapeHtml(obj.PartDesc),
'</br></div>',
' <br> ',
' <div class="Setup"><b></b> Setup(E/A): <br>',
Utils.escapeHtml(obj.EstActSetupHrs).replace('\r\n', ' / '), '</br></div>',
' <br> ',
' <div class="Run"><b></b> Run (E/A): <br>',
Utils.escapeHtml(obj.EstActRunHrs).replace('\r\n', ' / '), '</br></div>',
' <br> ',
' <div class="Qty"><b></b> Qty: <br>',
Utils.escapeHtml(obj.CalEndCompleteQty).replace('\r\n', ' / '), ' </br></div>'
].join('\n')
});
itemPanel = $(itemPanel);
var jj = ".res-";
jj += obj.PressNo;
var theDiv = $(jj);
if (theDiv.length == 0) {
return (typeof(callback) === 'function' ? callback(itemPanel) : itemPanel);
} else {
theDiv.append(itemPanel);
return undefined;
}
}
}
function pageCtrl()
{
var elements = document.querySelectorAll('[class^=res-]');
console.log(elements);
var active = 0;
setInterval(()=>{
if( ++active >= elements.length) active = 0;
//poor support for options
elements[active].scrollIntoView({
behavior:'smooth',
block:'start' //Where to align current item: 'start', 'end' or 'center'
})
},5000)
}
$(document).ready(function _onLoad() {
new ConfigurationController($('.configurationList')).load();
//setInterval(ctrl.load(), 60000);
pageCtrl();
});
</script>
<div class="configurationList">
<div class="res-1">
scroll to me then pause for 5 seconds next move to res-2
</div>
<div class="res-2">
scroll to me then pause for 5 seconds next move to res-8-5
</div>
<div class="res-8-5">
scroll to me then pause for 5 seconds next move to top and repeat
</div>
</div>
答案 0 :(得分:2)
从成功回调函数中调用pageCtrl()函数,以便在构建DOM之前,pageCtrl()中定义的逻辑可以与
进行交互$.ajax({
url: apiUrl,
type: "GET",
dataType: "json",
success: function(data) {
data.map(renderItem, function(el) {
if (el != undefined) {
element.append(el);
}
});
pageCtrl();
},
error: function(request, error) {
alert("Request: " + JSON.stringify(request) + " Error: " + error);
}
});