Я использую Datatables и пытаюсь получить данные строки, используя row (). Data () или row (). 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