Arbitrary sorting of assets (metadata + AJAX?)


(Andrew Harris) #1

Trying to solve a problem in a user friendly way, maybe someone has some suggestions.

 

I have a page of items which need to be sorted in an arbitrary way - not by name, or date, or any of the usual attributes. My only way thus far has been to create a 'sort' metadata field - add values to the items and use that as the sort key. Works fine, but it's pretty ordinary if you've got 20-30 items and you need to go into each one to edit the metadata field, then back out and refresh the page to make sure you've got it right. I'd love to offer something smoother.

 

What I'd really like to do is to be able to update the metadata field with just a click. A good example is star ratings on comments, where changing the rating with a click would update the metadata. Not that fussed that the listing would then need to be refreshed to view the new sort order (though I could probably just fudge that with javascript too).

 

Can it be done? I read some threads that suggested that it might be possible to do with triggers. Can anyone elaborate, or provide an example? I'm afraid I get a bit lost.


(Andrew Harris) #2

...possibly answering my own question here... is this where I should be looking?

http://manuals.matrix.squizsuite.net/web-services/chapters/soap-api-metadata-service#setassetmetadata

 

as you can probably guess, I'm getting quickly out of my depth!


(Cgrist) #3

Hi,

 

I think it might help if you explained a bit more about the use case?

 

When you say arbitrary, do you mean random? Or is it based on some kind of meaning? What you're suggesting sounds like it would let you update it as the user interacts with it (and you can do this with the JS API) but then the sort order will be updated for all users who view the listing... is this that what you want? Or do you want users to be able to manipulate the ordering only for their session?

 

If you do want that, you can use the actual sort order on the updateLink function of the JS API... http://manuals.matrix.squizsuite.net/web-services/chapters/javascript-api#updateLink

 

e.g:

 

js_api.updateLink({    "asset_id":<ID of parent asset>,
   "child_id":<ID of child asset>,
   "sort_order":0,
   "dataCallback": function(data){

      console.log('testing response from update link function:');

       console.log(data);
  });
})

 

I left out some of the parameters of that function, not sure which ones are required off the to of my head...

 

You can also use the JS API method for setting metadata if you still wanted to sort by metadata instead of the tree order.


(Andrew Harris) #4

Yes, sorry, arbitrary, as in a particular user deciding which item should go first, second etc. and everybody seeing that sort order.

 

This time it's stories in a newsletter, where the editor will be able to change the order in which stories appear prior to publishing, but I've wondered how to do it on previous occasions in different scenarios. Actually, tree order is probably sensible, given that it's viewable at a glance and also updatable via the asset map. Dunno why I didn't think of that.

 

Never used the JS API, looks like I've got a bit of a curve ahead of me judging by how much I understood of the page you linked to!

If only the manuals had an explain-it-like-i'm-five edition, that would be brilliant ;-)


(Cgrist) #5

ahh yep so the scenario is a kind of custom editing interface rather than a front end user, i get you. 

 

my first thought would be to list the story items, print the ID of the parent asset and the child asset as data attributes for each item, and use a JS plugin to make the items sortable, then use JS API to update all the links. 

 

So a list item might look like:

<li data-parent="1234" data-id="5678">Name of story 1</li>

 

To get started with the JS API, you need to create a JS API asset in your web services folder, and enable the functions you want to use, and then include the URL of the asset as a <script> tag on your page. Once you've done that, you can access the JS API object. This page is probably a better intro: 

 

http://manuals.matrix.squizsuite.net/web-services/chapters/basic-javascript-api-implementation

 

I would recommend using the chrome (or firebug) console so that you can submit JS API functions directly, rather than saving your JS and reloading the page every time, it makes it a lot easier to understand what the JS is doing once you get used to it.

 

Jquery UI might be a good start for the sorting: https://jqueryui.com/sortable/

 

Hope that helps!


(Nic Hubbard) #6

If you don't want to use the JS API, I wrote a sorting script a while back: http://www.zedsaid.com/blog/change-the-asset-map-sort-order-using-javascript-in-squiz-matrix


(Andrew Harris) #7

Thanks heaps Chris and Nic.

That's given me heaps to go on with. If I can pull this off, I'll post back here ;-)