Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

152
Visualizações
How to truncate query results with javascript?

I have a brand search. I want an adjustment to the query view to be displayed. In the example image below, I am searching for Meto brand, so I display the search results also under the name Meto. I want to display only the first character, if I search for Meto, it will only show the letter M.

How to do that with javascript?

enter image description here

My Template

<div class="brands-list">
    @for ((key, val): brandsByFirstChar) {
        <div class='brand-by-alphabet'>
            <h3 class='alphabet' id='alpha-@key'>@key</h3>
            <div class="row brand-collection-container">
                <div class='col-sm-3 brand-collection'><ul>
                    @for ((i, b): val) {
                        <li><a href="/search?srp-brandIds=@b.getId()&srp-actionTrigger=BRAND&srp-brandMode=true">@b.getName()</a></li>
                    @if ((i.index() % 1) == 0) {    
                        </ul>
                </div> 
                <div class='col-sm-3 brand-collection'><ul>
                    }
                }
                </ul>
                </div>
              </div>
            </div>
            }
        </div>

My JS

$(document).on("keypress", "#store-searchText", function (e) {
    if (e.which == 13) {
        var inputVal = $(this).val();
        window.location = location.href.replace(location.search, '') + "?q=" + inputVal
    }
});
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

For a js solution, you could change all h3.alphabet to replace with just the first character.

Using the .text overload with a function callback calls .text with each elements text in turn.

However you will get the FOUC (flash of unstyled content) as the page loads then runs your javascript - this would be best changed server-side (in the template)

$(function() {
  $(".brands-list h3.alphabet").text(function(i, txt) {
    return txt.substr(0, 1);
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="brands-list">
  <div class='brand-by-alphabet'>
    <h3 class='alphabet'>Bosch</h3>
    <div class="row brand-collection-container">
      <div class='col-sm-3 brand-collection'>
        <ul>
          <li>Bosch</li>
        </ul>
      </div>
    </div>
    <h3 class='alphabet'>Metro</h3>
    <div class="row brand-collection-container">
      <div class='col-sm-3 brand-collection'>
        <ul>
          <li>Metro</li>
        </ul>
      </div>
    </div>
  </div>
</div>


Alternatively, if you made an ajax call instead of reloading the page, you could intercept the html and amend it before it's displayed.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda