【发布时间】: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