简体   繁体   中英

tree structure in html with dynamic content

I have data coming from the database as the below format on query execution

[(‘Country-1’, ‘state1’), (‘Country-1’, ‘state2’), (‘Country-1’, ‘state3’),
(‘Country-2’, ‘state1’), (‘Country-2’, ‘state2’), (‘Country-2’, ‘state3’),
(‘Country-3’, ‘state1’), (‘Country-3’, ‘state2’), (‘Country-3’, ‘state3’)]

I want to convert to as this resultset as below format

context = { 
    'countries': [ { 'Countryname': 'country1’,
                    'state': [ {  'Statename': 'state1'},
                               {'Statename': 'state2'},
                               {'Statename': 'state3'} ]
                    },
                    { 'Countryname': 'country2’,
                    'state': [ {  'Statename': 'state1'},
                               {'Statename': 'state2'},
                               {'Statename': 'state3'} ]
                    }, 
                    { 'Countryname': 'country3’,
                    'state': [ {  'Statename': 'state1'},
                               {'Statename': 'state2'},
                               {'Statename': 'state3'} ]
                    }
                ]
}

So that I can iterate the data in the in HTML in Django to create the tree format:

<ul class = "myUL">
  {% for country in data %}
            <li class = "caret"> {{ country.countryname }} </li>
            <ul class="nested">
              {% for state in country.statename %}
                <li>{{state.statename}}</li>
                {% endfor %}
            </ul>
  {% endfor %}

Expected output for HTML is:

   Country-1  
             State1
             State2
             State3 
   Country -2 
             State1
             State2
             State3 
   Country -3 
             State1
             State2
             State3 

Try the following:

Data parser:

data = [('Country-1', 'state1'), ('Country-1', 'state2'), ('Country-1', 'state3'), ('Country-2', 'state1'), ('Country-2', 'state2'), ('Country-2', 'state3'), ('Country-3', 'state1'), ('Country-3', 'state2'), ('Country-3', 'state3')]

reformatted_data = {}

for pair in data:

    state_list = reformatted_data.get(pair[0], None)

    if state_list:
        if pair[1] in state_list:
            pass
        else:
            reformatted_data[pair[0]].append(pair[1])
    else:
        reformatted_data[pair[0]] = [pair[1]]

# Try this print in your console to make sure it's working properly
print(reformatted_data)

Template to display data:

<ul class = "myUL">
  {% for country, statelist in data.items %}
            <li class = "caret"> {{ country }} </li>
            <ul class="nested">
              {% for state in statelist %}
                <li>{{ state }}</li>
                {% endfor %}
            </ul>
  {% endfor %}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM