jquery-typeahead icon indicating copy to clipboard operation
jquery-typeahead copied to clipboard

Regenerate localsotrage data from a function

Open anpegar opened this issue 10 years ago • 3 comments

Hi everyone !!

I have a Typeahead created with the following configuration but I want to regerate the localstorage data from a javascript function, how I can I do it?

My typeahead:

$.typeahead({
            input: '#creaProdSearch',
            minLength: 1,
            maxItem: 0,
            accent: {
                from: 'áéíóúÁÉÍÓÚ',
                to: 'aeiouAEIOU'
            },
            order: "asc",    
            hint: true,
            generateOnLoad: true,
            cache: true,    
            compression: true,              
            template: "{{descripcion}} <br><small style='color:#999;'>Ref: {{refProducto}}</small>",
            source: {
                productos: {
                    display: ["descripcion","refProducto"],
                    ajax: function (query) {
                        var params = {
                                P_TEXTO: query,
                                P_EMPRESA: SavePedido.empresa,
                        }

                        return {
                            type: "POST",
                            url: "/crm/services/pedidos/productos/getProductos",
                            dataType: "json",
                            contentType: "application/json",
                            path: "rows",
                            data: JSON.stringify(params)
                        }
                    } 
                }
            },
            callback: {             
                onClickAfter: function (node, a, item, event) {

                    event.preventDefault();      
                }
            }
        });

My function to regenerate the typeahead:

function regenerate() {

  localStorage.removeItem("TYPEAHEAD_#creaProdSearch:productos");
  Typeahead["#creaProdSearch"].isGenerated = false;
  Typeahead["#creaProdSearch"].source = {};
  Typeahead["#creaProdSearch"].result = [];
  Typeahead["#creaProdSearch"].resultCount = 0;
  Typeahead["#creaProdSearch"].node.trigger('generateOnLoad.typeahead');
}

The above function delete the localstorage but not trigger the 'generateOnLoad.typeahead' method and, of course, not built again the localstorage and the Typeahead. What's wrong?

Thanks in advance.

anpegar avatar Sep 20 '16 12:09 anpegar

Hi again !! I have solved so, but I don't know if this is a good solution.

var params = {
  P_TEXTO: $("#creaProdSearch").val(),
  P_EMPRESA: SavePedido.empresa,
}
function regenerate() {
   localStorage.removeItem("TYPEAHEAD_#creaProdSearch:productos");
  Typeahead["#creaProdSearch"].node.trigger('generate.creaprodsearch.typeahead');
}

The idea is to make a typeahead reset and reload data again (deleting the cache).

anpegar avatar Sep 20 '16 13:09 anpegar

hi @anpegar yep this is a 2 step like your regenerate function does 1- clear storage 2- generate Typeahead source

running-coder avatar Sep 20 '16 15:09 running-coder

ok, thanks.

anpegar avatar Sep 21 '16 11:09 anpegar