简体   繁体   English

将列表限制为最后5个项目

[英]Limit a List to the last 5 items

Looking for a solution to the following: I have a list of items eg 寻找以下解决方案:我有一个项目列表,例如

<ul>
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
<li>Item 4</li>
<li>Item 5</li>
<li>Item 6</li>
<li>Item 7</li>
<li>Item 8</li>
<li>Item 9</li>
<li>...</li>
</ul>

The list is added to (for each person) each time they look at products on the site ie "recently viewed items" and the last product looked at is added to the bottom of the list. 每当他们在站点上查看产品时(即“最近查看的商品”),该列表即被添加到(针对每个人),并且上一次查看的产品被添加到列表的底部。 The list is taken from a MYSQL database. 该列表取自MYSQL数据库。 The list doesn't reset for that person it simply gets longer each time the same person viewing the site views more products. 该列表不会为该人重置,只是每次访问该网站的同一人查看更多产品时,列表就会更长。

Is there anyway to only show the last 5 items in the list ignoring the ones before bearing in mind new list items are added to the bottom of the list and the list is continually growing? 无论如何,在记住将新列表项添加到列表的底部并且列表持续增长之前,仅显示列表中的最后5个项目而忽略它们。

I'd like the list to just show the last five products at any one time for each person who views the site ie the list on the site maybe 我希望该列表可以随时显示每个查看该网站的每个人的最新五个产品,例如,该网站上的列表

  • Item 1 项目1
  • Item 2 项目2
  • Item 3 项目3
  • Item 4 项目4
  • Item 5 项目5
  • Item 6 项目6
  • Item 7 项目7
  • Item 8 项目8
  • Item 9 项目9
  • Item 10 项目10
  • Item 11 项目11
  • Item 12 项目12
  • Item 13 项目13
  • Item 14 项目14
  • Item 15 项目15
  • Item 16 项目16

but I only want the person who has veiwed all 16 products to just see the last 5 recently viewed products ie 但我只希望浏览了全部16种产品的人只看到最近浏览过的5种产品,即

  • Item 11 项目11
  • Item 12 项目12
  • Item 13 项目13
  • Item 14 项目14
  • Item 15 项目15
  • Item 16 项目16

Another person may come to the site and view just 4 products eg 另一个人可能会来该站点并仅查看4种产品,例如

<ul>
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
<li>Item 4</li>
</ul>

and they would see these 4 products in their recently viewed list. 他们会在最近查看的列表中看到这4种产品。

A third person may come to the site and view 12 products and they would see 可能有第三者访问该网站并查看12种产品,他们会看到

<ul>
<li>Item 8</li>
<li>Item 9</li>
<li>Item 10</li>
<li>Item 11</li>
<li>Item 12</li>
</ul>

When the person above who viewed 16 items comes back to the site they will see the last 5 items from their previous session and when they start viewing products the fisrt item will disappear and a new item will show at the bottom of the list 当查看了16个项目的上方的人回到网站时,他们将看到前一会话的最后5个项目,并且当他们开始查看产品时,第一个项目将消失,并且新项目将显示在列表的底部

<ul>
<li>Item 11</li>
<li>Item 12</li>
<li>Item 13</li>
<li>Item 14</li>
<li>Item 15</li>
<li>Item 16</li>
</ul>

then as they start browsing they will see 然后,当他们开始浏览时,他们会看到

  • Item 12 项目12
  • Item 13 项目13
  • Item 14 项目14
  • Item 15 项目15
  • Item 16 项目16
  • Item 17 项目17
etc... 等等...

is this possible? 这可能吗?

I'm going to leave everything from my previous answer but I believe the scope of the question has changed and is now a database/sql question. 我将保留先前回答中的所有内容,但我相信问题的范围已更改,现在是数据库/ sql问题。

I think what you're looking for is a SQL query to limit the number of rows returned and order them for you. 我认为您正在寻找的是一个SQL查询,以限制返回的行数并为您排序。

You can check out the documentation for MySQL 5.0 SELECT which can give you an idea of how to do this, but I will also give you an example below. 您可以查看MySQL 5.0 SELECT的文档,该文档可以使您了解如何执行此操作,但是我还将在下面提供示例。

Assuming you have a table of recently viewed items that looks like the following: 假设您有一张最近查看过的项目的表格,如下所示:

CREATE TABLE user_product_viewings (
    ID INT(32) NOT NULL auto_increment,
    product_id INT(32)  NOT NULL,
    user_id INT(32)  NOT NULL,
    viewed_at DATETIME NOT NULL,
primary KEY (ID));

You can use the following query to select the 5 latest entries for a specific user: 您可以使用以下查询为特定用户选择5个最新条目:

SELECT product_id FROM user_product_viewings WHERE user_id = #{USER_ID} ORDER BY viewed_at DESC LIMIT 5;

Old Answer 旧答案

It looks like you are trying to do this with javascript, so maybe your appending data to the list as the user browses the page. 看来您正在尝试使用javascript进行此操作,因此可能是在用户浏览页面时将数据追加到列表中。

I would not simply try to hide the items, but remove them complete from the DOM. 我不会简单地尝试隐藏项目,而是将其从DOM中完全删除。 The best way to do this is to write a function that will check the current length of the list and then remove the oldest children (top) before appending a new one. 最好的方法是编写一个函数,该函数将检查列表的当前长度,然后删除最旧的子项(顶部),然后再添加新的子项。

An example of that might be something like the following: 例如,可能如下所示:

  var addToList = function (item_value) {
    while ( list.children.length >= 5 )
    {
      list.removeChild(list.firstChild );
    }

    var item = document.createElement("li");
    item.innerText = item_value;

    list.appendChild(item);
  }

http://jsbin.com/oriwut/3/edit http://jsbin.com/oriwut/3/edit

Here is another solution which combines the above with only showing the last 5 items of a 10 item list. 这是将以上内容与仅显示10个项目列表中的最后5个项目结合在一起的另一种解决方案。

  var addToList = function (item_value) {

    while ( list.children.length >= 10 )
    {
      list.removeChild(list.firstChild );
    }

    var item = document.createElement("li");
    item.innerText = item_value;

    list.appendChild(item);

    list.scrollTop = list.scrollHeight;

  }

A little CSS: 一些CSS:

#list {
   overflow-x:scroll;
   width: 100px;
   height: 120px;
}

#list li {
   height: 20px;
}

http://jsbin.com/ujuxer/2/edit http://jsbin.com/ujuxer/2/edit

You can use the following CSS, but I'm not sure about browser compatibility (it's unclear if this is the proper compatibility table): 您可以使用以下CSS,但是我不确定浏览器的兼容性(尚不清楚是否是正确的兼容性表):

li { display: none; } 
li:nth-last-child(-n+5) {
    display: list-item;
}

http://jsfiddle.net/TzDqV/ http://jsfiddle.net/TzDqV/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM