2013-04-17 156 views
0

我有多個問題,其中包含子問題。我想存儲一個數據結構,這樣當用戶選擇第一個問題時,我可以選擇子問題。此外,一些子問題使用該類別中的一般問題。最初我想過使用多維數組,但後來我意識到需要相當長的時間來搜索數組。數據結構的類型

任何建議表示讚賞。

謝謝

這是我迄今爲止的解決方案。

//Key is the question and value(object) contains all the value related to the question 
categoryToSubquestions[2] = {"What type of countertop?":{ 
           "stone_slab_countertops": "Stone slab countertops", 
           "granite_countertops" : "Granite countertops", 
           "marble_countertops" : "Marble countertops", 
           "quartz_countertops" : "Quartz countertops", 
           "slate_countertops" : "Slate countertops", 
           "solid_surface_countertops" : "Solid Surface countertops", 
           "concrete_countertops" : "Concrete countertops", 
           "corian_countertops" : "Corian countertops", 
           "formica_countertops" : "Formica countertops", 
           "stainless_countertops" : "Stainless countertops", 
           "wood_or_butcher_block_countertops" : "Wood or Butcher block countertops", 
           "laminate_countertops" : "Laminate countertops", 
           "selectKey":"MappedCategory" 

          }, 
          "What best describes your countertop project?":{ 
           "install_or_replace": "Install or Replace", 
           "repair"  : "Repair", 
           "selectKey":"describe_countertop_project" 
          }, 
          "generalQuestions2": "4" 
          }; 
//This is general question that other categories might use...It is being used in the above category 
generalQuestion[4] = {"Is this project part of a larger remodel?":{ 
          "true" : "Yes", 
          "false": "No", 
          "selectKey":"part_of_larger_remodel" 
         } 
        }; 
//THIS IS categoryToSuquestion index to value assosciation...(JUST TO KEEP TRACK) 
var keyValue = new Array(
/*0*/   "cabinets_reface", 
/*1*/   "cabinets_refinish", 
/*2*/   "cabinets_countertop_install"); 

我現在有70個這個權利,我有點擔心,如果它會減慢,一旦我不斷增加更多的問題?

+3

聽起來像一棵樹給我。 – ChaosPandion

+2

它可能有助於提供您的數據結構的示例。我不確定你的意思是「一些小問題在這個範疇內使用一般問題」。 – showdev

+0

這一切都發生在客戶端(JavaScript)或者你是否在服務器端從數據庫中進行任何操作? –

回答

0

我會創建一個像這樣的數據結構。
不要忘記,你可以像它的屬性上的Hashtable/Dictionary一樣訪問它。

var questions_data_structure = { 
    "Q1": { 
     "question_text": "Parent Question #1?", 
     "sub_questions": ["SubQues1", "SubQues3"] 
    }, 
    "SubQues1": { 
     "question_text": "Sub Question 1?", 
     "parent_question": "Q1" 
    }, 
    "SubQues3": { 
     "question_text": "Sub Question 3?", 
     "parent_question": "Q1" 
    }, 
    "Ques8": { 
     "question_text": "Regular question without children questions?" 
    } 
} 

q = questions_data_structure["Q1"]; 
alert(q.question_text); 

if (q.sub_questions && q.sub_questions.length > 0) { 
    alert("I have child questions"); 

    var i = 0, len = q.sub_questions.length, childQuestionObj; 
    for (; i < len; i++) { 
     childQuestionObj = questions_data_structure[q.sub_questions[i]]; 
     alert(childQuestionObj.question_text); 
    } 
} 
else { 
    alert("I DON'T have child questions"); 
} 

所以,如果你配合你的數據結構的KEY值到HTML控件ID,你可以做這樣的事情。

// include jQuery library 
$.each(questions_data_structure, function(question_id, value) { 
    if (value.sub_questions && value.sub_questions.length > 0) { 
     $('#' + question_id).click(function(e) { 
      /* Show or hide your child question controls */ 
     }); 
    } 
}); 
+0

我已經使用了相同的結構,但我想知道是否會減慢網站顯着。目前我有70個問題,還有更多進來。 –

+0

DOM插入速度非常快(最多需要0.5秒才能在iPhone上插入10,000個DIV)。所以不要擔心。這一切都取決於您的算法來處理您的數據結構。我寫了更復雜的嵌套子節點和近200個或更多節點的數據結構,並沒有遇到性能問題。 – stun