文本未按正确的顺序显示
Text not displaying in right order
我正在尝试让 JSON 数据显示在段落中,但在不同日期获取的数据之间有一个分隔符。
function getAllLogs(emId) {
$.getJSON('datafile.json', function(data) {
$("#" + emId).html("All logs:<br>");
var firstDateDisplayed = false;
$.each(data, function(index, value) {
//Only displays dates once, then all log sheets for that date
if (firstDateDisplayed === false ||
data[index-1].log_sheet.time !== value.log_sheet.time) {
$("#" + emId).append("<br>----------------<br>");
//Formats and appends date to a paragraph
getLogDate(emId, index);
firstDateDisplayed = true;
}
//Formats and appends time to a paragraph
getLogTime(emId, index);
//Formats and appends all data to a paragraph
getLogData(emId, index);
});
});
}
我想要什么:
All logs:
----------------
Date 1
Time 1
Data 1
Time 2
Data 2
Time 3
Data 3
----------------
Date 2
Time 4
Data 4
Time 5
Data 5
我得到什么:
----------------
----------------
Date 1
Time 1
Data 1
Time 2
Data 2
Time 3
Data 3
Date 2
Time 4
Data 4
Time 5
Data 5
示例 JSON 数据:
[
{
"log_sheet": {
"data":"4",
"date":"2012-07-27T00:00:00-06:00",
"time":"2000-01-01T02:15:00Z",
"more_data":"7"
}
},
{
"log_sheet": {
"data":"8,
"date":"2012-07-27T00:00:00-06:00",
"time":"2000-01-01T07:30:00Z",
"more_data":"3"
}
}
]
我该如何解决这个问题?我的日期显示正确,但为什么分隔符不能?
获取日志时间函数:
//Gets, formats and displays the time for the current log sheet in the
//header of a collapsible menu or paragraph
function getLogTime(emId, logNum) {
//Accesses the JSON file
$.getJSON('datafile.json', function(data) {
//Takes only the part of the string that corresponds to time
var time = data[logNum].log_sheet.measured_at_time.substring(11,16);
//Formats time to 12 hour clock according to the hour
switch (time.substring(0, 2))
{
case "00":
//Changes 00 to 12 and adds am
time = time.replace(/00/, "12") + " am";
break;
case "01":
case "02":
case "03":
case "04":
case "05":
case "06":
case "07":
case "08":
case "09":
//Removes extra 0 and adds am
time = time.replace(/0/, "") + " am";
break;
case "10":
case "11":
//Adds am
time += " am";
break;
case "12":
//Adds pm
time += " pm";
break;
case "13":
//Replaces 13 with 1 and adds pm
time = time.replace(/13/, "1") + " pm"
case "14":
//Replaces 14 with 2 and adds pm
time = time.replace(/14/, "2") + " pm";
break;
case "15":
//Replaces 15 with 3 and adds pm
time = time.replace(/15/, "3") + " pm";
break;
case "16":
//Replaces 16 with 4 and adds pm
time = time.replace(/16/, "4") + " pm";
break;
case "17":
//Replaces 17 with 5 and adds pm
time = time.replace(/17/, "5") + " pm";
break;
case "18":
//Replaces 18 with 6 and adds pm
time = time.replace(/18/, "6") + " pm";
break;
case "19":
//Replaces 19 with 7 and adds pm
time = time.replace(/19/, "7") + " pm";
break;
case "20":
//Replaces 20 with 8 and adds pm
time = time.replace(/20/, "8") + " pm";
break;
case "21":
//Replaces 21 with 9 and adds pm
time = time.replace(/21/, "9") + " pm";
break;
case "22":
//Replaces 22 with 10 and adds pm
time = time.replace(/22/, "10") + " pm";
break;
case "23":
//Replaces 23 with 11 and adds pm
time = time.replace(/23/, "11") + " pm";
break;
}
//Uses only a portion of the string to accommodate possible future
//numbers
if (emId.substring(0,4) === "time") {
//Sets text in header to time
$("#" + emId + " .ui-btn-text").html(time);
} else {
//For displaying all of the logs
$("#" + emId).append(time + "<br>");
}
});
}
问题是,当你使用 $.getJSON()
获得更多数据时,你使用的是异步的 AJAX。这意味着脚本不会停止等待调用完成,而是继续执行。由于从服务器检索数据时存在延迟,但会立即打印分隔符,因此数据始终在分隔符之后打印。
一个简单的解决方法是为主循环中的数据保留容器,这样 AJAX 调用何时完成就无关紧要了。
function getAllLogs(emId) {
$.getJSON('datafile.json', function(data) {
var $emId = $( '#' + emId ); // cache the main container
$("#" + emId).html("All logs:<br>");
var firstDateDisplayed = false;
$.each(data, function(index, value) {
//Only displays dates once, then all log sheets for that date
if (firstDateDisplayed === false ||
data[index-1].log_sheet.time !== value.log_sheet.time) {
$emId.append("<br>----------------<br>");
// create a container for the date and have getLogDate() fill it later
$emId.append( '<div id="'+emId+'Date'+index+'"></div>' );
getLogDate(emId+'Date'+index, index);
firstDateDisplayed = true;
}
// do the same to the rest of the functions as we did above for getLogDate()
$emId.append( '<div id="'+emId+'Time'+index+'"></div>' );
$emId.append( '<div id="'+emId+'Data'+index+'"></div>' );
getLogTime(emId+'Time'+index, index);
getLogData(emId+'Data'+index, index);
});
});
}
所以基本上你想做的是在有一个新的日期外观时输入----?
var json = [
{
"log_sheet": {
"data":"4",
"date":"2012-07-27T00:00:00-06:00",
"time":"2000-01-01T02:15:00Z",
"more_data":"7"
}
},
{
"log_sheet": {
"data":"4",
"date":"2012-07-27T00:00:00-06:00",
"time":"2000-01-01T02:15:00Z",
"more_data":"7"
}
},
{
"log_sheet": {
"data":"4",
"date":"2012-07-27T00:00:00-06:00",
"time":"2000-01-01T02:15:00Z",
"more_data":"7"
}
},
{
"log_sheet": {
"data":"4",
"date":"2012-07-27T00:00:00-06:00",
"time":"2000-01-01T02:17:00Z",
"more_data":"7"
}
}
];
console.log("All logs:");
$.each(json, function(index, value) {
if(index == 0 || (index > 0 && json[index-1].log_sheet.time !== value.log_sheet.time))
{
console.log("-----------");
}
console.log("Date " + index + ": " + value.log_sheet.date);
console.log("Time " + index + ": " + value.log_sheet.time);
console.log("Data " + index + ": " + value.log_sheet.data);
});
结果:
All logs:
-----------
Date 0: 2012-07-27T00:00:00-06:00
Time 0: 2000-01-01T02:15:00Z
Data 0: 4
Date 1: 2012-07-27T00:00:00-06:00
Time 1: 2000-01-01T02:15:00Z
Data 1: 4
Date 2: 2012-07-27T00:00:00-06:00
Time 2: 2000-01-01T02:15:00Z
Data 2: 4
-----------
Date 3: 2012-07-27T00:00:00-06:00
Time 3: 2000-01-01T02:17:00Z
Data 3: 4
因此,重写的函数将如下所示:
function getAllLogs(emId) {
$.getJSON('datafile.json', function(data) {
var $output = $("#" + emId);
$output.html("All logs:<br>");
$.each(data, function(index, value) {
//Only displays dates once, then all log sheets for that date
if(index == 0 || (index > 0 && json[index-1].log_sheet.time !== value.log_sheet.time))
{
$output.append("<br>----------------<br>");
//Formats and appends date to a paragraph
//getLogDate(emId, index);
$output.append("Date " + index + ": " + value.log_sheet.time +"<br/>");
}
//Formats and appends time to a paragraph
//getLogTime(emId, index);
$output.append("Time " + index + ": " + value.log_sheet.time +"<br/>");
//Formats and appends all data to a paragraph
//getLogData(emId, index);
$output.append("Data " + index + ": " + value.log_sheet.data +"<br/>");
});
});
}
从我从您的帖子中收集到的信息来看,您实际上是按date
属性对data
和time
的日志进行分组。如果这是演员表,你可以尝试这样的东西:
function getAllLogs(elementId)
{
$.getJSON('datafile.json', function(data)
{
var logElement = $('#' + elementId).append('All logs:<br />');
var lastLogDateDisplayed = null;
$.each(data, function(index, value)
{
// check if we've reached another date
if (lastLogDateDisplayed != value.log_sheet.date)
{
// update the last shown date, and select its html
lastLogDateDisplayed = value.log_sheet.date;
logElement.append('----------------<br />');
.append(getLogDate(value));
}
// select the time and data as html strings
logElement.append(getLogTime(value))
.append(getLogData(value));
});
});
}
这里有一些值得注意的明确改进。首先,我们只选择一次elementId
元素。这将我们的选择器的性能从之前的 1 + 4n
命中率提高到 1
.此外,因此,getLogDate
、getLogTime
和getLogData
不接受value
(即当前日志项),并返回一个 dom 或 jQuery 对象以附加到日志元素本身。
这是一些更改,但它更干净,希望能帮助解决您的问题。
相关文章:
- 使用JS按顺序显示图像,而不是随机显示
- 我应该怎么做才能将这个数组从随机更改为在页面刷新时按顺序显示
- 批次图 在 X 轴上按顺序显示不同年份的数据
- 文本未按正确的顺序显示
- 要按顺序显示的图像
- 通过Angular Directive按顺序显示月份
- Javascript测试-使用Array.prototype以随机顺序显示选项
- 如何以反向/从右到左的顺序显示阿拉伯语网站的Nivo滑块分页
- 从下拉列表中的数组中选取某个项目,然后将其显示为第一项并按字母顺序显示
- 来自 flickr 的图像数组未按预期顺序显示
- 以随机顺序显示 ng 重复项目
- 按顺序显示动画 SVG 元素
- 时间滑块插件和传单 - 标记未按顺序显示
- 拉斐尔随机顺序显示物体
- 如何使用 Jquery 获取 XML 文件,并在每次加载时以随机顺序显示元素一次
- 按使用 jquery 检查复选框的顺序显示文本
- 按自定义顺序显示 DIV
- 按字母顺序显示的下拉列表数据项
- 根据特定值按顺序显示数组中的对象
- JavaScript:如何按顺序显示 GIF,避免预加载