当有5个结果时,仅显示YUI自动完成页脚

时间:2011-02-23 21:01:34

标签: javascript autocomplete yui yui-autocomplete

我希望自动填充显示只有在实际显示5个结果时才显示Displaying top 5 results的页脚。

到目前为止,我已将其设置为如果最初的结果少于5,则不会显示,但是一旦加载了5个结果,页脚始终会显示,无论实际显示的结果有多少。

请原谅愚蠢的flip噱头。

oAC.formatResult = function(oResultData, sQuery, sResultMatch) {
         var sKey = sResultMatch

         // Extract the part of the match that the user did not type
         var sKeyRemainder = sKey.substr(sQuery.length);


         oAC.setFooter("");
         var aMarkup = ["<div class='myCustomResult'>",
                             "<span style='font-weight:bold'>",
                             sQuery,
                             "</span>",
                             sKeyRemainder,
                             "<br>",
                             "</div>"];

         if (oResultData[2] >= 4) {
            flip = true;
         }
         if (flip) {
            oAC.setFooter("<div style=\"margin-left:5px;\"><span style=\"font-weight:bold;\">See more results for " + sQuery + "</span><br>Showing top 5 results</div>");
         }
         return (aMarkup.join(""));
      };

2 个答案:

答案 0 :(得分:1)

我没有尝试在formatResult函数中设置它,而是尝试使用doBeforeExpandContainer函数,它就像魅力一样。

oAC.formatResult = function(oResultData, sQuery, sResultMatch) {
   var sKey = sResultMatch

   // Extract the part of the match that the user did not type
   var sKeyRemainder = sKey.substr(sQuery.length);


   oAC.setFooter("");
   var aMarkup = ["<div class='myCustomResult'>",
                       "<span style='font-weight:bold'>",
                       sQuery,
                       "</span>",
                       sKeyRemainder,
                       "<br>",
                       "</div>"];

   oAC.setFooter("<div class=\"ac-footer\"><div class=\"ac-footer-content\"><a class=\"ac-footer-link\" href=\"#\" onclick=\"spandex(this,event,'filter-company');return false;\">See more results for <b>" + sQuery + "</b></a><br><span class=\"ac-footer-sub\">Showing top 5 results</span></div></div>");

   return (aMarkup.join(""));
};

oAC.doBeforeExpandContainer = function(sQuery, oResponse) {
   if (oAC._nDisplayedItems <= 5) {
      oAC.setFooter("");
   }

   return true;
}
每次容器打开时都会调用

doBeforeExpandContainer(正如您所期望的那样),而_nDisplayedItems将在列表中显示多少项(也如您所期望的那样)。

因此,在检索结果时,每次都会添加页脚。然后,在框显示之前,如果结果有五个或更少,则删除页脚。

答案 1 :(得分:0)

您希望仅在有5个结果时才显示页脚?

然后你不应该改变

if (oResultData[2] >= 4) {
    flip = true;
}

if (oResultData[2] == 4) {  // is this a zero based number? or should this be 5?
    flip = true;
}
else { flip = false } // assuming you don't already reset this in scope