vendredi 27 octobre 2017

Angular 1.6. Checkbox and Json data from database. How to get angular to show checked if in Json data?

I've been trying to look at how to represent checkbox in angular. I receive data from data base the example is : { ID: 27, ReportID: 1, CategoryID: 3, QuestionID: 23, Answer: {checkbox: {Text: "some text", data: {0: "User tasks", 1: "Scenarios"}}}

Now if we look at this then we see the "data" is the values I get from the database and there might be a lot of options that are left out since they were not checked when the record was sent to the database.

The code in the html that I've been trying to get to work with this is.

<label class="checkbox-inline">
    <input ng-model="reportInfo.Answer[question.ID]['checkbox']['data'][$index]" type="checkbox" ng-true-value="''" ng-false-value="''" ng-init="reportInfo.Answer[question.ID]['checkbox']['data'][$index]['checked'] = answerValue(ans, 'checkbox', 'data', ch.Choice)" >  
</label>

The reportInfo is empty in controller and the [*] is used to build up the json either if in edit or just making a new report. The answerValue is a function that is looking for values of existing answers or prepered records that are empty. looking like this.

$scope.answerValue = function (data, type, attribute, choice) {
    if (angular.equals({}, data.Answer)) {
        if (type == 'num')
            return 0; 
        else
            return '';
    }
    else {
        if (type == 'num')
            return data.Answer.number;
        else if (type == 'text')
            return data.Answer.text;
        else if (type == 'yesno')
            return data.Answer.yesno;
        else if (type == 'radio')
            return data.Answer.radio;
        else if (type == 'conditionalyesnotext') {
            if (attribute == 'radio')
                return data.Answer.conditionalyesnotext;
            else if (attribute == 'Text') {
                if (angular.equals({}, data.Answer.Text))
                    return '';
                else 
                    return data.Answer.Text.conditionalyesnotext;
            }
            else if (attribute == 'Textbox') {
                if (angular.equals({}, data.Answer.Textbox))
                    return '';
                else 
                    return data.Answer.Textbox.conditionalyesnotext;
            }
        }
        else if (type == 'checkbox') {
            if (attribute == 'data') {
                if (angular.equals({}, data.Answer.checkbox.data))
                    return '';
                else {
                    angular.forEach(data.Answer.checkbox.data, function(value, key) {

                      if ( value == choice) {
                        console.log(data.Answer.checkbox.data)
                      console.log(key + ': ' + value +' : ' + choice);
                        return true;
                      }
                    });
                }
            }
            else if (attribute == 'Text') 
                return data.Answer.checkbox.Text;
            else 
                return '';
        }
        else
            return  '';
    }
}

I've been looking into this and what ever I try to do. I can't get the checkboxes checked if the data exists in the json object.

Any idea on how I can solve this...

Thanks

=B




Aucun commentaire:

Enregistrer un commentaire