How to specify only for the new added row in datatable?

How to specify only for the new added row in datatable?

ElyaNordinElyaNordin Posts: 4Questions: 3Answers: 0

Currently I'm using forEach that will execute the code for every row by row of the datatable. My problem now is, how can I execute the code only for a specific row that newly added and ignoring the previously added row?

function InStockk() {
          _datatableData.forEach(function (rowData) {
              var ID = rowData.ID;

              var quantity = parseInt($("#txtQuantity" + ID).val(), 10);
              if (!quantity) { quantity = 0; }

              var id = $("#txtItem"+ID).select2("val");
              console.log("stock id :" + id);

             $.ajax({
               url: "../WS/wsQuotation.asmx/GetStockInfo",
               type: 'GET',
               data: {
                   StockID: id ,
               },
               success: function (data) {

                   var json = JSON.parse(data);

                   var instock = parseFloat(json[0]["Inventory_Quantity"]);

                   $("#txtStock" + ID).val(instock - quantity);

               },
              error: function (xhr, status, error) {
                    alert(xhr.responseText + ":  " + error);
               }
              });
         });
    }

Answers

  • rf1234rf1234 Posts: 2,808Questions: 85Answers: 406

    Assuming you want to process the maximum ID

    function InStockk() {    
        
        if ( typeof _datatableData[0] !== 'undefined' ) {
            
            //sort array of objects descending by ID
            _datatableData.sort(function(a, b){ 
              return a.ID == b.ID ? 0 : + (a.ID < b.ID) || -1;
            }); 
            
            var ID = _datatableData[0].ID;
    
            var quantity = parseInt($("#txtQuantity" + ID).val(), 10);
            if (!quantity) { quantity = 0; }
    
            var id = $("#txtItem"+ID).select2("val");
            console.log("stock id :" + id);
    
            $.ajax({
                url: "../WS/wsQuotation.asmx/GetStockInfo",
                type: 'GET',
                data: {
                    StockID: id ,
                },
                success: function (data) {
    
                var json = JSON.parse(data);
    
                var instock = parseFloat(json[0]["Inventory_Quantity"]);
    
                $("#txtStock" + ID).val(instock - quantity);
                },
                error: function (xhr, status, error) {
                    alert(xhr.responseText + ":  " + error);
                }
            });
        }
    }
    
  • rf1234rf1234 Posts: 2,808Questions: 85Answers: 406

    If you may not change the sequence of _datatableData this could work:

    function InStockk() {   
        
        var ID = -1;
        _datatableData.forEach(function (rowData) {
            if ( rowData.ID > ID ) {
                ID = rowData.ID;
            }
        });
        
        if ( ID >= 0 ) {
             
            //sort array of objects descending by ID
            _datatableData.sort(function(a, b){
              return a.ID == b.ID ? 0 : + (a.ID < b.ID) || -1;
            });
             
            var ID = _datatableData[0].ID;
     
            var quantity = parseInt($("#txtQuantity" + ID).val(), 10);
            if (!quantity) { quantity = 0; }
     
            var id = $("#txtItem"+ID).select2("val");
            console.log("stock id :" + id);
     
            $.ajax({
                url: "../WS/wsQuotation.asmx/GetStockInfo",
                type: 'GET',
                data: {
                    StockID: id ,
                },
                success: function (data) {
     
                var json = JSON.parse(data);
     
                var instock = parseFloat(json[0]["Inventory_Quantity"]);
     
                $("#txtStock" + ID).val(instock - quantity);
                },
                error: function (xhr, status, error) {
                    alert(xhr.responseText + ":  " + error);
                }
            });
        }
    }
    
  • rf1234rf1234 Posts: 2,808Questions: 85Answers: 406

    sorry the second post was wrong; forgot to delete the sorting code:

    function InStockk() {  
         
        var ID = -1;
        _datatableData.forEach(function (rowData) {
            if ( rowData.ID > ID ) {
                ID = rowData.ID;
            }
        });
         
        if ( ID >= 0 ) {
            
            var quantity = parseInt($("#txtQuantity" + ID).val(), 10);
            if (!quantity) { quantity = 0; }
      
            var id = $("#txtItem"+ID).select2("val");
            console.log("stock id :" + id);
      
            $.ajax({
                url: "../WS/wsQuotation.asmx/GetStockInfo",
                type: 'GET',
                data: {
                    StockID: id ,
                },
                success: function (data) {
      
                var json = JSON.parse(data);
      
                var instock = parseFloat(json[0]["Inventory_Quantity"]);
      
                $("#txtStock" + ID).val(instock - quantity);
                },
                error: function (xhr, status, error) {
                    alert(xhr.responseText + ":  " + error);
                }
            });
        }
    }
    

    But why are you not using an event? Assuming you use Editor as well.
    This would give you the ID of the row created:
    https://editor.datatables.net/reference/event/create

    yourEditor.on( 'create', function ( e, json, data, ID) {
     .... your code ...
    });
    
This discussion has been closed.