0

So, I have 3 dropdowns, one for Employee ID, second for monthly and third for quartely, so the data in monthly and quaterly dropdown is called from Database, so I used this AJAX call for the same.

So this works like, when an ID is selected that has data everything loads well, but when an ID is selected without data it shows 'No data available'.

The main issue starts there. After selecting an ID with no Data, the ID with data also shows no data.

For example, an 01 ID is selected from Employee ID drop down, and it has monthly or quartely data-- Works fine
02 ID is selected from the dropdown, and it has no data, shows No Data Available -- Works
But when again 01 is selected, it shows no data available, so you will have refresh the page to see the data. -- issue

Please if someone could help me. I'm very new to AJAX call. Thank you.

$('select[name="employee_id"]').on('change', function() {
  console.log('inside script')
  var employee_id = document.getElementById('employee_id').selectedOptions[0].value;
  console.log(employee_id)

  $.ajax({
    type: "POST",
    url: "/kpiDetailsList",
    datatype: "json",
    data: JSON.stringify({
      'employee_id': employee_id,
    }),
    contentType: 'application/json;charset=UTF-8',
    success: function(data) {
      console.log(data);
      var parsed_data = data;
      
      monthly_list = parsed_data.monthly_list;
      quarterly_list = parsed_data.quarterly_list;   
      console.log(monthly_list.length)
      console.log(quarterly_list.length)

      if (monthly_list.length != 0 || quarterly_list.length != 0) {
        console.log('inside first if')
        
        $("#list1").attr("enabled", "enabled").on('click');
        $('#list1_items').empty();
        
        for (index = 0; index < monthly_list.length; index++) {
          $('#list1_items').append("<li><input type='checkbox' name='month' value='" + monthly_list[index] + "'id='" + monthly_list[index] + "'><label for='" + monthly_list[index] + "'>" + monthly_list[index] + "</label></li>");
        }
        
        $("#list2").on('click');
        $('#list2_items').empty();
        
        for (index = 0; index < quarterly_list.length; index++) {
          if (quarterly_list[index] == 'Quarter1') {
            $('#list2_items').append("<li><input type='checkbox' name='quarter' value='" + quarterly_list[index] + "'id='" + quarterly_list[index] + "'><label for='" + quarterly_list[index] + "'> Apr - Jun (Quarter 1) </label></li>");
          } else if (quarterly_list[index] == 'Quarter2') {
            $('#list2_items').append("<li><input type='checkbox' name='quarter' value='" + quarterly_list[index] + "'id='" + quarterly_list[index] + "'><label for='" + quarterly_list[index] + "'> Jul - Sep (Quarter 2) </label></li>");
          } else if (quarterly_list[index] == 'Quarter3') {
            $('#list2_items').append("<li><input type='checkbox' name='quarter' value='" + quarterly_list[index] + "'id='" + quarterly_list[index] + "'><label for='" + quarterly_list[index] + "'> Oct - Dec (Quarter 3) </label></li>");
          } else if (quarterly_list[index] == 'Quarter4') {
            $('#list2_items').append("<li><input type='checkbox' name='quarter' value='" + quarterly_list[index] + "'id='" + quarterly_list[index] + "'><label for='" + quarterly_list[index] + "'> Jan - Mar (Quarter 4) </label></li>");
          } else {
            console.log('Data not found')
          }
        }
      } else if (monthly_list.length == 0 && quarterly_list.length == 0) {
        console.log('inside second if')
        $("#list1").attr("disabled", "disabled").off('click');
        $("#list1").attr("title", "Data not available");
        $("#list2").attr("disabled", "disabled").off('click');
        $("#list2").attr("title", "Data not available");
      } else if (monthly_list.length == 0 || quarterly_list.length == 0) {
        console.log('inside third if')
        if (monthly_list.length == 0) {
          console.log('inside monthly list')
          $("#list1").attr("disabled", "disabled").off('click');
          $("#list1").attr("title", "Data not available");
        } else {
          $("#list2").attr("disabled", "disabled").off('click');
          $("#list2").attr("title", "Data not available");
        }
      } else {
        console.log("data not found")
      }
    },
    error: function(e) {
      console.log("Error :" + e)
    }
  });
});
Riddhi Dua
  • 91
  • 8
  • 1
    Not really related to this question but you should look at cleaning up your onSuccess by turning some of it into functions – Richard Hpa Jun 28 '21 at 10:16
  • Not sure if relevant, but on fail you disable and off(click) on both list1 and list2, but on success you don't enable list2. `$("#list1").attr("enabled", "enabled").on('click');` `$("#list2").on('click');`. If `select[name="employee_id"]` == `#list2` then your event won't be firing. Is your event firing on the third step (1 valid, 2 false, 3 valid <-- this one) (do you get 'inside script')? – freedomn-m Jun 28 '21 at 10:40
  • If you need more help, you'll need to provide a [mcve]. You could split the code in snippet into 2 with two buttons (one that provides valid data and one that provides incorrect data) as it's not possible to provide an ajax call. – freedomn-m Jun 28 '21 at 10:42
  • Does this answer your question? [Stop all active ajax requests in jQuery](https://stackoverflow.com/questions/1802936/stop-all-active-ajax-requests-in-jquery) – Skatox Jun 28 '21 at 13:44

1 Answers1

1

You could just return from the function before you even run the ajax function.

$('select[name="employee_id"]').on('change', function() {
  console.log('inside script')
  var employee_id = document.getElementById('employee_id').selectedOptions[0].value;
  console.log(employee_id)
  if(employee_id == null){
    return;
  }

  ...rest of your function

}

This issue doesn't really have anything to do with the ajax part of it but more about validation of the input field.

Edit.

After reading your comment I see I misinterpreted the question, but the same idea actually still stands. Just return from the function. Calling a return anywhere in the function will just stop the function from running.

    success: function(data) {
      console.log(data);
      var parsed_data = data;
      if(!data){
         return
      }
    }
Richard Hpa
  • 2,353
  • 14
  • 23
  • Actually we have dropdown for Employee ID, so the issue is that when an employee ID is selected that has no data, and after that if an employee ID is selected that has data even that will show No Data, until you dont refresh the page. – Riddhi Dua Jun 28 '21 at 10:21
  • Sorry, I misinterpreted the question, but hope my edit now solves it (remember to mark it complete if it did). And like I said in a comment on the question. Have a look at cleaning up that success function as there is quite a bit going on. – Richard Hpa Jun 28 '21 at 10:34
  • Thats okay! but no it isnt working at the same issue remains. Ya sure Ill do that thanks. – Riddhi Dua Jun 28 '21 at 10:38
  • What comes back from the data variable if there is 'no data' – Richard Hpa Jun 28 '21 at 19:21