gpt4 book ai didi

javascript - Datatable.net 数据indexOf 与数据不匹配

转载 作者:行者123 更新时间:2023-11-28 04:11:31 26 4
gpt4 key购买 nike

我正在尝试查找与列数据值匹配的行的索引。我想从找到的索引中获取相应的 TR 元素。这是我的做法:

// A table with many people [name, position, office, age, etc...]
var table = $('#example').DataTable();

// Finding the row index of the person having 46 year old
var pos = table.column(3).data().indexOf("46");

// Getting position 7 (Cara Stevens)
console.log(`pos : ${pos}`);

// Getting data of the row at index 7
var dt = table.row(pos).data();

// We are getting Rhona Davidson instead of Cara Stevens, what's that ?
// Even if I try to get the TR html element by calling table.row(pos).node(),
// obviously this will not be the TR having (Cara Stevens) but the one
// with (Rhona Davidson)
console.log(`dt : ${dt}`);

我是否错过了有关 Datatable.net 和内部索引行为的某些内容,或者有什么背景?

这是问题的完整工作示例,请阅读控制台结果: https://jsfiddle.net/fzvsvfs2/

最诚挚的问候,

最佳答案

为了获得正确的行,您应该使用:

var dt = table.rows().data()[pos];

因为row(pos)是从原始行模型中选择索引pos的行,而不是显示顺序,您可以使用console.log(table .data()).

要获取 tr,只需:

table.rows().nodes()[pos]

关于javascript - Datatable.net 数据indexOf 与数据不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46230782/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com