【问题标题】:Dynamic List with jquery json data带有 jquery json 数据的动态列表
【发布时间】:2020-11-19 17:40:16
【问题描述】:

如何将以下条件附加到 id = #sellerMetal <ul> 容器,它是一个动态循环列表。

如果 dataJS
productList > 奖牌 > 金牌 = true,
追加<li class="icon"><img src="/img/goldmetal.png"></li>

如果 dataJS
productList.medals.silvermedal = true,
追加<li class="icon"><img src="/img/silvermedal.png"></li>

如果 dataJS
productList > 奖牌 > newshop= true,
追加<li class="icon"><img src="/img/newshop.png"></li>
...
...
...
...
...

这是产品列表。

下面是 Product List Append 的渲染列表。

预期结果:

<div id="List">
<div class="feature_ico">
    <ul id="#sellerMetal" class="icons">
        (Apend to here)
    </ul>
</div>
<div class="feature_ico">
    <ul id="#sellerMetal" class="icons">
        (Apend to here)
    </ul>
</div>
<div class="feature_ico">
    <ul id="#sellerMetal" class="icons">
        (Apend to here)
    </ul>
</div>
<div class="feature_ico">
    <ul id="#sellerMetal" class="icons">
        (Apend to here)
    </ul>
</div>
</div>

下面是示例 sn-p。

以下是我尝试过的代码:comment //Condition is here I have tried 是我提出的条件,它不像预期的那样工作。

//Below is the Data JS
var data = { productList: [
    {
        id: "e0c18c12-0b51-41ad-9f2c-aefc20cafcdb",
        link: "1",
        imgurl: "img001",
        text: 'Product 001',
        seo: '',
        medals: [{
            goldmedal: true,
            sellermedal: false,
            newshop: true
            }]
    },
    {
        id: "f3bee385-76d7-4a87-8bba-a51d33238858",
        link: "2",
        imgurl: "img002",
        text: 'Product 002',
        seo: '',
        medals: [{
            goldmedal: true,
            sellermedal: true,
            newshop: true
            }]
    },
    {
        id: "fc3dd6a9-5d8c-4262-aa65-a680e0f0cafe",
        link: "3",
        imgurl: "img003",
        text: 'Product 003',
        seo: '',
        medals: [{
            goldmedal: false,
            sellermedal: false,
            newshop: true
            }]
    },
    {
        id: "8615711e-8544-4484-933f-b14a93941b86",
        link: "4",
        imgurl: "img004",
        text: 'Product 004',
        seo: '',
        medals: [{
            goldmedal: false,
            sellermedal: false,
            newshop: true
            }]
    }]
};

$(function () {
    const getProductList = function (productItem) {
        const productListRender = $('<div>', { class: 'feature_ico'}).append($('<ul>', {
            id: plSettings.sellerMetal,
            class: 'icons'
        }));

        //Condition is here i have tried
        if (productItem.medals) {
            $.each(productItem.medals, function (index, data) {
                if (this.goldmedal === true) {
                    const medalRender = $(plSettings.sellerMetal);
                    const metalItem = $('<li>', {
                        class: 'icon gold'
                    }).append($('img'), {
                        src: ''
                    });
                    medalRender.append(metalItem);
                } else if (this.silvermedal === true) {
                    const medalRender = $(plSettings.sellerMetal);
                    const metalItem = $('<li>', {
                        class: 'icon gold'
                    }).append($('img'), {
                        src: ''
                    });
                    medalRender.append(metalItem);
                    }
                });
            }
            return productListRender;
        };

    const $product = $('#List')
    $.each(data.productList, function (index, data) {
        $product.append(getProductList(data));
    });
});

//Below is the Setting JS
var plSettings = $.extend({
    sellerMetal: '#' + 'sellerMetal',
    goldMetalSrc: '/img/tps/gold.png'
});
.feature_ico {
    border: 1px solid #e0e0e0
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="List"></div>

【问题讨论】:

    标签: javascript html jquery web dynamic


    【解决方案1】:

    喜欢这样吗?

    //Below is the Setting JS
    var plSettings = $.extend({
        goldmedal: '/img/tps/gold.png',
        sellermedal: '/img/tps/seller.png',
        newshop: '/img/tps/newshop.png'
    });
    
    //Below is the Data JS
    var data = { productList: [
        {
            id: "e0c18c12-0b51-41ad-9f2c-aefc20cafcdb",
            link: "1",
            imgurl: "img001",
            text: 'Product 001',
            seo: '',
            medals: {
                goldmedal: true,
                sellermedal: false,
                newshop: true
                }
        },
        {
            id: "f3bee385-76d7-4a87-8bba-a51d33238858",
            link: "2",
            imgurl: "img002",
            text: 'Product 002',
            seo: '',
            medals: {
                goldmedal: true,
                sellermedal: true,
                newshop: true
                }
        },
        {
            id: "fc3dd6a9-5d8c-4262-aa65-a680e0f0cafe",
            link: "3",
            imgurl: "img003",
            text: 'Product 003',
            seo: '',
            medals: {
                goldmedal: false,
                sellermedal: false,
                newshop: true
                }
        },
        {
            id: "8615711e-8544-4484-933f-b14a93941b86",
            link: "4",
            imgurl: "img004",
            text: 'Product 004',
            seo: '',
            medals: {
                goldmedal: false,
                sellermedal: false,
                newshop: true
                }
        }]
    };
    
    function createFeatureIco() {
      var el = document.createElement('div');
      el.classList.add('feature_ico');
      
      var ul = document.createElement('ul');
      ul.id = '#sellerMetal' // # ?
      ul.classList.add('icons');
      
      el.append(ul);
      
      return [el, ul]
    }
    
    function createMedal(src) {
        var li = document.createElement('li');
        var img = document.createElement('img');
        img.src = src
        li.append(img);
        return li
    }
    
    $(function () {
      const list = document.getElementById('List');
      
      data.productList.forEach(({ medals }) => {
        const [el, ul] = createFeatureIco();
        const { goldmedal, sellermedal, newshop } = medals;
        if(medals) {
          // conditions
          if(goldmedal) { ul.append(createMedal(plSettings.goldmedal))}
          if(sellermedal) { ul.append(createMedal(plSettings.sellermedal))}
          if(newshop) { ul.append(createMedal(plSettings.newshop))}
        }
        list.append(el);
      })
    
    
    });
    .feature_ico {
        border: 1px solid #e0e0e0
    }
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <div id="List"></div>

    【讨论】:

    • 嗯,我有一个循环列表,我只想根据条件将奖牌 li 附加到 ul。
    【解决方案2】:

    如果要附加所有真实条件标记,请将 else if 替换为 if。我保留了控制台日志以检查条件是否正确。

    //Below is the Data JS
    var data = {
      productList: [
        {
          id: "e0c18c12-0b51-41ad-9f2c-aefc20cafcdb",
          link: "1",
          imgurl: "img001",
          text: 'Product 001',
          seo: '',
          medals: [{
            goldmedal: true,
            sellermedal: false,
            newshop: true
          }]
        },
        {
          id: "f3bee385-76d7-4a87-8bba-a51d33238858",
          link: "2",
          imgurl: "img002",
          text: 'Product 002',
          seo: '',
          medals: [{
            goldmedal: true,
            sellermedal: true,
            newshop: true
          }]
        },
        {
          id: "fc3dd6a9-5d8c-4262-aa65-a680e0f0cafe",
          link: "3",
          imgurl: "img003",
          text: 'Product 003',
          seo: '',
          medals: [{
            goldmedal: false,
            sellermedal: false,
            newshop: true
          }]
        },
        {
          id: "8615711e-8544-4484-933f-b14a93941b86",
          link: "4",
          imgurl: "img004",
          text: 'Product 004',
          seo: '',
          medals: [{
            goldmedal: false,
            sellermedal: false,
            newshop: true
          }]
        }]
    };
    
    $(function () {
      const getProductList = function (productItem) {
        const productListRender = $('<div>', { class: 'feature_ico' }).append($('<ul>', {
          id: plSettings.sellerMetal,
          class: 'icons'
        }));
    
        //Condition is here i have tried
        if (productItem.medals) {
          $.each(productItem.medals, function (index, data) {
            if (this.goldmedal === true) {
              console.log('goldmedal', this.goldmedal)
              const medalRender = $(plSettings.sellerMetal);
              const metalItem = $('<li>', {
                class: 'icon gold'
              }).append($('img'), {
                src: ''
              });
              medalRender.append(metalItem);
            } 
            if (this.silvermedal === true) {
              console.log('silvermedal', this.silvermedal)
              const medalRender = $(plSettings.sellerMetal);
              const metalItem = $('<li>', {
                class: 'icon gold'
              }).append($('img'), {
                src: ''
              });
              medalRender.append(metalItem);
            } 
            if (this.newshop === true) {
              console.log('newshop', this.newshop)  // condition
              const medalRender = $(plSettings.sellerMetal);
              const metalItem = $('<li>', {
                class: 'icon gold'
              }).append($('img'), {
                src: ''
              });
              medalRender.append(metalItem);
            }
          });
        }
        return productListRender;
      };
    
      const $product = $('#List')
      $.each(data.productList, function (index, data) {
        $product.append(getProductList(data));
      });
    });
    
    //Below is the Setting JS
    var plSettings = $.extend({
      sellerMetal: '#' + 'sellerMetal',
      goldMetalSrc: '/img/tps/gold.png'
    });
    .feature_ico {
        border: 1px solid #e0e0e0
    }
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <div id="List"></div>

    【讨论】:

    • 嗨,它附加到所有列表而不是基于类别。
    猜你喜欢
    • 2018-03-02
    • 2022-12-11
    • 2020-02-13
    • 1970-01-01
    • 2020-04-04
    • 2012-04-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多