Send input to save to JSON file

Asked

Viewed 108 times

0

I have a screen where there is an auto-complete for sale items, the user searches the item by name or code, so far everything works.

I need to capture the chosen item and send it to backend (Django) to save to a JSON file. The idea is to have a JSON for each account.

But I don’t know how to send the inputs to the backend so that you can record this to a file, since I will upload that same file to fill a table of the chosen registered items.

This is my autocomplete code.

$(function() {

    $("#itemsearch").autocomplete({
      source: "/menus/autocomplete_search/",

      select: function (event, ui)
       {
       AutoCompleteSelectHandler(event, ui)
       {
            ui.item.value = "";
        }

        },
        minLength: 1,

    });
});

var result = 0
var price = 0

  function AutoCompleteSelectHandler(event, ui)
  {
    var selectedObj = ui.item;
    var name = selectedObj.value
    var codigo = selectedObj.item_num
    price = selectedObj.item_price
    var table = document.getElementById('tableItemVenda');
    var row = table.insertRow(1);
    row.innerHTML = "<td>"+codigo+"</td> <td>"+name+"</td> <td>R$ "+price+"</td>";
    inserirTotal(price);

}
  • What does chosen mean? Select on the page? Submit a form? This will determine which event you will call and the time you call the backend to record the selection

  • @Leonardopessoa the selected item is the ui.item of the autocomplete. It works like this, when typing the name or code, the(s) items appear in the autocomplete dropdown list, to choose the item, the user navigates with the arrows and press enter on the item you want.

1 answer

0

I was able to capture the user’s choices by calling the new 'Saveinvoice' function as soon as a new item is selected. Whenever an item is selected, it is saved in the selectedObj variable.

$(function() {

    $("#itemsearch").autocomplete({
      source: "/menus/autocomplete_search/",

      select: function (event, ui)

       {
       AutoCompleteSelectHandler(event, ui)
       {
            ui.item.value = "";
            SaveInvoice(); //chama funcao para salvar item para o json
        }

        },
        minLength: 1,

    });
  });

function AutoCompleteSelectHandler(event, ui)

  {
    selectedObj = ui.item;
}


function SaveInvoice(){
        $.ajax({
            url: "/menus/gerar_conta/",
            type: "POST",
            dataType: 'json',
            data: {selectedObj, csrfmiddlewaretoken: '{{ csrf_token }}'},
        });
    }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.