在我的基於Worklight的應用程序中,我有一個存儲在mysql中的評估值(int)。我有作爲JSON數據的評價整數值。如何從mysql獲取評分星級值?
{"storeId":1000,"zipcode":"600014","rating":3,}
使用Jquery我需要在應用程序中以星號圖像的形式顯示該評級值。 如果值是2,那麼我需要顯示2星形圖像,等等。
我該如何做到這一點?
在我的基於Worklight的應用程序中,我有一個存儲在mysql中的評估值(int)。我有作爲JSON數據的評價整數值。如何從mysql獲取評分星級值?
{"storeId":1000,"zipcode":"600014","rating":3,}
使用Jquery我需要在應用程序中以星號圖像的形式顯示該評級值。 如果值是2,那麼我需要顯示2星形圖像,等等。
我該如何做到這一點?
到目前爲止,看看你已經實現了什麼(適配器實現,客戶端適配器調用......)會很有幫助。
沒有它,我只能提供一種可能的方式從數據庫獲取值並顯示它。
假設:
實現:
在共同\ index.html的有,例如,一個空UL
元素只是讓所取得的數據將是一個列表:
<ul id="usersAndStars"></ul>
在共同\ JS \ main.js,調用適配器:
// Of course, you can invoke it whenever you actually want to, and not like below...
function wlCommonInit(){
getStarsFromDatabase();
}
function getStarsFromDatabase() {
var invocationData = {
adapter: 'getStars',
procedure: 'getStars'
};
WL.Client.invokeProcedure(
invocationData,
{
onSuccess: displayStars,
onFailure: failedGettingStars
});
}
然後你處理調用,成功與失敗的響應:
function failedGettingStars() {
alert("failure");
}
function displayStars(response) {
var ul = $('#usersAndStars');
var i, j, li;
for (i = 0; i < response.invocationResult.resultSet.length; i += 1) {
// Create new <li> element and populate it
li = $("<li/>").text("Item " + [i+1] + " has " + response.invocationResult.resultSet[i].stars + " stars: ");
// Add images of a star
// Note that this is purely applicative - instead of adding several img tags,
// You could simply add an image showing 5 or 2 or 3 or however stars you want...
// Or in any other way you want.
for (j = 0; j < response.invocationResult.resultSet[i].stars; j += 1) {
li.append("<img src='images/\star.png' alt='star'/>");
}
// Append the <li> element to the <ul> element
ul.append(li);
};
}
在適配器\ getStars \ getStars-impl.js文件:
var selectStatement = WL.Server.createSQLStatement("select * from users");
function getStars() {
return WL.Server.invokeSQLStatement({
preparedStatement : selectStatement,
parameters : []
});
}
在適配器\ getStars \ getStars.xml文件:
...
...
...
<procedure name="getStars"/>
最終結果是:
我會使用CSS(星號爲'background-image')的單個類,以便只能將一個元素插入到DOM中,並使用CSS/JavaScript更改寬度以顯示所有星。這也節省了HTTP請求。 – SeinopSys
是的,我同意。但是,正如代碼片段中所述,恆星的顯示可以大大優化。但這不是問題所在;用戶真正想知道的是如何處理Worklight適配器調用。 –
基本上你想問怎麼讀這個JSON響應,對嗎? –