2017-01-20 9 views
1

私はJsonを使用してHTMLページにSPARQLクエリの結果を表示しましたが、特定の値が入力され、警告ボックス。 bindingsが空の場合、今では何も表示されない、それだけでtrHeadersを示し用としてjson値が与えられていないときに警告ボックスを表示する方法

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が空の場合、または<td>が空の場合、どのように警告ボックスをポップアップ表示できますか? この質問が理解されたことを願っています。御時間ありがとうございます。

+0

はこれを試してください(result.bindings){} 他のアラート( '空')ここで、あなたは)(bindings.trimのためにチェックしている長さ== 0をクエリのSCRIに。 pt –

+0

人々がここで宿題をしているようです:最後の日に同じコードで質問しましたhttp://stackoverflow.com/questions/41617269/json-data-from-sparql-query-not-displaying-on-table and http://stackoverflow.com/questions/41602590/display-alert-box-when-table-element-has-no-text。重複としてマークする必要があります – AKSW

答えて

2

あなたはこのいくつかの異なる方法で行うことができます。

bindingsの場合は、オブジェクトの定数であるが、時には空でもよい:常にサーバーからの応答に設定されていない

if (data.results.bindings.length) { 
    //exists 
} else { 
    alert('goes here'); 
} 

bindings場合次の場合

if (data.results.hasOwnProperty('bindings')) { 
    //exists 
} else { 
    alert('goes here'); 
} 
関連する問題