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

222
Visualizações
Svelte on navigation, how to keep input values?

The problem here is that when I enter values in my input, the default values get set back into my input. I want to keep them while updating my route query params. I thought of stripping values out of variables and assigning them back but, that is a bad idea since the values only return in a string in the reactive.

<script>
  let facetFilters = [
        `collections:${slug}`
    ]
    let priceFilter
    let priceLeft = 0;
    let priceRight = 5000;
    $: prices = [priceLeft, priceRight]
    $: {
       /**
        * Seperate the facets in to different arrays. If facet has multiple values each value is added to the same array item
        * All values of a facet are seperated by a comma like so vendor=Bikkel,Merida -> url output: ?vendor=bikkel%2Cmerida
        * The price will be fitered out of the facet filters array
        * */
        
            let params = [...$page.url.searchParams.entries()]
            console.log(params)
            let price = params.filter(([key]) => key == 'price')
            .map(([key, values]) => values.split(',').map((value) => `${key}:${value}`));
            
            /* Convert price that is currently in an array to string */
            priceFilter = price.toString() /* when the url is like this '?price=1200+TO+2400' the output will be: price:1200 TO 2400 */
            
            let result = params.filter(([key]) => key !== 'price')
            .map(([key, values]) => values.split(',').map((value) => `${key}:${value}`));

            facetFilters =  [facetFilters, ...result] 
            
            
           
  }
  async function search(key, value) {
        
        let query = new URLSearchParams($page.url.searchParams.toString())
        
        if(key == 'price') {
            let val = value.join(" TO ")
            query.set(key, val)
        } else {
            console.log(value)
            query.set(key, value.split(","))
        }
        
        return goto(`?${query.toString()}`);
    }
</script>
<div>
   <span>&euro;</span>
   <input type="text" placeholder="0" bind:value={priceLeft}/>
   <span>tot</span>
   <input type="text" placeholder="5000" bind:value={priceRight}/>
   <button type="button" on:click={ () => search('price', [priceLeft, priceRight])}>
       update
   </button>
</div>
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

I assume that if the user navigates for the first time, you want to calculate the prices, and thereafter keep what the user entered. If this is the case, you could try and write your values to a store, and then, in your reactive block you either return what's in the store, if there is anything, or return your calculation.

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