Answers.Body: <p>The modifyEntrySortableAttributes technically gets called each time you load new elements on the index page, just so that the controller can confirm that the posted <code>order</code> attribute is valid. But the only time that your plugin can actually affect what is shown in the dropdown is the first time it’s called, on page load.</p>
  
  <p>Maybe a better way to go about this would be to have your plugin return all the available options up front, and then simply hide/show them with JavaScript when the source changes.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-03-18T11:23:53.957Z
Answers.Text: Brandon, could you please make such a script part of the core and add a new source parameter to `modifyEntrySortableAttributes`?
Answers.UserDisplayName: carlcs
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-03-18T11:41:38.567Z
Answers.Text: Thank you for the reply, Brandon. I actually already got far in implementing an AJAX service which returns the modified sortable attributes whenever the source changes – still haven't figured out how to use that data to actually change the contents of the ```sortMenu``` in ```Craft.EntryIndex``` (if its even possible). Your suggestion is a very viable approach, though – I'm a bit concerned about performance with pulling all the layout models and attributes at once, but I guess most sites will have sources in the single or double digits, which should be fine. Back to the drawing board, I guess!
Answers.UserDisplayName: Mats Mikkel Rummelhoff
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-03-20T17:13:21.800Z
Answers.Text: @MatsMikkelRummelhoff I added a [feature request](http://feedback.buildwithcraft.com/forums/285221-feature-requests/suggestions/7274973-add-a-source-parameter-to-modifyentrysortableattr) to have this whole functionality available as a core feature. Maybe you can contribute?
Answers.UserDisplayName: carlcs
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-03-20T20:16:26.767Z
Answers.Text: @carlcs Sweet, voted and commented. I wouldn't hold my breath, though my plugin _will_ have a working, albeit hacked-together, implementation of this sometime soon. Jury is still out on the final approach, but I'm pretty close and will get it sorted (pun intended).
Answers.UserDisplayName: Mats Mikkel Rummelhoff
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-04-06T01:04:47.493Z
Answers.Text: Shameless plug: Just pushed DashCols 1.2 which implements sorting for most columns, by source. Went with Brandon's suggested approach. https://github.com/mmikkel/DashCols-Craft
Answers.UserDisplayName: Mats Mikkel Rummelhoff
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-03-18T11:15:54.173Z
Answers.LastActivityDate: 2015-03-18T11:15:54.173Z
Answers.UserDisplayName: Brandon Kelly
Body: <p>I'm currently writing <a href="https://github.com/mmikkel/DashCols-Craft" rel="nofollow">a plugin</a> enabling users to easily add custom fields to index tables, using the native field layout designer.</p>
  
  <p>My current goal is to have any datetime, boolean, string or number fields added to a source's FieldLayout, also automatically be added to the relevant index table's <em>sortable attributes</em>. Obviously, I've met a stumbling block with <a href="http://buildwithcraft.com/docs/plugins/hooks-reference#modifyEntrySortableAttributes" rel="nofollow">modifyEntrySortableAttributes</a>.</p>
  
  <p>Everything works great on initial pageload (I've found a way to work around the fact that <em>modifyEntrySortableAttributes</em> doesn't have a clue about the source).</p>
  
  <p>My problem is that when navigating between sources, Craft uses AJAX, and the sortable attributes are not updated. Through logging I've confirmed that the <em>modifyEntrySortableAttributes</em> hook <em>does run</em> (see footnote!) every time the source changes, and I also know that my fix for identifying the current source also works (i.e. the plugin fetches the correct FieldLayout whenever the source changes).</p>
  
  <p>So – as far as I can tell – I'm basically stuck with a CP frontend issue, which is that the ".sortmenubtn" button doesn't refresh its ".menu" container contents when the source changes. I'd be happy to refresh the menu myself through some JS in my plugin, but I've looked at the AJAX calls and can't spot any data related to the sortable attributes.</p>
  
  <p>My backup solution is to create my own AJAX service to fetch the updated attributes, but it would be really cool to do this with a minimal amount of hacks (the plugin already contains a few...).</p>
  
  <p>Would greatly appreciate any ideas for solving this!</p>
  
  <ul>
  <li>Footnote: It's actually weird that the hook runs when the source changes, as this directly contradicts <a href="http://buildwithcraft.com/classreference/elementtypes/EntryElementType#defineSearchableAttributes-detail" rel="nofollow">the following statement in the docs</a>:</li>
  </ul>
  
  <p><em>Note that this method will only get called once for the entire index; not each time that a new source is selected.</em></p>
  
Tags: <plugin-development><control-panel><ajax><hook>
Title: Updating index tables' sortable attributes on AJAX
ThreadText: # Updating index tables' sortable attributes on AJAX
  
  (Asked by: username_0 on 2015-03-17)
  
  I'm currently writing [a plugin](https://github.com/mmikkel/DashCols-Craft) enabling users to easily add custom fields to index tables, using the native field layout designer.
  
  My current goal is to have any datetime, boolean, string or number fields added to a source's FieldLayout, also automatically be added to the relevant index table's *sortable attributes*. Obviously, I've met a stumbling block with [modifyEntrySortableAttributes](http://buildwithcraft.com/docs/plugins/hooks-reference#modifyEntrySortableAttributes).
  
  Everything works great on initial pageload (I've found a way to work around the fact that *modifyEntrySortableAttributes* doesn't have a clue about the source).
  
  My problem is that when navigating between sources, Craft uses AJAX, and the sortable attributes are not updated. Through logging I've confirmed that the *modifyEntrySortableAttributes* hook *does run* (see footnote!) every time the source changes, and I also know that my fix for identifying the current source also works (i.e. the plugin fetches the correct FieldLayout whenever the source changes).
  
  So – as far as I can tell – I'm basically stuck with a CP frontend issue, which is that the ".sortmenubtn" button doesn't refresh its ".menu" container contents when the source changes. I'd be happy to refresh the menu myself through some JS in my plugin, but I've looked at the AJAX calls and can't spot any data related to the sortable attributes.
  
  My backup solution is to create my own AJAX service to fetch the updated attributes, but it would be really cool to do this with a minimal amount of hacks (the plugin already contains a few...).
  
  Would greatly appreciate any ideas for solving this!
  
  * Footnote: It's actually weird that the hook runs when the source changes, as this directly contradicts [the following statement in the docs](http://buildwithcraft.com/classreference/elementtypes/EntryElementType#defineSearchableAttributes-detail):
  
  *Note that this method will only get called once for the entire index; not each time that a new source is selected.*
  
  **Tags:** plugin-development, control-panel, ajax, hook
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2015-03-18. Score: 2 | Accepted answer
  
  The modifyEntrySortableAttributes technically gets called each time you load new elements on the index page, just so that the controller can confirm that the posted `order` attribute is valid. But the only time that your plugin can actually affect what is shown in the dropdown is the first time it’s called, on page load.
  
  Maybe a better way to go about this would be to have your plugin return all the available options up front, and then simply hide/show them with JavaScript when the source changes.
  
  > **Comments:**
  > **username_2**: username_1, could you please make such a script part of the core and add a new source parameter to `modifyEntrySortableAttributes`?  
  > **username_0**: Thank you for the reply, username_1. I actually already got far in implementing an AJAX service which returns the modified sortable attributes whenever the source changes – still haven't figured out how to use that data to actually change the contents of the ```sortMenu``` in ```Craft.EntryIndex``` (if its even possible). Your suggestion is a very viable approach, though – I'm a bit concerned about performance with pulling all the layout models and attributes at once, but I guess most sites will have sources in the single or double digits, which should be fine. Back to the drawing board, I guess!  
  > **username_2**: @username_0 I added a [feature request](http://feedback.buildwithcraft.com/forums/285221-feature-requests/suggestions/7274973-add-a-source-parameter-to-modifyentrysortableattr) to have this whole functionality available as a core feature. Maybe you can contribute?  
  > **username_0**: @username_2 Sweet, voted and commented. I wouldn't hold my breath, though my plugin _will_ have a working, albeit hacked-together, implementation of this sometime soon. Jury is still out on the final approach, but I'm pretty close and will get it sorted (pun intended).  
  > **username_0**: Shameless plug: Just pushed DashCols 1.2 which implements sorting for most columns, by source. Went with username_1's suggested approach. https://github.com/mmikkel/DashCols-Craft  
  
  ---
  
  