DataTables warning requested unknown parameter

本文解析了DataTable在请求数据时出现的警告信息,详细介绍了错误信息中各部分的意义,并提供了诊断及解决方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

This is possibly the most cryptic warning message that DataTables will show. It is a short error message as it needs to cope with all of the data source options that DataTables has, but flexible enough to convey information for each of these cases, hence why it can appear a little cryptic on first inspection. However, when we break it down into its component parts, it is actually relatively straight forward, as described below.

Meaning

Each cell in DataTables requests data, and when DataTables tries to obtain data for a cell and is unable to do so, it will trigger a warning, telling you that data is not available where it was expected to be. The warning message is:

DataTables warning: table id={id} - Requested unknown parameter '{parameter}' for row{row-index}, column{column-index}`

where:

  • {id} is replaced with the DOM id of the table that has triggered the error
  • {parameter} is the name of the data parameter DataTables is requesting
  • {row-index} is the DataTables internal row index (row().index()) for the row that has triggered the error.
  • {column-index} is the column data index (column().index()) for the column that has triggered the error. The column index information was added in DataTables 1.10.10.

So to break it down, DataTables has requested data for a given row, of the {parameter} provided and there is no data there, or it is null or undefined (DataTables doesn't know, by default how to display these parameters - see below if your data does contain these values).

 

 

Diagnosis

The key part to understanding this error message is the {parameter} aspect of it. It will be in one of three forms:

  • Integer
  • String
  • Function

Parameter is an integer

When {parameter} is an integer, DataTables is looking for data from an array. This is usually the case when using DOM sourced data (i.e. the data for the table is read automatically from the document). In this circumstance the requested data doesn't exist in source array - likely because the array isn't long enough. This can occur if:

  • There is a colspan or rowspan in the tbody of the table, which is not supported by DataTables.
  • Using columns or columnDefs you have specified more columns than there are in the HTML
  • The number of cells in the table does not satisfy the equation #cells = #columns * #rows(i.e. there are more columns defined in the header than in the table body, or vice-versa).

Parameter is a string

If {parameter} is shown as a string, this will typically indicate that you have used columns.dataor columns.render to pull data out of an object, be it Ajax loaded or from the client-side. For example the error message in read: Requested unknown parameter 'Name' for row 0. This will indicate that a column which uses columns.data has been unable to obtain valid data to display - for example:

1
{ data:  'Name'  }

would produce this error if the data source object for the row had no Name parameter or the data wasnull or undefined.

As a result, this error usually occurs if:

  • The data property specified doesn't exist (a typo or a gap in data)
  • The value of the property specified is null

Parameter is a function

If {parameter} simply says {function} it means that columns.data or columns.renderhave been given as a function, but the function has returned null or undefined. For example, the following would trigger such an error:

1
2
3
4
5
{ data:  function  ( row, type, set ) {
     if  ( type ===  'display'  ) {
         return  row.Name;
     }
} );

Note how the return statement is only used when type === 'display'. As a result, if type is notdisplay then undefined is the return value from the function and DataTables will throw a warning.

Resolution

The key to resolving this error, is to ensure that DataTables has all of the data that is required. Specifically, check the following:

  • colspan and rowspan have not been used in the tbody of the table.
  • The equation #cells = #columns * #rows is satisfied.
  • If using columns ensure that you have specified exactly the number of columns that are present in the HTML for the table.
  • Also if using `dt-init columns, remove any trailing commas you might have at the end of the array. The additional comma can cause issues for older browsers.
  • If using dt-init columnDefs` ensure that you have not specified more columns than there are in the HTML
  • If using columns.render or columns.data ensure that they are returning a value (no return in Javascript is the same as returning undefined which will result in this error).
  • Ensure that the server-side script is completing its execution successfully. Long running scripts can time out for example. The server error logs will give an indication if this is the case.

null or undefined data

null and undefined values in a data source absolutely are valid and very often useful. DataTables warns about the use of them by default, as the majority of the time they are not intended for display - for example, rather than showing null in the table, you might want to show Not yet set, or simply an empty string (empty cell). For this, DataTables has a columns.defaultContent option.

With columns.defaultContent set, any null or undefined value will be replaced with the value specified. No warning will be shown in this circumstances.

转载于:https://www.cnblogs.com/sdream/p/5378078.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值