2017-03-01 24 views
7

如果我的术语不正确,我很抱歉 - 这绝对不是我的专业领域。列出按键分组的数组内容

我期待从json文件中创建一个<select>列表,并通过键将<optgroup>s中的条目分组。我已经成功地列出了选择中的所有条目,但不知道如何循环并嵌套项目。

我的JSON是这样的:

[ 
    { 
     "Type" : "Overdrive", 
     "Brand" : "Chase Bliss", 
     "Name" : "Brothers", 
     "Width" : 2.75, 
     "Height" : 4.77, 
     "Image" : "public/images/pedals/chasebliss-brothers.png" 
    } 
] 

这里是我正在渲染<select>

window.RenderPedals = function(pedals){ 
    for(var i in pedals) { 
     var $pedal = $("<option>"+ pedals[i].Brand + " " + pedals[i].Name +"</option>").attr('id', pedals[i].Name.toLowerCase().replace(/\s+/g, "-").replace(/'/g, '')); 
     $pedal.data('width', pedals[i].Width); 
     $pedal.data('height', pedals[i].Height); 
     $pedal.data('height', pedals[i].Height); 
     $pedal.data('image', pedals[i].Image); 
     $pedal.appendTo('.pedal-list'); 
    } 
} 

我所寻找的是这样的标记:

<select> 
    <optgroup label="Chase Bliss"> 
     <option data-width="..." data-height="..." data-image="...">Chase Bliss Brothers</option> 
    </optgroup> 
</select> 

能任何人都指向正确的方向?

+1

如果'pedals'是一个数组,你应该看看[这个问题,它的答案(http://stackoverflow.com/questions/500504/why-is-using - 对于功能于与阵列迭代-A-坏主意?RQ = 1)。 –

回答

0

你可以使用对象解构当前对象的属性,没有for..in循环,创建一个<optgroup>元素与label属性设置为"Brand",追加<option><optgroup>,追加<optgroup><select>。检查是否存在具有label属性设置为Brand<optgroup>元素,如果true<option>追加到<optgroup>,否则新<optgroup>元素<select>追加与html一套新<option>元素。

var data = [{ 
 
    "Type": "Overdrive", 
 
    "Brand": "Chase Bliss", 
 
    "Name": "Brothers", 
 
    "Width": 2.75, 
 
    "Height": 4.77, 
 
    "Image": "public/images/pedals/chasebliss-brothers.png" 
 
} 
 
, { 
 
    "Type": "Underdrive", 
 
    "Brand": "Bliss Chase", 
 
    "Name": "Sisters", 
 
    "Width": 5.72, 
 
    "Height": 7.74, 
 
    "Image": "public/images/pedals/chasebliss-sisters.png" 
 
} 
 
, { 
 
    "Type": "Throughdrive", 
 
    "Brand": "Bliss Chase", 
 
    "Name": "Cousins", 
 
    "Width": 2.75, 
 
    "Height": 4.74, 
 
    "Image": "public/images/pedals/chasebliss-cousins.png" 
 
} 
 
]; 
 

 
window.RenderPedals = function(pedals) { 
 
    var {Type, Brand, Name, Width, Height, Image} = pedals; 
 
    var option = $("<option>", { 
 
       text: `${Brand} ${Name}`, 
 
       id: Name.toLowerCase() 
 
        .replace(/(\s+)|(['"])/g, (m, p1, p2) => p1 ? "-" : ""), 
 
       data: { 
 
        width: Width, 
 
        height: Height, 
 
        image: Image 
 
       } 
 
       }); 
 
    if ($("optgroup").is(`[label="${Brand}"]`)) { 
 
    $(`optgroup[label="${Brand}"]`).append(option); 
 
    } else { 
 
    $("<optgroup>", { 
 
     label: Brand, 
 
     html: option 
 
    }).appendTo(".pedal-list"); 
 
    } 
 
} 
 

 
data.forEach(RenderPedals);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"> 
 
</script> 
 
<select class="pedal-list"></select>

+0

这不会错过'分组'部分吗?即阵列中的所有条目将列在第一个品牌下。 – bsyk

+0

@bsyk不确定你的意思?问题中只有一个对象出现在数组中。 OP可以使用'data.forEach(RenderPedals)' – guest271314

+0

我假设问题中的数据被简化为单个条目,并且在真实应用程序中会有更多条目,否则分组列表框的要点是什么。 尝试使用不同的品牌值向阵列中添加更多条目。 – bsyk

0

如果你有这样的匹配HTML的数据结构,这将是更容易推理。

给出

[ 
    { 
     "Type" : "Overdrive", 
     "Brand" : "Chase Bliss", 
     "Name" : "Brothers", 
     "Width" : 2.75, 
     "Height" : 4.77, 
     "Image" : "public/images/pedals/chasebliss-brothers.png" 
    } 
] 
,而你想要的结构

<select> 
    <optgroup label="Chase Bliss"> 
     <option data-width="..." data-height="..." data-image="...">Chase Bliss Brothers</option> 
    </optgroup> 
</select> 

,而不是试图让从JSON这种转化成HTML,创建的HTML相匹配的新的数据结构

。如果数据结构与HTML结构相匹配,则呈现它的逻辑将更容易。

像下面将工作做好..

[ 
    { 
    brandName: "Chase Bliss", 
    items: [ { name: "Brothers" /* width, height, etc */ } ] 
    } 
] 

你可以写一个减少功能,使这一转变:

let optionGroups = pedals.reduce((optionGroups, pedal) => { 
    let optionGroup = optionGroups.find(group => pedal.Brand === group.brandName) || {} 

    if (!optionGroup.brandName) { 
    optionGroup.brandName = pedal.Brand 
    optionGroup.items = [] 
    optionGroups.push(optionGroup) 
    } 

    optionGroup.items.push(pedal) 
    return optionGroups 
}, []) 

渲染功能,可以那么就映射在所述外组,然后是内部物品。

window.renderPedals = (optionGroups) => { 
    optionGroups.map((group) => { 
    let $optgroup = $("<optgroup>", { 
     label: group.brandName 
    }); 

    group.items.map((pedal) => { 
     let {Type, Brand, Name, Width, Height, Image} = pedal; 
     let $pedal = $("<option>", { 
     text: `${Brand} ${Name}`, 
     id: `${Name.toLowerCase().replace(/\s+/g, "-").replace(/'/g, '')}`, 
     data: { 
      width: Width, 
      height: Height, 
      image: Image 
     }, 
     appendTo: $optgroup 
     }); 
    }) 
    $optgroup.appendTo(".pedal-list"); 
    }) 
} 

https://jsfiddle.net/f4x9vk0f/2/