我正在使用数据表,并尝试使用row()。data()或rows()。data()获得行数据。问题是该行未定义。控制台显示行未定义,这就是为什么我无法获取数据的原因。
数据是从本地json文件加载的。请参阅下面的代码。我已经在网络上尝试了所有解决方案,但仍然无法获取数据。我在这里想念东西吗?
$(document).ready(function() {
var oTable = $('#example').DataTable();
function reloader(val) {
$('#example').DataTable( {
"destroy" : true,
"ajax": val + ".json",
"columns": [
{ "data": "selected" },
{ "data": "audienceName" },
{ "data": "audienceId" },
{ "data": "uniques" }
],
columnDefs: [ {
orderable: false,
//className: 'select-checkbox',
targets: 0,
'checkboxes': {
'selectRow': true
}
} ],
select: {
style: 'multi'
},
order: [[ 1, 'asc' ]]
} );
};
$('#example tbody').on( 'click', 'tr', function () {
$(this).toggleClass('selected');
console.log(this); // prints the row element
var data = oTable.row( this ).data();
alert(data) // undefined
//var row = oTable.row(pos).data();
console.log(data['uniques']); // error
} );
$('select').on('change', function() {
//alert( this.value );
reloader(this.value);
});
} );
和JSON
{
"data": [
{
"selected" : "",
"audienceName":"Automotive",
"audienceId":"111111",
"uniques": "888"
},
{
"selected" : "",
"audienceName":"Automotive_Hybrid",
"audienceId":"1111",
"uniques": "888"
}
]
}
console.log(oTable)返回下面,但是alert(oTable)返回[object Object]
_Api {context: Array(1), selector: {…}, tables: ƒ, table: ƒ, draw: ƒ, …}
$: ƒ ()
ajax: {__dt_wrapper: true, json: ƒ, params: ƒ, reload: ƒ, url: ƒ}
cell: ƒ ()
cells: ƒ ()
checkboxes: ƒ ()
clear: ƒ ()
column: ƒ ()
columns: ƒ ()
context: [{…}]
data: ƒ ()
destroy: ƒ ()
draw: ƒ ()
i18n: ƒ ()
init: ƒ ()
off: ƒ ()
on: ƒ ()
__dt_wrapper: true
arguments: (...)
caller: (...)
length: 0
name: ""
prototype: {constructor: ƒ}
__proto__: ƒ ()
[[FunctionLocation]]: jquery.dataTables.js:7194
[[Scopes]]: Scopes[3]
one: ƒ ()
order: ƒ ()
page: ƒ ()
row: ƒ ()
rows: ƒ ()
search: ƒ ()
select: ƒ ()
selector: {rows: null, cols: null, opts: null}
settings: ƒ ()
state: ƒ ()
table: ƒ ()
tables: ƒ ()
__proto__: Object(0)
any: ƒ ()
concat: ƒ concat()
context: []
count: ƒ ()
each: ƒ ( fn )
eq: ƒ ( idx )
filter: ƒ ( fn )
flatten: ƒ ()
indexOf: ƒ indexOf()
iterator: ƒ ( flatten, type, fn, alwaysNew )
join: ƒ join()
lastIndexOf: ƒ lastIndexOf()
length: 0
map: ƒ ( fn )
pluck: ƒ ( prop )
pop: ƒ pop()
push: ƒ push()
reduce: ƒ reduce()
reduceRight: ƒ reduceRight()
reverse: ƒ reverse()
selector: null
shift: ƒ shift()
slice: ƒ ()
sort: ƒ sort()
splice: ƒ splice()
to$: ƒ ()
toArray: ƒ ()
toJQuery: ƒ ()
unique: ƒ ()
unshift: ƒ unshift()
constructor: ƒ ( context, data )
__proto__: Object