【问题标题】:How create a collapse table with jQuery Ajax response如何使用 jQuery Ajax 响应创建折叠表
【发布时间】:2020-02-05 05:39:27
【问题描述】:

我正在尝试创建一个网站,我可以在其中搜索最便宜的供应商的特定产品。我可以使用 Elastichsearch 从数据库中获取数据,并使用 jQuery Ajax 获取数据库。

<script>
    $('.js-ajax').select2({
        ajax: {
            url: '/product/api/elasticsearch',
            dataType: 'json',
            width: 'resolve', // need to override the changed default
            minimumResultsForSearch: -1,
            dropdownCssClass: 'select2-hidden',

            success: function (data) {
                var returnedData = data;
                // clear table
                $('#products tbody').empty();

                for(let i = 0; i < returnedData.results.length; i++){
                    $("#products").find('tbody')
                        .append($('<tr>')
                            .append($('<td>')
                                .text(returnedData.results[i].id)
                            )

                            .append($('<td class="columt">')
                                .text(returnedData.results[i].text)
                            )

                            .mouseover(returnedData.results[1].text)

                            .append($('<td class="columnp">')
                                .text(returnedData.results[i].sku)
                            )
                            .append($('<td class="colum7">')
                                .text(returnedData.results[i].vendor)
                            )
                        );
                }
            }
        }
    });
</script>

我可以将响应放入表格中,但如何将响应放入折叠表格中?我认为我必须创建一个 foreach 循环并标记每个产品。

我试过了,但是没用

function addProducts(products) {
  products.forEach(fucntion(product) {
                   var template = $('#products').text();//get template
                    template = template.replace('{id}', product.id);//replace placeholders to data
                    template = template.replace('{title}', product.title);
                    template = template.replace('{price}', product.id);
                    //...other data
                    $('#search-result').append(template);//add to box
                    bindProduct(product.id);//add event  
                   });
}

function bindProduct(productID) {
  $('#'+productID).on('mouseover', function() {//or another event
    showPopup();//justs add opened class or what you want
  });
}

上面的代码有语法错误,我找不到。

第一个想法是通过鼠标悬停将数据放入表格中。但是由于这在小屏幕上无法按预期工作,我决定将数据置于折叠状态,但我被卡住了

【问题讨论】:

    标签: jquery ajax bootstrap-modal collapse


    【解决方案1】:

    你把函数拼错了。 试试:

    function addProducts(products) {
      products.forEach(function(product) { //here was the "fuction" misspell
                       var template = $('#products').text();//get template
                        template = template.replace('{id}', product.id);//replace placeholders to data
                        template = template.replace('{title}', product.title);
                        template = template.replace('{price}', product.id);
                        //...other data
                        $('#search-result').append(template);//add to box
                        bindProduct(product.id);//add event  
                       });
    }
    
    function bindProduct(productID) {
      $('#'+productID).on('mouseover', function() {//or another event
        showPopup();//justs add opened class or what you want
      });
    }
    

    【讨论】:

    • 感谢更新,是否也用这段代码sn-p将鼠标悬停事件替换为折叠?
    猜你喜欢
    • 2023-03-17
    • 2021-02-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-01
    • 2023-03-14
    • 2020-01-22
    • 2018-08-12
    相关资源
    最近更新 更多