Jump to content

Sorting of page field


Hari KT
 Share

Recommended Posts

Hi,

I have add page field with categories to `user` template.

In the processwire admin we can sort the page field items by dragging and moving the items.

I would like to bring the same functionality to the front-end, so users can have a functionality to drag and drop. This can be saved via ajax rather than a save button.

Would be helpful if anyone have done something like this.

Thank you

Link to comment
Share on other sites

changing the order of a page field is as easy as pushing the elements one at a time:

I prepared an example on codepen using jqueryUI "sortable widget" with the "serialize" option and get http://codepen.io/diogo-ed/pen/fgazd

On the pen I followed the instructions from the serialize method and used the ids of the pages after the underscore on the ID "page_123" http://api.jqueryui.com/sortable/#method-serialize

The code in PW is really simple:

foreach($input->get->page as $g){ //iterate all the page[] array from GET
  $p = $pages->get($g); // look for the page with this ID
  echo "<li>".$p->id."</li>";
  $page->of(false); // set output formatting of
  $page->page_field->push($p); // push it to the end of the field
  $page->save("page_field"); // save page
}

EDIT: I'm using lightning.pw for the ajax call in the demo :)

EDIT2: Like kongondo says, you need to sanitize the get calls. Take this only as a prof of concept.

EDIT3: Added the output formatting of and save the field (forgot before)

  • Like 2
Link to comment
Share on other sites

Hey diogo,

Thank you for the reply.

My problem is not at the front-end creating dragging stuff. My problem is at the backend how to make use of the PageArray to get sorted.

See the PHP code

  • Like 1
Link to comment
Share on other sites

@Diogo - cool idea using lightning.pw...I always forget that. But where is your demo?

Hm, not that cool, my free trial will expire in 3 days :P

The demo is here:

$.get(
  "http://rhodium-4zp.lightningpw.com/test/", // <- there, see?
  sorted,
  function( data ) {
    $( "#result" ).html( data );
  });
}
  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...