Jquery datatable ширина колонки

How to set column widths to a jQuery datatable?

I have a jQuery datatable(outlined in red), but what happens is that the table jumps out of the width I have set for the div(which is 650 px).

Here is the screen shot:

Here is my code:

7 Answers 7

I found this on 456 Bera St. Man is it a lifesaver.

But — you don’t have a lot of room to spare with your data.

Specify the css for the table:

It works for me!

Try setting the width on the table itself:

You’ll have to adjust the 650 by a couple pixels to account for whatever padding, margins, and borders you have.

You’ll probably still have some issues though. I don’t see enough horizontal space for all those columns without mangling the headers, reducing the font sizes, or some other bit of ugliness.

The best solution I found this to work for me guys after trying all the other solutions. Basically i set the sScrollX to 200% then set the individual column widths to the required % that I wanted. The more columns that you have and the more space that you require then you need to raise the sScrollX %. The null means that I want those columns to retain the datatables auto width they have set in their code.

Источник

DataTables

Columns

Although DataTables can obtain information about the table directly from the DOM, you may wish to give DataTables specific instructions for each individual column. This can be done using either the aoColumnDefs parameter, or aoColumns and the object information given for each column.

The aoColumnDefs parameter and aoColumns achieve the same aim, but differ in how they work:

  • aoColumnDefs: This array allows you to target a specific column, multiple columns, or all columns, using the aTargets property of each object in the array (please note that aoColumnDefs was introduced in DataTables 1.7). This allows great flexibility when creating tables, as the aoColumnDefs arrays can be of any length, targeting the columns you specifically want. The aTargets property is an array to target one of many columns and each element in it can be:
    • a string — class name will be matched on the TH for the column
    • 0 or a positive integer — column index counting from the left
    • a negative integer — column index counting from the right
    • the string «_all» — all columns (i.e. assign a default)
  • aoColumns: If specified, then the length of this array must be equal to the number of columns in the original HTML table. Use ‘null’ where you wish to use only the default values and automatically detected options.

Both aoColumnDefs parameter and aoColumns can be used together, although aoColumnDefs is preferred due to it’s flexibility. If both are used, aoColumns definitions will take the highest priority. Likewise, if the same column is targeted multiple times in aoColumnDefs, the first elements in the array will take the highest priority, and the last the lowest.

Allows a column’s sorting to take multiple columns into account when doing a sort. For example first name / last name columns make sense to do a multi-column sort over the two columns.

aDataSort
Show details
Default: null Takes the value of the column index automatically
Type: array
Code example:

You can control the default sorting direction, and even alter the behaviour of the sort handler (i.e. only allow ascending sorting etc) using this parameter.

asSorting
Show details
Default: [ ‘asc’, ‘desc’ ]
Type: array
Code example:

Enable or disable filtering on the data in this column.

bSearchable
Show details
Default: true
Type: boolean
Code example:

Enable or disable sorting on this column.

bSortable
Show details
Default: true
Type: boolean
Code example:

Deprecated When using fnRender() for a column, you may wish to use the original data (before rendering) for sorting and filtering (the default is to used the rendered data that the user can see). This may be useful for dates etc.

Please note that this option has now been deprecated and will be removed in the next version of DataTables. Please use mRender / mData rather than fnRender.

bUseRendered
Show details
Default: true
Type: boolean
Code example:

Enable or disable the display of this column.

bVisible
Show details
Default: true
Type: boolean
Code example:

Developer definable function that is called whenever a cell is created (Ajax source, etc) or processed for input (DOM source). This can be used as a compliment to mRender allowing you to modify the DOM element (add background colour for example) when the element is available.

fnCreatedCell
Show details
Default:
Type: function
Code example:

Deprecated Custom display function that will be called for the display of each cell in this column.

Please note that this option has now been deprecated and will be removed in the next version of DataTables. Please use mRender / mData rather than fnRender.

fnRender
Show details
Default:
Type: function
Code example:

The column index (starting from 0!) that you wish a sort to be performed upon when this column is selected for sorting. This can be used for sorting on hidden columns for example.

iDataSort
Show details
Default: -1 Use automatically calculated column index
Type: int
Code example:

This property can be used to read data from any JSON data source property, including deeply nested objects / properties. mData can be given in a number of different ways which effect its behaviour:

  • integer — treated as an array index for the data source. This is the default that DataTables uses (incrementally increased for each column).
  • string — read an object property from the data source. Note that you can use Javascript dotted notation to read deep properties / arrays from the data source.
  • null — the sDefaultContent option will be used for the cell (null by default, so you will need to specify the default content you want — typically an empty string). This can be useful on generated columns such as edit / delete action columns.
  • function — the function given will be executed whenever DataTables needs to set or get the data for a cell in the column. The function takes three parameters:
    • The data source for the row
    • The type call data requested — this will be ‘set’ when setting data or ‘filter’, ‘display’, ‘type’, ‘sort’ or undefined when gathering data. Note that when undefined is given for the type DataTables expects to get the raw data for the object back
    • <*>Data to set when the second parameter is ‘set’.

    The return value from the function is not required when ‘set’ is the type of call, but otherwise the return is what will be used for the data requested.

Note that prior to DataTables 1.9.2 mData was called mDataProp. The name change reflects the flexibility of this property and is consistent with the naming of mRender. If ‘mDataProp’ is given, then it will still be used by DataTables, as it automatically maps the old name to the new if required.

mData
Show details
Default: null Use automatically calculated column index
Type: string
Code example:

This property is the rendering partner to mData and it is suggested that when you want to manipulate data for display (including filtering, sorting etc) but not altering the underlying data for the table, use this property. mData can actually do everything this property can and more, but this parameter is easier to use since there is no ‘set’ option. Like mData this can be given in a number of different ways to effect its behaviour, with the addition of supporting array syntax for easy outputting of arrays (including arrays of objects):

  • integer — treated as an array index for the data source. This is the default that DataTables uses (incrementally increased for each column).
  • string — read an object property from the data source. Note that you can use Javascript dotted notation to read deep properties / arrays from the data source and also array brackets to indicate that the data reader should loop over the data source array. When characters are given between the array brackets, these characters are used to join the data source array together. For example: «accounts[, ].name» would result in a comma separated list with the ‘name’ value from the ‘accounts’ array of objects.
  • function — the function given will be executed whenever DataTables needs to set or get the data for a cell in the column. The function takes three parameters:
    • The data source for the row (based on mData)
    • The type call data requested — this will be ‘filter’, ‘display’, ‘type’ or ‘sort’.
    • The full data source for the row (not based on mData)

    The return value from the function is what will be used for the data requested.

mRender
Show details
Default: null Use mData
Type: string
Code example:

Change the cell type created for the column — either TD cells or TH cells. This can be useful as TH cells have semantic meaning in the table body, allowing them to act as a header for a row (you may wish to add scope=’row’ to the TH elements).

sCellType
Show details
Default: td
Type: string
Code example:

Class to give to each cell in this column.

sClass
Show details
Default: Empty string
Type: string
Code example:

When DataTables calculates the column widths to assign to each column, it finds the longest string in each column and then constructs a temporary table and reads the widths from that. The problem with this is that «mmm» is much wider then «iiii», but the latter is a longer string — thus the calculation can go wrong (doing it properly and putting it into an DOM object and measuring that is horribly(!) slow). Thus as a «work around» we provide this option. It will append its value to the text that is found to be the longest string for the column — i.e. padding. Generally you shouldn’t need this, and it is not documented on the general DataTables.net documentation

sContentPadding
Show details
Default: Empty string
Type: string
Code example:

Allows a default value to be given for a column’s data, and will be used whenever a null data source is encountered (this can be because mData is set to null, or because the data source itself is null).

sDefaultContent
Show details
Default: null
Type: string
Code example:

This parameter is only used in DataTables’ server-side processing. It can be exceptionally useful to know what columns are being displayed on the client side, and to map these to database fields. When defined, the names also allow DataTables to reorder information from the server if it comes back in an unexpected order (i.e. if you switch your columns around on the client-side, your server-side code does not also need updating).

sName
Show details
Default: Empty string
Type: string
Code example:

Defines a data source type for the sorting which can be used to read real-time information from the table (updating the internally cached version) prior to sorting. This allows sorting to occur on user editable elements such as form inputs.

sSortDataType
Show details
Default: std
Type: string
Code example:

The title of this column.

sTitle
Show details
Default: null Derived from the ‘TH’ value for this column in the original HTML table.
Type: string
Code example:

The type allows you to specify how the data for this column will be sorted. Four types (string, numeric, date and html (which will strip HTML tags before sorting)) are currently available. Note that only date formats understood by Javascript’s Date() object will be accepted as type date. For example: «Mar 26, 2008 5:03 PM». May take the values: ‘string’, ‘numeric’, ‘date’ or ‘html’ (by default). Further types can be adding through plug-ins.

sType
Show details
Default: null Auto-detected from raw data
Type: string
Code example:

Defining the width of the column, this parameter may take any CSS value (3em, 20px etc). DataTables applies ‘smart’ widths to columns which have not been given a specific width through this interface ensuring that the table remains readable.

Источник

Читайте также:  Как зарядить наушники айпод копия
Оцените статью
sWidth
Show details