Javascript 如何获取数据表列的名称?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32598279/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get name of datatable column?
提问by Anna T
I'm using DataTable 1.10.9 (from https://datatables.net). Columns for the datatable are defined at the initialization step in javascript and each column has a unique name, e.g.:
我正在使用 DataTable 1.10.9(来自https://datatables.net)。数据表的列是在 javascript 初始化步骤中定义的,每列都有一个唯一的名称,例如:
var table = $('#example').DataTable({
columns: [
{ name: 'first-name' },
{ name: 'last-name' },
{ name: 'position' },
{ name: 'location' },
{ name: 'salary' }
]
});
I know I can get the column from the table by the name I've given it, but I can't seem to find how to find a column's name from the column object. (I'm hoping it's possible with the current state of the component.) For example:
我知道我可以通过给定的名称从表中获取列,但我似乎无法找到如何从列对象中找到列的名称。(我希望组件的当前状态是可能的。)例如:
table.columns().every(function() {
//I'd want to see what the name of this column is, something like:
console.log(this.name()); //will throw an exception since no such function exists
//or
console.log(this.name); //will output 'undefined'
});
What's the proper function or property to get the name there?
在那里获得名称的正确功能或属性是什么?
回答by davidkonrad
You can retrieve the initialization options through table.settings().init()
- and by that the columns
definition this way :
您可以通过table.settings().init()
- 并通过columns
这种方式检索初始化选项:
var columns = table.settings().init().columns;
When clicking on a cell / <td>
you can find the column index by (best practice in case of hidden columns) :
单击单元格时/<td>
您可以通过以下方式找到列索引(隐藏列的最佳做法):
var colIndex = table.cell(this).index().column;
An example of a click handler that alerts the corresponding column.name
when a cell is clicked
单击column.name
单元格时发出警报的单击处理程序示例
$("#example").on('click', 'td', function() {
//get the initialization options
var columns = table.settings().init().columns;
//get the index of the clicked cell
var colIndex = table.cell(this).index().column;
alert('you clicked on the column with the name '+columns[colIndex].name);
})
Your every()
example would be
你的every()
例子是
var columns = table.settings().init().columns;
table.columns().every(function(index) {
console.log(columns[index].name);
})
demo -> http://jsfiddle.net/6fstLmb6/
回答by James Jithin
The following should work for you as per the API document:
var tableColumns = [{
name: 'first-name'
}, {
name: 'last-name'
}, {
name: 'position'
}, {
name: 'location'
}, {
name: 'salary'
}]
var table = $('#example').DataTable({
tableColumns
});
table.columns().every(function (index) {
console.log(tableColumns[index].name);
});
回答by Ozan
Documentationshows that columns().every()
sets this
to the current column's column()
object. Further examination of the column object shows that it has a column().data()
method which returns the data the column contains as an array of values. This should provide the data you need.
文档显示columns().every()
设置this
为当前列的column()
对象。对列对象的进一步检查表明,它有一个column().data()
方法可以将列包含的数据作为值数组返回。这应该提供您需要的数据。