0

我正在尝试显示顶级用户列表及其回复数量,但有两个问题:

  1. 它向用户显示错误的回复数量(由于某种原因它加倍),并且
  2. 有时一个用户多次出现在顶级用户列表中

我需要一个包含正确回复数量的所有唯一用户的列表,如下所示:

  • 简·多伊 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);
    }

关于我需要改变什么的任何想法?

如果这有帮助,这里是完整的代码:

     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>

如何使用户列表中的每个项目及其回复仅出现一次,以及如何使回复数量停止成倍增加?谢谢您的帮助。

4

1 回答 1

0

如果您对帖子数量有疑问,您最好在

htmlToAdd + '<div class="peopleLink"><a href="' + people[i][2] + '">' + people[i][3] + '</a> has posted ' + people[i][0] + ' replies.</div>';

并确保它是正确的。您可能需要重新检查 people数组和该数组中的数据。

代码的其他部分对我来说很好。如果您可以发布完整的代码,我将能够帮助您。

于 2013-01-08T01:30:56.250 回答