【发布时间】:2023-03-15 10:13:01
【问题描述】:
我已经使用 Json 在 HTML 页面中显示了 SPARQL 查询的结果,我的问题是当输入某个值并且查询不显示结果时,它应该显示一个警告框。我的代码如下:
HTML
<table id="results">
</table>
查询脚本
var query = [
"PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>",
"PREFIX yago: <http://dbpedia.org/class/yago/>",
"PREFIX type: <http://dbpedia.org/class/yago/>",
"PREFIX prop: <http://dbpedia.org/property/>",
"SELECT ?name ?runtime",
"WHERE {",
"?film rdf:type dbo:Film.",
"?film dbp:name ?name.",
"?film dbo:director dbr:Peter_Jackson.",
"} GROUP BY ?name ?runtime"
].join(" ");
alert("this query: [" + query + "]");
var queryUrl = url + "?query=" + encodeURIComponent(query) + "&format=json";
console.log(queryUrl);
$.ajax({
dataType: "jsonp",
url: queryUrl,
success: function (data) {
console.log(data);
// get the table element
var table = $("#results");
// get the sparql variables from the 'head' of the data.
var headerVars = data.head.vars;
// using the vars, make some table headers and add them to the table;
var trHeaders = getTableHeaders(headerVars);
table.append(trHeaders);
// grab the actual results from the data.
var bindings = data.results.bindings;
// for each result, make a table row and add it to the table.
for (rowIdx in bindings) {
table.append(getTableRow(headerVars, bindings[rowIdx]));
}
if (bindings.trim().length == 0) {
alert("empty"); //IF BINDING IS EMPTY DISPLAY ALERT BOX
}
}
});
目前,如果bindings为空,它不会显示任何内容,它只显示trHeaders。
如果bindings 为空或<td> 为空,如何弹出警告框?
希望这个问题能被理解。感谢您的宝贵时间。
【问题讨论】:
-
试试这个 if(result.bindings){} else alert('empty') 你在查询脚本中检查 bindings.trim().length == 0
-
似乎人们在这里做功课:同样的问题,前几天在这里问过相同的代码stackoverflow.com/questions/41617269/… 和stackoverflow.com/questions/41602590/…。应该标记为重复
标签: javascript jquery json sparql