Vue js循环通过对象在模板中使用v-for

时间:2019-10-29 09:58:32

标签: javascript templates vue.js v-for

谁能帮我将下面的表达式转换为vue js v-for循环表达式。我提供了一个到目前为止使用值和属性名称所做的示例。

原始

<ul class="nav nav-pills">
    {% for opt, item in filterOptions %}
      <li class="dropdown" >
        <a class="dropdown-toggle" data-toggle="dropdown" href="#" role="button" aria-haspopup="true"
          aria-expanded="false">{{opt}}</a>
        <div class="dropdown-menu">
        {% for i in item %}
          <a href="#">
            <div class="inner">
              <div class="checkbox">
                <label class="filter-label-item">
                  <input type="checkbox" data-prop="{{opt}}" value="{{i.value}}" {% if i.checked %} checked {% endif %}/>
                  <span class="cr"><i class="cr-icon fa fa-check"></i></span>
                  <span>{{i.value}} <span class="badge badge-dark">{{i.count}}</span></span>
                </label>
              </div>
            </div>
          </a>
        {% endfor %}
        </div>
      </li>
      {% endfor %}
    </ul>

到目前为止我做了什么

<ul class="nav nav-pills">
      <li class="dropdown" v-for="(value , propertyName) in filterOptions">
        <a class="dropdown-toggle" data-toggle="dropdown" href="#" role="button" aria-haspopup="true"
          aria-expanded="false">{{propertyName}}</a>
        <div class="dropdown-menu" v-for="i in value.filterOptions">
          <a href="#">
            <div class="inner">
              <div class="checkbox">
                <label class="filter-label-item">
                  <input type="checkbox" data-prop="{{propertyName}}" value="{{i.value}}" {% if i.checked %} checked {% endif %} />
                  <span class="cr"><i class="cr-icon fa fa-check"></i></span>
                  <span>{{i.value}} <span class="badge badge-dark">{{i.count}}</span></span>
                </label>
              </div>
            </div>
          </a>
        </div> 
      </li>

数据

var filterOptions = {
        year: [{ value: '2019', checked: true, count: 10 }, { value: '2013', checked: false, count: 99 }, { value: '2017', checked: true, count: 10 }],
        model: [{ value: 'Explorer', checked: true, count: 8 }, { value: 'Ram 1500', checked: false, count: 4 }, { value: 'Piloto', checked: false, count: 10 }],
        priceRange: [{ value: '$10,000-$20,000', checked: false, count: 5 }, { value: '$20,000-$30,000', checked: false, count: 22 }, { value: '$30,000-$40,000', checked: false, count: 10 }],
        mileage: [{ value: '1-1000', checked: false, count: 10 }, { value: '1001-2000', checked: false, count: 64 }, { value: '2001-3000', checked: false, count: 10 }],
        bodyStyle: [{ value: 'Crew Cab Pickup', checked: false, count: 13 }, { value: 'Quad Cab Pickup', checked: false, count: 9 }, { value: 'Sport Utility', checked: true, count: 2 }]
    }

1 个答案:

答案 0 :(得分:0)

  <ul class="nav nav-pills">
    <li class="dropdown" v-for="(items, opt) in filterOptions">
      <a class="dropdown-toggle" data-toggle="dropdown" href="#" role="button" aria-haspopup="true" aria-expanded="false">{{opt}}</a>
      <div class="dropdown-menu">
        <a href="#" v-for="item in items">
          <div class="inner">
            <div class="checkbox">
              <label class="filter-label-item">
                  <input type="checkbox" :data-prop="opt" :value="item.value" :checked="item.checked"/>
                  <span class="cr"><i class="cr-icon fa fa-check"></i></span>
                  <span>{{item.value}} <span class="badge badge-dark">{{item.count}}</span></span>
                </label>
            </div>
          </div>
        </a>
      </div>
    </li>
  </ul>