Prevent select event from firing if selection has not changed
Prevent select event from firing if selection has not changed
rldean1
Posts: 141Questions: 66Answers: 1
How do I stop the select
event from firing via ajax.reload()
if the selection has not changed?
How do I evaluate IF the selection has changed? Is there a mechanism already built into DT, or would I need to roll my own? If so, do you have any recommendations?
This discussion has been closed.
Answers
I don't believe that
ajax.reload()
will re-select the rows. You must have some custom code doing this. Maybe you will want to use theuser-select
event instead of theselect
event. Please post more information/code that happens when you are usingajax.reload()
. A test case would be great.Kevin
@kthorngren :
ajax.reload()
will retain selections, which is fine. I have observed that theselect
event is fired after a reload (I prevent de-selection)user-select
as you suggested might be a good solution, but I can't seem to access the row data viadt.row({selected: true}).data()
ordt.row('.selected').data()
because the data is not yet selected (documentation says: "user-select
event is triggered before items are selected").on( 'click', 'tr', function ()
because, well, I preferselect
since it's an explicit DT utility, but I may have no choice..on('click'...
. OR, I need to find a way to compare my{selected token}
with the "current selection" to see if they are the same BEFORE I callajax.reload()
I stand corrected I found that if you have
rowId
then Datatables will re-select afterajax.reload()
.I don't believe there is anything in Datatables to know if the selections have changed after ajax.reload(). Looks like you might be keeping track of the indexes of the selected rows. Maybe you can compare the saved indexes to the indexes in the select event. You could set a flag in the
xhr
event to know if theselect
is running to to a reload or other selection, see example in this thread.Kevin
I don't fully understand the example. Does the
xhr
event give me access to previously selected information?I'm afraid I've made this way too complicated. I've been stuck in a rut for a while.
Ultimately, all I'm really trying to do is enable/disable jQuery tabs based on row data across all selections in all my tables.
If certain data is the same as I click between tabs (like the Date), then I don't need to reload tables. If the Date changes, then I would need to reload the table.
I can't figure out a "simple" way to do this.
No, that example is just used to set a flag to indicate their was an XHR response from the ajax.reload() to use in your
select
event.I'm not totally clear on what you are trying to achieve.....
What do you mean by enable/disable? Do you mean to make the tab not clickable?
How does the Date change. Can you trigger a reload if the Date changes then you don't need to compare each time you switch tabs? Or maybe use a global variable to indicate one or more changes have been made then fetch via ajax when the tab is shown.
I built a simple test case that hopefully you can update to reflect your data. It doesn't use ajax to load the data but JS data. Understanding your data and how / when you want to check for changes to the Date will help us better understand how to help you. Modify the test case as you see fit.
http://live.datatables.net/xojiqite/12/edit
Kevin
@kthorngren I really appreciate your help here... however, I won't be able to replicate this in the test environment without a great deal of effort. This issue can be closed.
I do have some thoughts and suggestions about the select event, which I may write a "feature request" post.
I read through your other thread and think I misunderstood your question above. I thought you were wanting to do something in the
select
event afterajax.reload()
. But in the other thread it seems like you want to keep the automatic re-selection from happening afterajax.reload()
. You should be able to usedeselect()
to de-select all the rows beforeajax.reload()
.I looked through the code in the other thread and get the gist of what you are doing but its hard to visualize without actually seeing it with the data.
I updated my example to show how you can use
filter()
to determine if there is matching data in the other tabs (to the left) and disable if not matched:http://live.datatables.net/xojiqite/14/edit
My example uses Bootstrap tabs. Not sure which you are using as I've never used
$("#tabs").tabs("option", "disabled", []);
. Anyway maybe my example will give you some ideas to help.Kevin