0

i have a table and I need to bulk updation. Before I need to check one column have no null value and the values are unique.So i loop the table.But how can i exit from the loop when same values appear in table?

i checked is there any null field.But after i cant exit from the loop where i checked unique values

when duplicate value occur i need to show in countDuplicate the row count of table.

 if (NullField==false)
    {

        $('#tblClassName tbody tr').each(function (i, item) 
        {
            countDuplicate = parseInt(countDuplicate + 1);
            $.ajax({
                type: "POST",
                url: "@Url.Action("IsAdmissionNumbervalid", "StudentDetailUpdation")",
                data: JSON.stringify({

                    AdmissionNo:             
$($(this).find("#txtAdmissionNumber")).val(),
                    FieldName: $("#ddlField option:selected").val(),
                    StudentID: $(this).find(".txtStudentID").html(),
                }),
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (response) {
                    if (response == "Failed") {
                        IsduplicateExist = true;                           

                        alert("Admission Number already exists");

                        return false;
                    }

                }

        })

            if (IsduplicateExist == true) {
                return false;
            }
            })
    }
    alert($('#tblClassName tbody tr').length)
    alert(countDuplicate)

2 Answers 2

1

The entire .each loop is iterated before you get to process the responses. You need to sequalize the ajax calls if you want to control the loop based on their results.

Jquwry 1.5 ajax request returns a jqXHR object derived from the Promis interface.

The jqXHR objects returned by $.ajax() as of jQuery 1.5 implement the Promise interface, giving them all the properties, methods, and behavior of a Promise

So, you can sequalize your ajax calls with async/await and control the loop based on the responses. First, make your function an async function. The, use await to make the iterator wait until the response arrives.

       var res = await $.ajax({
            type: "POST",
            url: "@Url.Action("IsAdmissionNumbervalid", "StudentDetailUpdation")",
            data: JSON.stringify({

                AdmissionNo:             
                   $($(this).find("#txtAdmissionNumber")).val(),
                   FieldName: $("#ddlField option:selected").val(),
                   StudentID: $(this).find(".txtStudentID").html(),
            }),

            contentType: "application/json; charset=utf-8",
            dataType: "json"


      })

      if (res === "Failed")
         return false;    //exit the loop here

Additionally, you should use a try-catch block to catch any errors.

Sign up to request clarification or add additional context in comments.

Comments

0

You need to declare the variable IsduplicateExist outside of the .each() function.

if (NullField == false) {

  var IsduplicateExist; // Declare variable outside of loop
  $('#tblClassName tbody tr').each(function(i, item) {
    countDuplicate = parseInt(countDuplicate + 1);
    $.ajax({
      type: "POST",
      url: "@Url.Action("
      IsAdmissionNumbervalid ", "
      StudentDetailUpdation ")",
      data: JSON.stringify({

        AdmissionNo: $($(this).find("#txtAdmissionNumber")).val(),
        FieldName: $("#ddlField option:selected").val(),
        StudentID: $(this).find(".txtStudentID").html(),
      }),
      contentType: "application/json; charset=utf-8",
      dataType: "json",
      success: function(response) {
        if (response == "Failed") {
          IsduplicateExist = true;

          alert("Admission Number already exists");

          return false;
        }

      }

    })

    if (IsduplicateExist == true) {
      return false;
    }
  })
}
alert($('#tblClassName tbody tr').length)
alert(countDuplicate)

2 Comments

its already declared outside the .each() function. when duplicate value occur i need the rowcount in countDuplicate variable .Now its same as $('#tblClassName tbody tr').length
@Jasmine Hmm... Does Charlie H's solution work for you?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.