Whenever I use <td colspan="x"></td>, I get the following error:
Uncaught TypeError: Cannot set property '_DT_CellIndex' of undefined(…)
Demo
$("table").DataTable({});
<link href="https://cdnjs.cloudflare.com/ajax/libs/datatables/1.10.12/css/jquery.dataTables.min.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/datatables/1.10.12/js/jquery.dataTables.min.js"></script>
<table style="width:50%;">
<thead>
<tr>
<th>1</th>
<th>2</th>
<th>3</th>
<th>4</th>
<th>5</th>
<th>6</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
<td>6</td>
</tr>
<tr>
<td>1</td>
<td colspan="2">3 4</td>
<td colspan="3">4 5 6</td>
</tr>
</tbody>
</table>
It's working properly without DataTables.js, but when we use this structure with datatable.js it is not working. We need above table structure. Does anyone have any idea how we can use this table structure datatable.js?
You can hack around the lack of colspan support by adding an "invisible" cell for every cell that's eliminated:
<tr>
<td colspan="3">Wide column</td>
<td style="display: none;"></td>
<td style="display: none;"></td>
</tr>
<tr>
<td>Normal column</td>
<td>Normal column</td>
<td>Normal column</td>
</tr>
DataTables doesn't complain, the table renders normally and sorting works (invisible columns sort as the empty string).
I haven't tried this with rowspan.
COLSPAN: Ajax or JavaScript sourced data
Excellent solution provided by Dirk Bergstrom only works with HTML sourced data.
It is also possible to use the same idea with Ajax sourced data.
You need to use createdRow option to modify required cells when TR element is created in table body.
For example:
var table = $('#example').DataTable({
ajax: 'https://api.myjson.com/bins/qgcu',
createdRow: function(row, data, dataIndex){
// If name is "Ashton Cox"
if(data[0] === 'Ashton Cox'){
// Add COLSPAN attribute
$('td:eq(1)', row).attr('colspan', 3);
// Hide required number of columns
// next to the cell with COLSPAN attribute
$('td:eq(2)', row).css('display', 'none');
$('td:eq(3)', row).css('display', 'none');
// Update cell data
this.api().cell($('td:eq(1)', row)).data('N/A');
}
}
});
See this example for code and demonstration.
See jQuery DataTables: COLSPAN in table body TBODY - Ajax data article for more details.
ROWSPAN
It is possible to emulate ROWSPAN attribute using RowsGroup plug-in.
To use the plugin, you need to include JavaScript file dataTables.rowsGroup.js and use rowsGroup option to indicate indexes of the columns where you want grouping to be applied.
var table = $('#example').DataTable({
'rowsGroup': [2]
});
See this example for code and demonstration.
See jQuery DataTables: ROWSPAN in table body TBODY article for more details.
COLSPAN and ROWSPAN
It is possible to group cells both vertically and horizontally simultaneously if we combine both workarounds described above.
See this example for code and demonstration.
See jQuery DataTables: COLSPAN in table body TBODY - COLSPAN and ROWSPAN article for more details.
Simple solution is here.
<tr>
<td colspan="4">details</td>
<td style="display: none"></td>
<td style="display: none"></td>
<td style="display: none"></td>
</tr>
No other solution
https://datatables.net/forums/discussion/33497/cannot-set-property-dt-cellindex-of-undefined
If you are using scrollX
<thead>
<tr>
<th style="width: 20px;"></th>
<th>column H</td>
<th>column H</td>
</tr>
</thead>
<tbody>
<tr>
<td colspan="3">Wide column</td>
<td style="display: none;"></td>
<td style="display: none;"></td>
</tr>
<tr>
<td>123</td>
<td>Normal column</td>
<td>Normal column</td>
</tr>
</tbody>
Modified answer of #Dirk_Bergstrom
Refer below link.
fnFakeRowspan
Also refer the plugin here.
fnFakeRowspan plugin
Hope this will help you
I don't know at what point, but I guess the fnFakeRowspan does not support the current DataTable version.
For an alternative, checkout RowsGroup plugin.
Found it pretty easy to implement and works well.
Searching works perfectly, ordering doesn't.
Check here for an implementation.
Because datatables doesn't support colspan, I usually ended up with using unordered lists (ul) with list-style: none;
here is the result
Related
I'm trying to make the sorttable.js to read values from a custom tr attribute, which is "sort_value".
<table>
<thead>
<tr>
<th class="sort_string">Name</th>
<th class="sort_int">Age</th>
<th class="sort_int">Date of Birth</th>
</tr>
</thead>
<tbody>
<tr>
<td>Adam</td>
<td>22</td>
<td sort_value="19930403">April 3 1993</td>
</tr>
<tr>
<td>Matt</td>
<td>20</td>
<td sort_value="19950220">Feb 20 1995</td>
</tr>
<tr>
<td>Josh</td>
<td>25</td>
<td sort_value="19900730">July 30 1990</td>
</tr>
<tr>
<td>Kent</td>
<td>27</td>
<td sort_value="19880322">March 22 1988</td>
</tr>
</tbody>
</table>
Here's the jsfiddle: http://jsfiddle.net/zz4mugen/6/
Everything is perfect except the date of birth column. Please take a look at the .js of the jsfiddle. It's on the external resources column.
You're using custom attributes, but the name is not valid. Own attributes have to start with data-. See the official documentation: http://www.w3.org/TR/html5/dom.html#embedding-custom-non-visible-data-with-the-data-*-attributes
Below an interesting blog with explanation and examples about sorting different types of values in tables, such as numbers, texts, currencies and dates for different locales.
https://yoast.com/articles/sortable-table/#example
I have created a basic table in js fiddle. I am using the datatable sorter function, however if you click along the headers, or click a header, skip one and click another, it seems to ignore the first mouse-click. (To replicate the issue click on Confirmation Period, then ABN, then back to Confirmation Period)
Any thoughts?
<table id="tableSort" class="tableSort" cellspacing="0" style="margin-top:20px;margin-left:10px;">
<thead>
<tr>
<th>Confirmation Period</th>
<th>Legal/Entity Name</th>
<th>ABN</th>
<th>Business/Trading Name</th>
<th>Status</th>
</tr>
</thead>
<tr>
<td>1</td>
<td>a</td>
<td>34</td>
<td>78</td>
<td>b</td>
</tr>
<tr>
<td>2</td>
<td>c</td>
<td>100</td>
<td>90</td>
<td>g</td>
</tr>
and the JS...
$(document).ready(function () {
$('#tableSort').dataTable({
"searching": false,
"paging": false,
"info": false
});
});
jsfiddle: http://jsfiddle.net/wcdg3ddL/
The table is actually sorting as expected. There are two reasons why it looks like the columns aren't sorting:
You have insufficient rows in your table to assess whether or not sorting is working. Add few more rows of data and you should see what I mean.
Because you've removed the arrows from the header row with your custom styling you cannot accurately gauge how the sorting is behaving. If you add in the default CSS styling you can see the direction in which a column is being sorted.
Here is a fiddle where I have added sufficient rows so that the columns appear to be sorting correctly.
I just added more rows to your fiddle:
<tr>
<td>1</td>
<td>a</td>
<td>34</td>
<td>78</td>
<td>b</td>
</tr>
I have a question. Is there any possibility in jQuery dataTables plugin to only filtering rows with specific class?
For example: I got a table and I want to filter only rows with searchable class. The rest rows stays as they is.
Is this even possible?
<table class="dataTable">
<tr class="searchable">
<td>text to search</td>
<td>text to search</td>
</tr>
<tr>
<td>something else</td>
<td>something else</td>
</tr>
<tr class="searchable">
<td>text to search</td>
<td>text to search</td>
</tr>
<tr>
<td>something else</td>
<td>something else</td>
</tr>
</table>
Edit:
I'm thinking to write my own plugin, but I never do that before.
Something like:
getFilterInput
table.each(tr).function(){
if(row.hasClass(searchable){
//do filtering
}else{
//leave row alone
}
Anyone does something like that and can give me a clue where to start?
You can just use selector like this: $('.searchable')
$('tr').not('.searchable').hide();
One thing you could do is use a hidden column that says whether that row is searchable or not, then when you need to find those specific rows, you just search that particular column.
The way you're trying to leverage classes in your markup will almost certainly require that you modify DataTables.js and/or develop your own plugin.
I am have a rails app that I am writing cucumber test for. I am trying to get a number out of specific row so I can assert against it. I have a table that looks like this:
<table class="table table-striped" id="kids">
<thead>
<tr>
<th>Name</th>
<th>Balance</th>
<th></th>
</tr>
</thead>
<tbody>
<tr data-link="/kids/2">
<td>Jason</td>
<td>
<span>$</span>
<span class="money">1.00</span>
</td>
</tr>
<tr data-link="/kids/3">
<td>Neely</td>
<td>
<span>$</span>
<span class="money">0.50</span>
</td>
</tr>
</tbody>
</table>
I need to select a td that has a specific name, and then get the balance from the span in the following td. I am sure there is a way to do this with Xpath, but I cannot figure it out. Any help would be great.
You asked for XPath so here it is:
//td[preceding-sibling::td[text()='Neely']]/span[#class='money']
Note that XPathes aren't very readable and it may be better to use Capybara's ruby methods instead:
tr = find('tr', text: 'Neely')
tr.find('.money').text
This question is related to this question I asked a little while back. The updated code is posted here. This to note is that i am looking to create a HTML table dynamically that looks similar to this:
<table>
<tbody>
<tr>
<td colspan="3" align="right">Header</td>
</tr>
<tr>
<td colspan="3" align="right">Header</td>
</tr>
<tr>
<td colspan="3" align="right">Header</td>
</tr>
<tr>
<td>Col1</td>
<td>Col3</td>
<td>Col4</td>
</tr>
<tr>
<td>Col1</td>
<td>Col3</td>
<td>Col4</td>
</tr>
</tbody>
</table>
I can get this done in markup but when I do it in js the colspan does not seem to work in IE7. Any hep will be greatly appreciated.
http://www.w3schools.com/jsref/prop_tabledata_colspan.asp
The colSpan javascript property has a capital S.