动态填充表时突出显示和排序问题
Highlight and sorting issue when populating table dynamically
我试图让我的表格每行都有一个复选框列,以及悬停时要突出显示的行。当数据在 html 文件上静态声明时,它可以正常工作,但是当从服务器检索数据时(我使用的是 $.getJSON),排序变得一团糟,突出显示停止工作。
此外,它还为表中的每一行显示此消息。
数据表警告:从数据中请求未知参数"5" 第 0 行的来源
这是我的代码:
$(function ()
{
var oTable;
var tRow;
var checkboxIdsArray = new Array();
var allChecked = false;
// To generate the checkbox for each row
var nCloneTh = document.createElement('th');
var nCloneTd = document.createElement('td');
nCloneTd.innerHTML = '<input type="checkbox" id="op_checkbox" />';
nCloneTd.className = "center";
// Deal with the checbox selection
$('#op_checkbox').live('click', function()
{
var operatorId = $(this).parents('tr').attr('id');
});
$('#example thead tr').each(function ()
{
this.insertBefore(nCloneTh, this.childNodes[0]); // Add the header before the first header
});
// Instantiate the DataTable
oTable = $('#example').dataTable({"aaSorting": [[ 0, "asc" ]]});
$.getJSON('../../controller/UserController.php/getUsers',
function(data)
{
$.each(data, function(i, item)
{
oTable.fnAddData(
[
item.idUser,
item.nameUser,
item.telephoneUser,
item.cnpjUser,
item.inscEstUser
]
);
});
$('#example tbody tr').each(function ()
{
this.insertBefore(nCloneTd.cloneNode(true), this.childNodes[0]); // Add the checkbox to the td's
});
});
// Deals with the highlight of the rows
$('#example tbody tr').hover(function()
{
tRow = this;
$(this).children().addClass('highlighted');
},
function()
{
var nTrs = oTable.fnGetNodes();
$(tRow).children().removeClass('highlighted');
}
);
// Deals with the export options
var oTableTools = new TableTools( oTable,
{
"aButtons":
[
{
"sExtends": "div",
"sButtonText": "Hello world"
}
]
});
$('#demo').before( oTableTools.dom.container );
// Deals with the check all button click
$('#checkall_link').live('click', function()
{
var i = 0;
if(!allChecked)
{
$(oTable.fnGetNodes()).each(function()
{
allChecked = true;
$('#checkall_link').text('Uncheck all');
this.childNodes[0].childNodes[0].checked = true; // Set all checkbox to checked
checkboxIdsArray[i] = this.childNodes[0].childNodes[0].id; // Store the current checkbox id the checkboxIds array
i++;
});
}
else
{
$(oTable.fnGetNodes()).each(function()
{
allChecked = false;
$('#checkall_link').text('Check all');
this.childNodes[0].childNodes[0].checked = false; // Set all checkbox to checked
checkboxIdsArray = [];
console.log(checkboxIdsArray);
});
}
});
$('#manage_del').click(function()
{
if($(this).attr('class') == 'disabled')
{
alert("disabled");
}
else
{
alert("enabled");
}
});
$('#manage_new').click(function()
{
if($(this).attr('class') == 'disabled')
{
alert("disabled");
}
else
{
alert("enabled");
}
});
});
这是我的桌子的样子。https://i.stack.imgur.com/rGm0F.jpg
正如您在右侧的箭头中看到的,它会创建另一列(可能是因为添加了复选框),还可以在左侧箭头中看到第二列突出显示,但选中的标题是第一列(带有复选框)。当我将鼠标悬停在行上时,它不会突出显示。
任何帮助将不胜感激。谢谢。
更新
现在使用 delegate(),但它还没有工作。
// Deals with the highlight of the rows
$('#example tbody').delegate('tr', 'hover', function()
{
tRow = this;
$(this).children().addClass('highlighted');
},
function()
{
var nTrs = oTable.fnGetNodes();
$(tRow).children().removeClass('highlighted');
});
我会亲自使用委托(主要是因为我从来没有让我的悬停示例为您处理动态内容)
下面是一些示例代码,向您展示如何使用委托:
缩略图列表的示例设置:
<ul>
<li>
<img src="http://www.dummyimage.com/64x64/000/fff" />
<p>some title text</p>
</li>
</ul>
// attach the handler via delegate()
$(document).delegate("li", "hover", function() { // this works because the delegate function looks for all li's that are children to the document.
$(this).children('p').fadeToggle("fast");
});
// after you've attached the handler create some elements.
setTimeout(function() {
var list = $('ul'),
node = list.children('li'),
i = 25;
while (i) {
list.append(node.clone());
i--;
}
}, 1000);
现场演示
相关文章:
- Datatables-如何在未显示所有列的情况下禁用表中最后一列的排序
- localeCompare 显示使用前导变音字符对单词进行排序时不一致的行为
- 在IOS(Phonegap)中按姓名对联系人进行排序(姓名显示为中文和英文)
- 数组排序后显示更改
- 将列设置为在EasyUI数据网格上按排序显示
- 如何对select2自动完成结果进行排序,只显示以第一个字母开头的项目
- 动态生成<选择>未显示已排序列表中的第一个项目
- (钛工作室)显示带有排序数据的表视图
- 动态填充表时突出显示和排序问题
- 如何在Meteor中使用两个按时间排序的不同模板显示两个不同的集合
- 在使用可排序排序期间/之后显示使用 JSON 创建的列表中的项目位置
- NodeJS时间戳:比较时间并显示记录排序升序
- 按活动排序,但不在结果中显示活动
- 脚 - 排序指示器未正确显示
- 表排序器寻呼机插件 - 默认显示所有条目
- 显示排序的火基数组的特殊情况(使用 AngularJS)
- jqGrid 显示排序后Col/hideCol不起作用
- 检索两个列表,对值进行排序和比较,然后显示所有结果
- Meteor,按布尔值显示/排序值
- 如何显示排序只有25个元素在无限滚动