例如,
const output = document.getElementById('output');
function delete_item(id){ //change in function
console.log("ID " + id);
}
let productsArray = [];
$.getJSON('/products', products => {
let outputContent = '';
productsArray = Object.values(products);
productsArray.forEach(product => {
outputContent += `<div data-id="${ product.ID }">
<div id='edit'><i class='fa fa-pencil-square-o' aria-hidden='true'></i></div>
<div class='delete'><i class='fa fa-trash-o' aria-hidden='true'></i></div>
<div id='product'> <span class='name'> <a href=''>${product.NAME}</a></span>
<span class='price'>Price: £${product.PRICE}</span>
</div>`;
}); // changed id to class
output.innerHTML = outputContent;
});
$(document).on('click','.delete',function(){ // put click functionality outside
delete_item($(this).parent().attr('data-id')); // pass data-id to function
});
如何在每两列后插入一个空白列?
答案 0 :(得分:3)
这是一种类似的方法,它使用矩阵和整数选择列的技巧。原始data.frame获取带有cbind
的NA列。然后使用每两列引用此新对象的列,然后使用矩阵引用最终的NA列,使用rbind
填充最终列。
cbind(dataX, NewCol=NA)[c(rbind(matrix(seq_along(dataX), 2), ncol(dataX)+1))]
a b NewCol c d NewCol.1 e f NewCol.2
1 1 2 NA 3 4 NA 5 6 NA
2 2 3 NA 4 5 NA 6 7 NA
3 3 4 NA 5 6 NA 7 8 NA
4 4 5 NA 6 7 NA 8 9 NA
5 5 6 NA 7 8 NA 9 10 NA
答案 1 :(得分:2)
我们可以使用split
将独特位置的数据集拆分为list
data.frame
,并list
,cbind
与{{}一起循环1}}和NA
元素在一起
cbind
答案 2 :(得分:0)
让我们构造一个空数据框,其行数与dataX
相同empty_df <- data.frame(x1=rep(NA,nrow(df)),x2=rep(NA,nrow(df)),x3=rep(NA,nrow(df)))
dataX<-cbind(dataX,empty_df)
dataX<-dataX[c("a","b","x1","c","d","x2","e","f","x3")]
导致:
a b x1 c d x2 e f x3
1 1 2 NA 3 4 NA 5 6 NA
2 2 3 NA 4 5 NA 6 7 NA
3 3 4 NA 5 6 NA 7 8 NA
4 4 5 NA 6 7 NA 8 9 NA
5 5 6 NA 7 8 NA 9 10 NA