show list according to the letter clicked in jade data retrieve from mongodb

℡╲_俬逩灬. 提交于 2019-12-12 05:39:23

问题


I sent my data to pug file as data.brand_name to show all brand

Now i want to show the brands according to alphabets.

I want when i click on a letter 'a' then it only show a list which has starting letter 'a'

I don't got how to do this,please help me.

EDIT 1

my pug file for aplhabet is like this

 div.row
                            div.drug_alphabets.pull-left
                                span.center
                                    ul
                                        li
                                            a.btn2.hoverable(href='#') A
                                        li
                                            a.btn2.hoverable(href='#') B
                                        li
                                            a.btn2.hoverable(href='#') C
                                        li
                                            a.btn2.hoverable(href='#') D

I am using this code to display brands

   for each brand in data   
      h4 brand- #{brand.brand_name} 

i get brands like

brand - a
brand - b
brand - c

回答1:


You can jQuery to achive your goal. But first of all you need to add a additional class for parent div of brand names (in this case I used .brands) and add an attribute for h4 element to store brand names.

.brands
    for each brand in data   
        h4(name=brand.brand_name) brand- #{brand.brand_name} 

Then include jQuery to your page and use following script after page load.

$(".drug_alphabets a").on("click", function() {

    var clickedLetter = $(this).text()

    $(".brands h4").each(function() {
        var brandName = $(this).attr("name")
        if (brandName && brandName.toLowerCase()[0] == clickedLetter.toLowerCase()) {
            $(this).show();
        } else {
            $(this).hide();
        }
    });

})

Here is working snippet : https://codepen.io/anon/pen/ZavPNo



来源:https://stackoverflow.com/questions/47381397/show-list-according-to-the-letter-clicked-in-jade-data-retrieve-from-mongodb

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!