-1

i want to access textStatus after ajax.now i want to check this variable in if condition anyone help me $this->registerJs("colorArray = ['#ff4c4c','#32CD32'];

    $('.grooveTable').on('click','td', function(){
    color = $(this).data('color') == undefined ? 0 : $(this).data('color')*2;

        // Get Url Parameter

        var result = [];
        window.location.search.replace(/[?&]+([^=&]+)=([^&]*)/gi, 
        function(str, key, value) {
            result[key] = value;
        });

    $.ajax({
    url: '" . yii\helpers\Url::toRoute("area-chart/change-area-status") . "',
    dataType: 'json',
    method: 'GET',
    data: {areaCodeId : $(this).attr('id'),colourCode:color, location:result['tion%5D'],
    company_code:result['ny_code%5D'],division_code:result['rt%5Bdivision_code%5D']},
    success: function (data, textStatus, jqXHR) {
    statusCheck = data;
    drawChart(data.idCompleted, data.idPending, data.idStarted);
    },});

        if(textStatus == 'SUCCESS' && color == undefined || color == colorArray.length ){

                $(this).css('background-color',colorArray[0]);
                $(this).data('color','0');

        }else if(textStatus == 'SUCCESS' == 'UPDATE'){

                $(this).css('background-color',colorArray[color+1]);
                $(this).data('color',color+1);

        }

});");

Developer
  • 29
  • 5

2 Answers2

2

ajax is async. just pass the parameter to your functions

$.ajax({
    url: '" . yii\helpers\Url::toRoute("area-chart/change-area-status") . "',
    dataType: 'json',
    method: 'GET',
    data: {
        areaCodeId: $(this).attr('id'), colourCode: color, location: result['tion%5D'],
        company_code: result['ny_code%5D'], division_code: result['rt%5Bdivision_code%5D']
    },
    success: function (data, textStatus, jqXHR) {
        drawChart(data.idCompleted, data.idPending, data.idStarted);
        doSomething(textStatus);
    },
});


function doSomething(textStatus){
    if(textStatus == 'SUCCESS' && color == undefined || color == colorArray.length ){

        $(this).css('background-color',colorArray[0]);
        $(this).data('color','0');

    }else if(textStatus == 'SUCCESS'){

        $(this).css('background-color',colorArray[color+1]);
        $(this).data('color',color+1);

    }
}
izik
  • 1,058
  • 7
  • 16
0

Try with below code:

   $.ajax({
  url: '" . yii\helpers\Url::toRoute("area-chart/change-area-status") . "',
  dataType: 'json',
  method: 'GET',
  data: {
    areaCodeId: $(this).attr('id'),
    colourCode: color,
    location: result['tion%5D'],
    company_code: result['ny_code%5D'],
    division_code: result['rt%5Bdivision_code%5D']
  },
  success: function(data, textStatus, jqXHR) {
    drawChart(data.idCompleted, data.idPending, data.idStarted);
updatebackground($(this),textStatus, color, colorArray); // call function here
  },
});

move all code to a seperate funtion:

funtion updatebackground(element,textStatus, color, colorArray){
if (textStatus == 'SUCCESS' && color == undefined || color == colorArray.length) {

  element.css('background-color', colorArray[0]);
  element.data('color', '0');

} else if (textStatus == 'SUCCESS') {

  element.css('background-color', colorArray[color + 1]);
  element.data('color', color + 1);

}
}

Thanks,

Sonu Singh
  • 191
  • 8