0
我試圖表現出頂級用戶的列表和它們的答覆的數量,但其答覆的正確數量的有兩個問題:發佈獨特的用戶的列表與jQuery代碼
- 它顯示了錯號碼回覆給用戶(它加倍由於某種原因),並
- 有時一個用戶多次顯示在頂部的用戶列表中達
我需要的所有獨特的用戶提供了列表的正確的回覆數量,如下所示:
- 李四8
- 傑克·史密斯7
- 安德烈·約翰遜5
- 弗雷德·傑克遜4
- 等等
我認爲需要編輯的部分是這樣的:
{
var htmlToAdd = '';
people.sort(compareTotals);
people.remove(0);
var peopleLimit = people.length;
if (peopleLimit > 10)
peopleLimit = 10;
for (var i = 0; i < peopleLimit; i++)
{
htmlToAdd = htmlToAdd + '<div class="peopleLink"><a href="' + people[i][2] + '">' + people[i][3] + '</a> has posted ' + people[i][0] + ' replies.</div>';
}
jQuery('#peopleList').html(htmlToAdd);
}
任何想法我需要c焊割?
萬一這會有所幫助,這裏是全碼:
if (jQuery("div.community_topic_box").size() > 0) // Only true for the front page
{
jQuery("#postersFrame").insertAfter(".community_stats");
}
else
{
jQuery("#postersFrame").hide();
}
});
var people = new Array();
var peopleFinished = 0;
var repliesHandled = 0;
var count = 0;
// Generic JSON functions
function JSONscriptRequest(fullUrl) {
this.fullUrl = fullUrl;
this.headLoc = document.getElementsByTagName("head").item(0);
this.scriptId = 'YJscriptId' + JSONscriptRequest.scriptCounter++;
}
JSONscriptRequest.scriptCounter = 1;
JSONscriptRequest.prototype.buildScriptTag = function() {
this.scriptObj = document.createElement("script");
this.scriptObj.setAttribute("type", "text/javascript");
this.scriptObj.setAttribute("src", this.fullUrl);
this.scriptObj.setAttribute("id", this.scriptId);
}
JSONscriptRequest.prototype.removeScriptTag = function() {
this.headLoc.removeChild(this.scriptObj);
}
JSONscriptRequest.prototype.addScriptTag = function() {
this.headLoc.appendChild(this.scriptObj);
}
function handlerTopics(results) {
var obj;
if (results.total > 0)
{
count = results.total/30;
var htmlToAdd = '';
if (results.total > 30)
{
var iterations = results.total/30;
for (var j=0; j < iterations; j++)
{
obj=new JSONscriptRequest('https://api.getsatisfaction.com/companies/' + companyName + '/topics.json?sort=recently_active&active_since=' + timeStamp + '&limit=30&page=' + j +'&callback=handlerEachTopics');
obj.buildScriptTag(); // Build the script tag
obj.addScriptTag(); // Execute (add) the script tag
}
}
else{
obj=new JSONscriptRequest('https://api.getsatisfaction.com/companies/' + companyName + '/topics.json?sort=recently_active&limit=30&active_since=' + timeStamp + '&callback=handlerEachTopics');
obj.buildScriptTag(); // Build the script tag
obj.addScriptTag(); // Execute (add) the script tag
}
}
jQuery('#peopleList').html('Loading member details...')
}
function handlerEachTopics(results) {
if (results.data.length > 0)
{
for(var i=0; i<results.data.length; i++)
{
if (results.data[i].active_replies > 0)
{
obj=new JSONscriptRequest(results.data[i].url + '/replies.json?callback=handlerEachTopicReplies');
obj.buildScriptTag(); // Build the script tag
obj.addScriptTag(); // Execute (add) the script tag
repliesHandled++;
}
}
}
peopleFinished++;
}
function handlerEachTopicReplies(results) {
if (results.data.length > 0)
{
for(var i=0; i<results.data.length; i++)
{
if ((!results.data[i].author.employee) &&(!isInArrayYet(results.data[i].author.canonical_name) > 0))
{
people[people.length] = [1, results.data[i].author.canonical_name, results.data[i].author.at_sfn, results.data[i].author.name];
}
}
}
repliesHandled--;
if ((peopleFinished >= count - 1) && (repliesHandled <= 0))
{
var htmlToAdd = '';
people.sort(compareTotals);
people.remove(0);
var peopleLimit = people.length;
if (peopleLimit > 10)
peopleLimit = 10;
for (var i = 0; i < peopleLimit; i++)
{
htmlToAdd = htmlToAdd + '<div class="peopleLink"><a href="' + people[i][2] + '">' + people[i][3] + '</a> has posted ' + people[i][0] + ' replies.</div>';
}
jQuery('#peopleList').html(htmlToAdd);
}
}
function isInArrayYet(name)
{
for(var i=0; i< people.length; i++)
{
if (people[i][1] == name)
{
people[i][0]++;
return true;
}
}
return false;
}
function compareTotals(a, b) {
return b[0] - a[0];
}
Array.prototype.remove = function(from, to) {
var rest = this.slice((to || from) + 1 || this.length);
this.length = from < 0 ? this.length + from : from;
return this.push.apply(this, rest);
};
</script>
我如何在用戶列表中的每個項目,他們的答覆只出現一次,我怎麼做回覆數停止乘以?感謝您的幫助。
「人」變量的內容是什麼?做一個'console.log(people)'。如果可能的話,展示它如何生成數組 – Alexander