页内 JS 搜索在某些页面中不起作用

发布于 2025-01-10 10:06:49 字数 1949 浏览 0 评论 0原文

我在 Django Web 应用程序上使用简单的 JS 页内搜索。问题是由于某种原因它在某些视图中不起作用。它适用于 GameListView 创建的模板,但不适用于 GameListViewByDist 创建的模板。我不确定它是否相关,但两个视图都在从导航栏搜索字段所在的 base.html 扩展的 game_list.html 上呈现。由于两个视图代码非常相似,我很难找到错误。我很感激任何帮助。

**HTML**
**BASE.HTML**
<nav>  
   <input id="searchbox" type="search" placeholder="Search">
</nav>


**JS**
let type_timer;
let type_interval = 300;
let search_input = document.getElementById('searchbox');

search_input.addEventListener('keyup', () =>{
  clearTimeout(type_timer);
  type_timer = setTimeout(liveSearch, type_interval);
});

function liveSearch(){
  let cards = document.querySelectorAll('.game_card');
  let search_query = document.getElementById("searchbox").value;

  // SHOW MATCHES AND HIDE THE REST
  for (var i = 0; i < cards.length; i++){
    if(cards[i].innerText.toLowerCase().includes(search_query.toLowerCase())){
      cards[i].classList.remove("is_hidden");
    } else{
      cards[i].classList.add("is_hidden");
    }
  }
}



**VIEWS.PY**
class GameListView(SortableListMixin, ListView):
    sort_fields = [('name'), ('purchase_date')]
    model = models.Game
    ordering = ['-purchase_date']

    def get_context_data(self, **kwargs):
        context = super(GameListView, self).get_context_data(**kwargs)
        context.update({
            'tag_list': models.Tag.objects.all(),
        })
        return context


class GameListViewByDist(ListView):
    def get_queryset(self):
        self.distributor = get_object_or_404(models.Distributor, pk=self.kwargs['distributor'])
        return models.Game.objects.filter(gamestatus__distributor=self.distributor)

    def get_context_data(self, **kwargs):
        context = super(GameListViewByDist, self).get_context_data(**kwargs)
        context.update({
            'tag_list': models.Tag.objects.all(),
        })
        return context

I'm using a simple JS in-page search on my Django web application. The problem is that it isn't working in some of the views for some reason. It works on the template created by GameListView but it doesn't work on the template created by GameListViewByDist. I'm not sure if it's relevant but both views are rendered on game_list.html which extends from base.html where the navbar search field is. Since both view codes are much alike i'm having a hard time finding the error. I appreciate any help.

**HTML**
**BASE.HTML**
<nav>  
   <input id="searchbox" type="search" placeholder="Search">
</nav>


**JS**
let type_timer;
let type_interval = 300;
let search_input = document.getElementById('searchbox');

search_input.addEventListener('keyup', () =>{
  clearTimeout(type_timer);
  type_timer = setTimeout(liveSearch, type_interval);
});

function liveSearch(){
  let cards = document.querySelectorAll('.game_card');
  let search_query = document.getElementById("searchbox").value;

  // SHOW MATCHES AND HIDE THE REST
  for (var i = 0; i < cards.length; i++){
    if(cards[i].innerText.toLowerCase().includes(search_query.toLowerCase())){
      cards[i].classList.remove("is_hidden");
    } else{
      cards[i].classList.add("is_hidden");
    }
  }
}



**VIEWS.PY**
class GameListView(SortableListMixin, ListView):
    sort_fields = [('name'), ('purchase_date')]
    model = models.Game
    ordering = ['-purchase_date']

    def get_context_data(self, **kwargs):
        context = super(GameListView, self).get_context_data(**kwargs)
        context.update({
            'tag_list': models.Tag.objects.all(),
        })
        return context


class GameListViewByDist(ListView):
    def get_queryset(self):
        self.distributor = get_object_or_404(models.Distributor, pk=self.kwargs['distributor'])
        return models.Game.objects.filter(gamestatus__distributor=self.distributor)

    def get_context_data(self, **kwargs):
        context = super(GameListViewByDist, self).get_context_data(**kwargs)
        context.update({
            'tag_list': models.Tag.objects.all(),
        })
        return context

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文