DEFAULT
I create an ajax datatable whose rows are sometimes filled with json at the end of the table: jsfiddle , and sometimes at the top of the table. It depends on the ajax response time.
RECOMMENDED OUTPUT
I have two input jsons from two different sources, and the output is a table:
<table> <tr><td>1</td><td>2</td><td>3</td></tr> <tr><td>1</td><td>2</td><td>3</td></tr> <tr><td>1</td><td>2</td><td>3</td></tr> ... <tr><td>1</td><td>2</td><td>3</td></tr> <tr><td>1</td><td>2</td><td>3</td></tr> <tr><td>8</td><td>7</td><td>6</td></tr> <tr><td>8</td><td>7</td><td>6</td></tr> <tr><td>8</td><td>7</td><td>6</td></tr> <tr><td>8</td><td>7</td><td>6</td></tr> <tr><td>1</td><td>2</td><td>3</td></tr> <tr><td>1</td><td>2</td><td>3</td></tr> ... <tr><td>1</td><td>2</td><td>3</td></tr> </table>
Rows from 2. json are inserted into the table (created from 1. json) at a specific position. This position is constant, lengths 1 and 2. The json data is constant.
FIRST DECISION
I have to add the first column containing the number and sort the data by them, omitting them - jsfiddle . I can hide the first jsfiddle column, but I rather use a custom function because it does not work in IE8.
var t = $("#tab1").DataTable({ "ajax": "data1.json", columnDefs: [ { className: "hide", "targets": [ 0 ] }, ], "columns": [ { "data": "id"}, { "data": "cat1"}, { "data": "cat2"}, { "data": "cat3"} ] }); $.ajax({ type: "GET", url: "data2.json", contentType: "application/json; charset=utf-8", dataType: "json", success: function (response) { t.rows.add(response.data); t.draw(); } });
IDEA - CUSTOM FUNCTION
I am trying to create a custom function rows.addinposition(rows, position) , but it works like the function rows.add() .
I copied and changed the rows.add function found in jquery.dataTables.js to line 7879 , I changed out.push() to out.splice() splice docs .
I know this is not recommended, it is better to extend datatables api ...
_api_register( 'rows.addinposition()', function ( rows, position ) { var newRows = this.iterator( 'table', function ( settings ) { var row, i, ien; var out = []; for ( i=0, ien=rows.length ; i<ien ; i++ ) { row = rows[i]; if ( row.nodeName && row.nodeName.toUpperCase() === 'TR' ) {
It would be great if you could help me.
I found similar questions:
EDIT
Thanks davidkonrad, but I am testing it in jsfiddle and I found 2 problems:
- the order is wrong 2., 1., not 1., 2. - I think the problem is easy.
- sometimes these added rows are on the top of the table, sometimes in the correct position. Accidentally. - maybe a big problem.
I am debugging it in jsfiddle and its behavior is very strange:
console.log('rowCount = '+rowCount);
if the lines are on top (bad position) return:
rowCount = 0 rowCount = 1
and for did not execute because firebug does not show var i .
if they are in a good position , return:
rowCount = 5 rowCount = 6
and for and var i returned in this example:
1. loop:
i = 5 i = 4 i = 3
2.loop:
i = 6 i = 5 i = 4 i = 3
Did I miss something? Why is the order strange?