有什么方法可以在现有的rhsonsontable函数中添加此javascript-http://jsfiddle.net/zpLm7f2k/吗?我知道有办法做到。请参阅下面的程序。但是我无法添加http://jsfiddle.net/zpLm7f2k/
中提到的javascriptlibrary(rhandsontable)
# create some quick data to test and append a row for totals
dat <- rbind(
head(mtcars,10),
# sum doesn't really make sense for the total but use
# in this example as an aggregate function
lapply(head(mtcars,10), sum)
)
rownames(dat)[11] <- "Totals"
rht <- rhandsontable(dat) %>%
# make totals row readOnly to prevent user from overwriting
hot_row(11, readOnly = TRUE)
htmlwidgets::onRender(
rht,
"
function(el, x) {
var hot = this.hot
hot.addHook(
'afterChange',
function(changes, source) {
if(source === 'edit') {
//debugger
changes.forEach(function(change) {
var sum = hot.getData(0,change[1],9,change[1]).map(
function(d){ return parseFloat(d[0]) || 0 }
).reduce(
// simple reduce to calculate sum
function(left,right) {
return left + right
},
0
)
hot.setDataAtCell(10, change[1], sum, 'calculate') // make sure to specify 'calculate' so no infinte loop
})
}
}
)
}
"
)