0

I have pulled data from mysql table and decode json_decode now we have json array for board,class,subject and they are related like parent ->child->grandchild(board->class->subject) board_auto_id is first of classList json array class_auto_id is the first index of classSubjectList

Note : I have posted partial array of classList and classSubjectList

var boardList           =[{"board_auto_id":"1","board_name":"CBSE"},{"board_auto_id":"2","board_name":"ICSE"},{"board_auto_id":"3","board_name":"NCERT"}];
var classList          = {"1":[{"class_auto_id":"1","class_name":"VI"},{"class_auto_id":"2","class_name":"VII"},{"class_auto_id":"3","class_name":"VIII"},{"class_auto_id":"4","class_name":"IX"},{"class_auto_id":"5","class_name":"X"},{"class_auto_id":"6","class_name":"XI"},{"class_auto_id":"7","class_name":"XII"}]};
var classSubjectList   = {"1":[{"class_auto_id":"1","sub_auto_id":"1","subject_name":"Science"},{"class_auto_id":"1","sub_auto_id":"2","subject_name":"Mathematics"},{"class_auto_id":"1","sub_auto_id":"3","subject_name":"Geography : The Earth Our Habitat "},{"class_auto_id":"1","sub_auto_id":"4","subject_name":"History : Our Pasts - I"},{"class_auto_id":"1","sub_auto_id":"5","subject_name":"Civics : Social And Political Life-I"},{"class_auto_id":"1","sub_auto_id":"86","subject_name":"English : Grammar"},{"class_auto_id":"1","sub_auto_id":"139","subject_name":"English : Writing Skills"},{"class_auto_id":"1","sub_auto_id":"155","subject_name":"English : Reading"},{"class_auto_id":"1","sub_auto_id":"209","subject_name":"संस्कृत : व्याकरण"},{"class_auto_id":"1","sub_auto_id":"220","subject_name":"Computer Science"},{"class_auto_id":"1","sub_auto_id":"235","subject_name":"Literature in English ( NCERT)"},{"class_auto_id":"1","sub_auto_id":"238","subject_name":"हिंदी व्याकरण"},{"class_auto_id":"1","sub_auto_id":"253","subject_name":"English : Vocabulary"}],"2":[{"class_auto_id":"2","sub_auto_id":"6","subject_name":"Science"},{"class_auto_id":"2","sub_auto_id":"7","subject_name":"Mathematics"},{"class_auto_id":"2","sub_auto_id":"8","subject_name":"Geography : Our Environment"},{"class_auto_id":"2","sub_auto_id":"9","subject_name":"History : Our Pasts - II"},{"class_auto_id":"2","sub_auto_id":"10","subject_name":"Civics : Social And Political Life - II"},{"class_auto_id":"2","sub_auto_id":"87","subject_name":"English : Grammar"},{"class_auto_id":"2","sub_auto_id":"140","subject_name":"English : Writing Skills"},{"class_auto_id":"2","sub_auto_id":"154","subject_name":"English : Reading"},{"class_auto_id":"2","sub_auto_id":"210","subject_name":"संस्कृत : व्याकरण"},{"class_auto_id":"2","sub_auto_id":"213","subject_name":"Computer Science"}]}

HTML code

<select name="boardId" id="boardId" class="style1"><option value="">Select Board</option></select>
<select name="classId" id="classId" class="style1"><option value="">Select Class</option></select>
<select name="subjectId" id="subjectId" class="style1"><option value="">Select Subject</option></select>
2
  • Do you want multiple, related drop-down lists? Can you post more detail? Commented Mar 18, 2015 at 7:49
  • yse i want to create related dropdown using json instead of ajax Commented Mar 18, 2015 at 7:53

2 Answers 2

1

You can do something like this to bind json to dropdown

<select id="boardList"></select>

  $.each(boardList, function (key, value) {
             console.log(value.board_auto_id);
             appenddata += "<option value = '" + value.board_auto_id + " '>" + value.board_name + " </option>";                        
         });
        $('#boardList').html(appenddata);

OnChange of boardList dropdown you need to filter records from another jsonArray and bind it to the next dropDown same as mentioned in above code.

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

Comments

0

first display board by using

jQuery.each(boardList, function(i , item) {
       selectedBoard='';// if you display no board selected 
       boardListHtml += '<option '+ selectedBoard +'   value="'+item.board_auto_id+'">'+item.board_name+'</option>';
    });

jQuery('#boardId').html(boardListHtml);

create function for class and subject for each call

function getBoardByClass(boardId){
    jQuery('#classId').html('');
    var classChapterListHtml = '<option value="">Select Class</option>';
    jQuery.each(classList, function(i , item) {
       var selectedClass = (boardId ==item.class_auto_id) ? 'selected' : '';    
       classChapterListHtml += '<option '+ selectedClass +'   value="'+item.class_auto_id+'">'+item.class_name+'</option>';
    });
    jQuery('#classId').html(classChapterListHtml);

}


function getSubjectByClass(classID){

    jQuery('#subjectId').html('');
    var subjectListHtml = '<option value="">Select Subject</option>';
    jQuery.each(classSubjectList, function(id , subjectList) {
            if (classID==id) {
            jQuery.each(subjectList, function(subIds , subjectName) {

                subjectListHtml += '<option value="'+subjectName.sub_auto_id+'">'+subjectName.subject_name+'</option>';

                });

            }

    });

    jQuery('#subjectId').html(subjectListHtml);

}

and finally change event for class and subject filter

$("#boardId").change(function() {
        jQuery('#classId').html('');
        getBoardByClass($(this).val());

        });

$("#classId").change(function() {
        jQuery('#subjectId').html('');
        getSubjectByClass($(this).val());

        }); 

Comments

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.