2014-02-23 21 views
1

我正在使用MVC模型(django)。我在模板中有一個for循環,用於從sqlite3數據庫收集數據。我需要填充一個JavaScript變量(使用JSON格式),它將數據庫中的radar.client字段分開。該字段是逗號分隔值字符串。我需要解析它到不同的「兒童」價值的json。現在讓我進入練習。JavaScript拆分字符串變量(不使用任何額外變量)

產生的JSON變種的代碼如下所示:

var json = { 
    "id": "1", 
    "name": "Server", 
    "children": [ 
     {% if clave_radar % } 
     {% for radar in clave_radar % } 
     {"id": "{{ radar.key }}", 
     "name": "{{ radar.ap }}", 
     "data": {"": "", "": ""}, 
     "children": [ 
//This is where I need to split in different children 
       {"id": "1_{{ radar.key }}", 
       "name": "{{ radar.clients }}", 
       "data": {"": "", "": ""}, 
       "children": []}, 

     ]}, 
      {% endfor % } 
     {% endif % } 
    ], 
}; 

現在讓我告訴你的服務器處理的碼看起來像客戶端的例子:

var json_test={ 
    "id": "1", 
    "name": "Server", 
    "children": [ 
     { 
     "id": "13", 
     "name": "WLT", 
     "data": {"": "","": ""}, 
     "children": [ 
      { 
      "id": "1_13", 
      "name": "081196(Intel Corporate), 68a3c4(Liteon Technology Corporation), b8d9ce(Samsung Electronics)", 
      "data": {"": "","": ""}, 
      "children": [] 
      }, 
      //Children Liteon and Samsung should appear here and not packed in 1_13 
     ] 
     }, 
     ], 
    }; 

而且這是我努力使它看起來像這樣:

varjson_real={ 
    "id": "1", 
    "name": "Server", 
    "children": [ 
    { 
     "id": "13", 
     "name": "WLT", 
     "data": {"": "","": ""}, 
     "children": [ 
     { 
      "id": "1_13", 
      "name": "081196(Intel Corporate)", 
      "data": {"": "","": ""}, 
      "children": [] 
     }, 
     { 
      "id": "2_13", 
      "name": "68a3c4(Liteon Technology Corporation)", 
      "data": { 
      "": "", 
      "": "" 
      }, 
      "children": [] 
     }, 
     { 
      "id": "3_13", 
      "name": "b8d9ce(Samsung Electronics)", 
      "data": {"": "","": ""}, 
      "children": [] 
     }, 
     ] 
    }, 
    ] 
}; 
+0

什麼是你的問題其實?試圖結束這個... –

回答

0

我剛剛找到了解決這個問題的方法。首先我被this how to split a string to an array with django

啓發我添加此功能可將radar型號:

def list_clients(self): 
    return self.clientes_encontrados.split('), ') 

然後,代碼想是這樣的:

var json_radar = { 
    "id": "1", 
    "name": "Server", 
    "children": [ 
    {% if clave_radar %} 
     {% for radar in clave_radar %} 
     {"id": "{{ radar.key }}", 
     "name": "{{ radar.ap }}", 
     "data": {"": "","": ""}, 
     "children": [ 
      {% if radar.clients != "null" %} 
      {% for client in radar.list_clients %} 
      {"id": "1_{{ radar.key }}", 
      "name": "{{ client }}", 
      "data": {"": "", "": ""}, 
      "children": []}, 
      {% endfor %} 
      {% endif %} 

     ]}, 
     {% endfor %} 
    {% endif %} 
    ], 
}; 
3

爲什麼你不循環c如下所示:

var json = { 
"id": "1", 
"name": "Server", 
"children": [ 
{% if clave_radar % } 
    {% for radar in clave_radar % } 
     {"id": "{{ radar.key }}", 
     "name": "{{ radar.ap }}", 
     "data": {"": "", "": ""}, 
     "children": [ 
     {% for client in radar.clients %} 
       {"id": "1_{{ radar.key }}", 
       "name": "{{ client }}", 
       "data": {"": "", "": ""}, 
       "children": []}, 
     {% endfor % } 
     ]}, 
    {% endfor % } 
{% endif % } 
], 
}; 
+0

我已經試過這種方式,它將在「081196(Intel Corporate),68a3c4(Liteon Technology Corporation)的每個字符上遍歷一個新的循環(您創建的'for'循環), b8d9ce(三星電子)「,我需要在這種特殊情況下(逗號分隔)發生3次。 – EnriqueH

+0

我不知道它是一個字符串,反正你可以for循環改成這樣: {%的客戶在radar.clients.split(「」)%}這是爲什麼你會這個字符串分割到3個對象。 –

+0

您正在幫助我接近解決方案......我無法在django中使用'.split'。不過,我現在正在閱讀如何從模型中拆分http://stackoverflow.com/questions/8317537/django-templates-split-string-to-array – EnriqueH