Question/Feature requests for stateRestore default state
Question/Feature requests for stateRestore default state
Wernfried
Posts: 57Questions: 12Answers: 0
Is it possible in stateRestore to set the default/inital state by option or API? I did not find a simple way to do it.
A possible implementation might be an extension of https://datatables.net/ref/core/option/stateRestore.defaults property:
true- The end user can set a state to be the default statefalse- The end user cannot set a default state."string"- The name of the state to be loaded (if existing) by default
Then the default state could be retrieved for example from URL parameters:
const urlParams = new URLSearchParams(window.location.search);
new DataTable('#myTable', {
layout: {
topStart: {
buttons: ['stateCreate', 'statesList']
}
},
stateRestore: {
defaults: urlParams.get('state')
}
});
If such feature becomes available, I think I could convince my manager to go for DataTables Plus
Wernfried
This question has accepted answers - jump to:
Answers
You have my attention
In all seriousness, what you are looking for is already available (assuming I've understood correctly). To have initial states in StateRestore you use
stateRestore.predefinedand you can setisDefaulton the one you want to be loaded up by default.Here is a little example.
Does that match what you want?
Allan
Not really. In my application the user will create the states by himself, I don't know the individual preferences and needs of my users. Yes, the user can select which state should be the default - but just one. The user may have 2 or 3 states which they use frequently and they want to open them with one click, typically with a bookmark in their browser or even as part of their start-pages.
Wernfried
Ah I see - many thanks for the clarification. How about if there were an option for when you change state it sets either a search location or a query parameter on the URL. That way you could book mark that URL and StateRestore would check the search or query (depending on the configuration) for that parameter and use the appropriate state. It could be something like
?dtsr=1where1is the state id. Would that suit?Allan
Yes, that's exactly what I was looking for. At the moment I create a custom button where I loop over all state properties and add each key-value as query parameter on the URL. A single query parameter (i.e the state id) would make much more sense.
Wernfried
Perfect - I think I can add that in fairly short order. I'm in the middle of some SearchPanes updates at the moment, but I'll look at implementing this and get back to you when it is ready.
Allan
I've just released StateRestore 2.2.0 which introduces a new
stateRestore.bookmarkoption.You can see a set of example here - they walk through the various options for how this new feature can be configured.
If you get a chance to try it out, I'd welcome any feedback you have.
Regards,
Allan
Hi Allan
From documentation, this looks really great, thank you.
I will test it in the next days or weeks.
Best Regards
Wernfried
I started implementation of this feature, really nice.
Just a few minor styling points when you use the extension with Bulma styling. When you create a new state, then you get a dialog like this:
Is this layout by intention? I mean, it is working but it is not award winning beautiful. What about this one?
Or maybe, use the
titleattribute on the check-box.Font Awesome is not part of Bulma Framework, they are just used as example. To get nicer drop-down indicators you may replace
by
Best Regards
Wernfried
I completely agree, that looks much better!
The reason I didn't do that, or something similar, is that the Bulma documentation just has the label and inputs stacked, so I assumed that is what folks using Bulma were expecting the UI to look like by default, and it can then be tweaked if needed.
Possibly I've got the wrong end of the stick with that though?
I've made that mistake before. They use FA in their docs, so I made use of FA as well. I'll get that updated - thanks!
Allan
For me the Bulma
.helpclass is just a generic class you can use anywhereSure, it makes sense to put a help text below a text-input or a dropdown-input. But it's stupid to put it below a tiny checkbox element. So, why not putting it below the label?
I did not find any sample or documentation where
.helpis used in combination with a checkbox. Most likely the Bulma developers did not have this combination in their mind. They provide generic Flexbox helpers, for me that's an invitation to utilize all nice functions you have with Flexbox.Wernfried
Their documentation shows the labels and inputs stacked, and the checkbox example there is an exception though, as its has the label to the right:
I don't think that would work for the StateRestore use case, and I prefer your styling. As I say, I used a stacked layout since it seemed to be what their documentation shows. I'll check and see if the classes and markup I provide options for in StateRestore allow an alternative to easily be set.
Allan
One more point. It took me some time to understand the concept of these "incremental" states. More than one state can be active at the same time, which makes the entire feature more powerful.
But when you use stateRestore.include then you have a small issue.
Assume, you create a state with (only) property
columnVisibilityand another one with(only) propertycolumnSearchand you setisDefault: trueon both of them - which is accepted on local and I do accept it on ajax.Then I would expect DT to load both states, and applies columnVisibility and columnSearch at the inital view. However, DT loads only the first one, the user cannot control the order, I guess.
I see several possible solutions:
isDefault: truemust be set on one state only, setting it on more than one state is not possibleIn this case, you may also enforce to enable all state properties - which is maybe not applicable for all use-cases
isDefault == trueand applies them. Of course, the user may have created several states which are contradicting to each other and the final result depends on the order of reading and may not match the expectation of the user.isDefaultattribute on state level, you may set it at property level. Each property must have not more than oneisDefault. Honestly, this would seem to be the most advanced solution - but maybe an overkill and many user might be overwhelmed by it.Thus, I would suggest option (2) and just take the risk, that users may create multiple default states which are contradicting to each other.
The bookmarks may be also extended to load more than just one state, i.e. URL parameter
?dtsr=1&dtsr=2should load state 1 and state 2. Of course, for hash location#this wold not be possible easily.Kind Regards
Wernfried
A few more comments.
The stored state always contains
lengthproperty, no matter if{include: {paging: ...}}is true or false.I would expect
lengthexists only for{include: {paging: true}}. Perhaps it is not relevant.One more issue:
The logic of active states is not fully correct. I have these three states:
In my table I would expect all three states as active, but I get only 'To Do' and 'My Team - CC only' - even when I load 'My Team' explicitly!
Looks like, if fails when comparing the
visibleproperty. Visibility of columns had not been touched at all, they are all default.Kind Regards
Wernfried
Hi Allan
You can forget the last paragraph of my previous post regarding the unclear active/non-active states. It was a mess in my data.
However, this part is still valid
Thanks for the update. Regarding the page length issue - StateRestore has
pageStartandpageLengthoptions in thestateRestore.includeproperty.When I started on StateRestore 2 I did actually just have a single
pagingoption there, but while developing and testing it, I decided to split it into two parts as I felt that provided more utility.I'm afraid that the error here is in the documentation which is an artefact of that history. I've committed a fix for that.
Allan