Search code examples
javascriptjqueryinputeachdevextreme

keep the memory of more than one index


I have more than three TextBoxes. I want to send this data as in the code. The "indeks " value is always the last click. How do I keep the index?

      click:function(e) {

  var item = e.itemElement.index();
indeks = item;                   
 }
      var field= "";      
     onl: function () {
            $.ajax({
        type: "GET",
        cache:true,
        url: MYURL,
        success: function (msg, result, status, xhr) {
            var obje = jQuery.parseJSON(msg)
            var i = 0;  
            field = " ";
            $('#wrapper *').filter(':input').each(function () {

                if (txtvalue != "") {
                    if (i) 
                        field += " and ";
                    field = field + "[" + obje[indeks]+ "]" $(this ).val() + "'";                      

                    i++;
                }

            });
                });

},

Solution

  • As I wasn't sure if I got your question right, I prefered to comment on your topic - but now it seems that my answer helped you, so I decided to post it as an actual answer:

    1st step: declare an array outside your success handler
    2nd step: push the index and the value for the element into this array
    3rd step: loop through all entries of the array and build your 'select' statement

    Here is your edited example: https://jsfiddle.net/Lk2373h2/1/

           var clickedIndexes = [];
        click:function(e) {
    
      var item = e.itemElement.index();
    indeks = item;                   
     }
      var field= "";      
         onl: function () {
                 $.ajax({
        type: "GET",
        cache:true,
        url: MYURL,
        success: function (msg, result, status, xhr) {
            var obje = jQuery.parseJSON(msg)
            var i = 0;  
            field = " ";
            $('#wrapper *').filter(':input').each(function () {
                $(this).attr('id', i);
                var txtvalue=$(this).val();
                  if (i) 
                                clickedIndexes.push({
                                    index: indeks,
                                    value: $(this ).val()
                                });
                            var selectString = "";
    
                            for (var u = 0; u < clickedIndexes.length; u++) {
                                selectString += "[" + obje[clickedIndexes[u].index].ALAN + "]" + "=" + "'" + clickedIndexes[u].value + "'";
    
                            }
    
                            field = selectString;  
    
                            i++;
    
            });
                });
    
    },