从HTML表中选择行的Highchart
Highchart from select rows in HTML table
我有一个HTML表,其中包含动态生成的值。这是页面加载时的样子:http://jsfiddle.net/HE8Vj/
HTML表的第一列包含复选框。我希望从HTML表生成的饼图仅包含HTML表上其复选框已被选中的行。
我使用Highcharts库制作饼状图。
我已经设法让代码部分工作。当选中(1)或(1、2)或(1、2、3)或(1、2、3)中的复选框时,饼图生成正常
但是当正确的顺序被改变时,比如当跳过一行时,例如:行(2)或(2,3,4)或(1,3,4)或(4),控制台弹出一个错误,并且不会生成饼状图。
是 JSFIDDLE
这件事已经困扰我很久了。
小代码段:
if ($('#check'+i).is(':checked'))
{
console.log(i);
options.series[0].data[i-1].push(this.innerHTML);
}
谢谢!
编辑:这是整个文件(不需要额外的文件来运行它)- http://pastebin.com/nhr4Q0fm
好吧,你有点小题大做了所以现在是这样的:
// Here we iterate on each of the returned selectors (checked inputs)
$.each($(':checked'), function (index, item) {
// item is the checked input
//First we get a jQuery reference to the 'item', then we chain the 'parents' method with an argument of 'tr' meaning: get all parents of this 'item' which are a 'tr' element. Then we get only the first returned on using 'first()' method, then get that returned elements 'children' who are 'td'.
var tds = $(item).parents('tr').first().children('td');
// Since we now should have a collection/array of all 'td' in the 'tr' of the ':checked' input we can iterate through the array. Since tds[0] would be the first one in the row, that is where the checked input lives. Thus tds[1] and tds[2] are 'name' and 'project' respectively.
var name = $(tds[1]).html();
var projects = parseFloat($(tds[2]).html());
// Now we just push the two values as an array
options.series[0].data.push([name, projects]);
});
演示:http://jsfiddle.net/robschmuecker/W5asQ/2/
我相信这是因为只有在这里检查输入时才会将数据输入到数组中:
if (j == 1) {
if ($('#check'+i).is(':checked'))
{
console.log(i);
options.series[0].data[i-1].push(this.innerHTML);
//alert('selected label: ' + this.innerHTML);
};
}
这意味着每当$('#check'+i)
未选中时,您将数组中的.data[i-1]
保留为undefined
,即Highcharts。图表库不理解。在这些情况下,您应该添加一个空白、零或" "值:
if (j == 1) {
if ($('#check'+i).is(':checked'))
{
console.log(i);
options.series[0].data[i-1].push(this.innerHTML);
//alert('selected label: ' + this.innerHTML);
}else{
options.series[0].data[i-1].push(" ");
}
}
确保在j == 2
时也输入值为0,否则您将遇到相同的错误。
相关文章:
- 下拉选择可自动更改第二个下拉选择
- 如何使用jQuery选择下拉列表的值
- jquery点击函数select&取消选择
- Windows8应用程序(html&Javascript):从图片库(除了文件选择器)显示图像的另一种方式
- 按照选项卡索引的顺序循环一个jQuery选择
- 在动态创建的元素上获取对特定选择器的引用
- AngularJS-在JSON选择器中使用变量名
- 每当您在选择器内移动鼠标时,悬停功能就会重复
- 获取选择框的状态
- d3基于用户选择动态更新节点
- 提交后保留下拉选择的值
- JQuery对动态创建的对象进行选择
- 无法在Ionic select中预先选择最后一个选项
- 使用CSS或js,使用动态选择器选择任意li的下一个元素
- jQuery自动完成阻止选择后聚焦
- 使用此选项选择父类内部的类
- 当我选择 30 天图表时,我的 Highchart 图表真的很慢
- highchart mysql-json,带有where条件的下拉选择值
- Highchart's选择函数延迟更新
- 从HTML表中选择行的Highchart