alfajango / jquery-dynatable

A more-fun, semantic, alternative to datatables
http://www.dynatable.com
Other
2.77k stars 361 forks source link

Default sort #91

Open lossoth opened 10 years ago

lossoth commented 10 years ago

Is it possible to set a default sort using data-dynatable-column?

stevenolen commented 10 years ago

+1

bandedos commented 10 years ago

+1

dafinley commented 10 years ago

Is this possible?

stevenolen commented 10 years ago

This is actually already supported, but not documented very well. Here's how I set my "total" column to be sorted desc on load (most notably, features.sort and dataset.sorts):

        var dynatable = $('#table').dynatable({
                features: {
                    pushState: false,
                    paginate: true,
                    sort: true,
                    recordCount: true
                },
                dataset: {
                    records: table_data,
                    sorts: { 'total': -1 },
                    perPageDefault: 30
                }
            }).data('dynatable');
JangoSteve commented 10 years ago

@stevenolen is absolutely right. I'll have to add that to the Sorting section of the documentation.

That said, I wouldn't mind adding support for a data-dynatable-default-sort=true attribute you could put directly on the <th> column you want sorted by default.

ksireesha commented 9 years ago

I am fetching the data in alphabetical order but after i bind it to dynatable it's order is not alphabetical anymore... This is odd. Is the data-dynatable-default-sort=true feature implemented ? I tried using sorts: { 'name': -1 } but it didn't work for me.

JangoSteve commented 9 years ago

@ksireesha that feature is not yet implemented using the data-dynatable-default-sort HTML attribute.

Also, dynatable should preserve the order of your records without configuring any default sorting. Are you positive the records are actually being returned from the server or initially populating in the page in alphabetical order?

ksireesha commented 9 years ago

Hello @steve Yes records are in alphabetical order while fetching, as I am using the same database stored procedure to bind another dropdown as well. That drop down has the records alphabetically ordered.

JangoSteve commented 9 years ago

Any chance you could copy the html table and dynatable to a jsfiddle to reproduce the issue? You could fork and modify this existing jsfiddle from the readme if it helps.

ksireesha commented 9 years ago

OK will try to reproduce the issue and get back to you. Thanks for the quick response.

ksireesha commented 9 years ago

Hello Steve,

Here is the fiddle. What I found is it's not working in chrome. In FF the sort order is intact.

tinynumbers commented 9 years ago

The trick @stevenolen suggested (using dataset: { sorts: { 'column': -1 } }) is not working for me in the latest stable download version linked in the Readme. Looks like this feature was added in PR #6 way back in January. Any chance of getting a new stable release pushed out that would include this feature?

mcat95 commented 9 years ago

Not working for me.

piwi91 commented 9 years ago

This doesn't work for me either. The problem is in the mergeSettings, line 159.

var newOptions = $.extend(true, {}, defaults, options);

The options object does have the sort configuration but after the merge it isn't in the newOptions object.

My initialization of dynatable:

$('#dynatable-table').dynatable({
                features: {
                    paginate: true,
                    sort: true,
                    pushState: false,
                    search: true,
                    recordCount: true,
                    perPageSelect: true
                },
                dataset: {
                    ajax: true,
                    ajaxUrl: 'my-route'
                    ajaxOnLoad: true,
                    records: [],
                    sorts: { 'name': 1 }
                }
            });
hugoduraes commented 9 years ago

I'm having the same problem as @piwi91... settings are not being merged!

umdstu commented 9 years ago

Any updates on addresses this deep copy issue in the mergeSettings function? I even tried changing the queries/sorts default hashes in the file itself, and they still don't survive the deep copy.

Also, this is effecting default queries, not just sorts, AFAIK.

DraZen10 commented 9 years ago

Using the example in JSFiddle posted by @ksireeshacould I saw sort function working although it does not in my implementation. Found a workaround just adding the variable sort[field_for_sorting]=-1 into the http_get vars as I open the table from a html form. Hope this helps someone

umdstu commented 9 years ago

That was my solution as well, DraZen10. Clearly not at all ideal, but it has been working so far.

On Thu, Apr 30, 2015 at 11:54 AM DraZen10 notifications@github.com wrote:

Using the example in JSFiddle http://jsfiddle.net/kgonkxyn/3 posted by @ksireeshacould I saw sort function working although it does not in my implementation. Found a workaround just adding the variable _sort[field_forsorting]=-1 into the http_get vars as I open the table from a html form. Hope this helps someone

— Reply to this email directly or view it on GitHub https://github.com/alfajango/jquery-dynatable/issues/91#issuecomment-97849258 .

rstuart85 commented 9 years ago

For anyone that is wondering, this issue appears to be fixed in the master branch.

DraZen10 commented 9 years ago

Yes! It works. I see no change in version or revision, but the file is slightly different and the issue seems to be fixed. Thanks!

umdstu commented 9 years ago

I don't understand. The commit history shows no recent commits. Where was the change?

rstuart85 commented 9 years ago

Not sure when the change was committed @umdstu but the problem was in the init method of the Sorts object. It would always set settings.dataset.sorts to null if there were no GET params. It's stopped doing that now.

rstuart85 commented 9 years ago

Looks like it was fixed over a year ago in b7ead6a1fb61aadbbf3b63531b828b823faa70ee, but there hasn't been a release since then.

aklatzke commented 8 years ago

If anyone else is having this issue and doesn't have the luxury to wait until that commit (b7ead6a) is merged in, the changes are fairly easy to merge in manually. In the stable version of the dynatable script:

Change line 103:

sortKeys: null -> sortKeys : []

Line 193, change:

if (!this.settings.dataset.ajax || (this.settings.dataset.ajax && this.settings.dataset.ajaxOnLoad) || this.settings.features.paginate)

to

if (!this.settings.dataset.ajax || (this.settings.dataset.ajax && this.settings.dataset.ajaxOnLoad) || this.settings.features.paginate || (this.settings.features.sort && !$.isEmptyObject(this.settings.dataset.sorts)))

Replace the Sorts.init function at line 871 with the following:

this.init = function() {
  var sortsUrl = window.location.search.match(new RegExp(settings.params.sorts + '[^&=]*=[^&]*', 'g'));
  if (sortsUrl) {
    settings.dataset.sorts = utility.deserialize(sortsUrl)[settings.params.sorts];
  }
  if (!settings.dataset.sortsKeys.length) {
    settings.dataset.sortsKeys = utility.keysFromObject(settings.dataset.sorts);
  }
};

Hope this helps someone.

dant89 commented 8 years ago

Doesn't look like data-dynatable-sorts got acted upon? Is there a way I can sort any table on the first column by default with the same code? Would be really useful and stop me having to write a custom function... unless there is one that does this?

yogi1135 commented 8 years ago

Guys you are using 'sorts' key on the param, you should be using it on the input field dataset:{ ajaxData: {sorts:{date_missing: -1}} },

JangoSteve commented 8 years ago

I haven't had a chance to figure out why this isn't working. The reason is exactly as @piwi91 described in this comment. I have no idea why the jquery extend function causes the sorts object to be deleted. I don't have time to debug it at the moment, but an alternative way to get default sorting working in case it helps anyone is to bind to the dynatable:init event and add the default sort programmatically on initialization, like this:

$('#my-table')
  .bind('dynatable:init', function(e, dynatable) {
    dynatable.sorts.add('sort_column', -1);
  })
  .dynatable();
msaus commented 6 years ago

I guess this is not still fixed.