Index column with deferred rendering
Index column with deferred rendering
Hi there. I tried using the code from this example to implement an index column. However, it seems that it doesn't work for tables with deferred rendering.
First of all, neither search
, nor order
events are fired when I switch the page in my table. But even if I listen to the page.dt
event, the nodes don't seem to be available yet by the time my listener is invoked. My table displays 10 rows per page, and when I switch to page 2, only 10 nodes are available (checked inside my listener function), page 3 – 20, page 4 – 30, and so on. That is, only the nodes for the previously visited pages are available. If I switch to the previous pages, the index column will be drawn correctly. And if I add a small delay (100ms in my case), it also helps.
Answers
The events are being triggered as expected in this example.
The nodes wouldn't be available if
deferRender
is enabled. This is the optimisation for that option to speed up the table loading. By the time theevent draw
is triggered, the nodes would be fully formed (see the example above),Could you look at that, please, and see if it helps. If it's still not working for you, please can you update my example, or link to your page, so that we can see the problem.
Colin
Thank you for your answer. I added the code needed to display the index column to your code (see here). However, there's a bug. Try switching pages in this order: 1 -> 6 -> 5. You should see that the numbering is wrong (page 6 has indexes 11-17, page 5 has indexes 11-20).
Please note that I have also tried to define my index column like so:
It works well most of the time, but sometimes the numbering gets screwed up too. It happens after some sorting/redrawing and I can't reliably reproduce it.
Hi,
What do you want to happen when the table is ordered or sorted? Should display gaps in the index, or is a number assigned to a row, always what it will display?
Allan
The content of the index column should be static, in some sense. That is, regardless of the ordering or sorting, the very first row on the very first page should always have number 1, and the number is incremented for each next row. Therefore, there should never be any gaps, and the number of the next row should always be +1 from the previous one.
Instead of using
column().nodes()
usecell().data()
as shown in the first example you linked to. Like this:https://live.datatables.net/ruhuguwu/273/edit
Kevin
If you don't want the indexes to change after sorting or searching then move the code to
initComplete
, for example:https://live.datatables.net/wibayuzi/1/edit
Kevin
@kthorngren, the former of your two examples seems to do what I want. However, typescript complains:
Even if I suppress the warnings, the code doesn't seem to work.
I'm using
datatables.net@1.13.8
.I found another solution on the internet, and it seems to work so far:
Do you think this is a reliable solution?
Also, if you could help me out solving the issue above, I'd appreciate. I couldn't figure out myself what's wrong. The version I'm using is the same as in the example you linked (1.13.8). I also tried disabling all my datatables plugins. Even if I suppress the typescript errors, the code doesn't seem to work (the content of the cells appears as
[Object object]
).Editing the HTML is directly means that DataTables wouldn't be aware of the change, but that's fine if you don't want that column to be searchable or orderable, which you don't, so yep, a good way to go for you.
I'll let Allan reply to the TS issue, that's his territory!
Colin
@colin, thank you!
The TS error is an error in the DT TypeScript files - sorry. Do
0 as any
for the moment. I know it's a bit crap:but it will at least get you going until I get the release with the fix out (it has been fixed).
Out of interest, what the is VSC extension you are using to show the parameter names for functions - that's cool. I've not seen that before.
Can you update Kevin's example to show the issue you are having with
[object Object]
so I can take a look into it please?Allan
This is not VSC but WebStorm. It does this out of the box.
I'm still seeing
[object Object]
. To reproduce this you simply need to specifydata: null
for the index column (see here). Surprisingly, though, if I changenull
to some real property name from my datasource, it still won't work as expected – it will just display the data from that property.More details
If I log
this.data()
right after changing the data, it shows the whole row's data instead of the cell's data (which is unexpected, I guess).I think you need to add
columns.defaultContent
with an empty string to that column.. Otherwise thedata: null
will try to display the full row of data thus the[object Object]
. This is from thecolumns.data
docs:Kevin
Now the index column just displays whatever I put in the defaultContent. And
this.data()
still shows the data of the whole row (I check it after doingthis.data(i++)
).Do you still have the TS2554 error you noted above?
Can you provide a test case showing the issue?
I'm not familiar with TypeScript either so if its TS related then Allan will need to help.
Kevin
If you are using
data: null
you don't want to then usecell().data()
to write to that cell. Assuming DataTables even copes with that (I've never tried it), it would delete the rest of the data from the row, since as Kevin points outnull
means the whole data object is the data for that cell.If the issue is that you want to write the index to a data point on the object, a data point which doesn't exist, just make one up: https://live.datatables.net/wibayuzi/2/edit . Set a default content value so DataTables doesn't complain about it initially.
The other option, since you just want an index based on the row's position in the array, is to add it as a property before DataTables uses the data for the display. If you are Ajax loading the data like in the example, then use
ajax.dataSrc
to manipulate the data: https://live.datatables.net/roxoyipa/1/edit .Allan
That's a neat feature I never considered ;-)
Kevin