Friday, February 17, 2012

JQuery Sortable and VisualForce

So todays topic of discussion is how to use the sortable method of JQuery within a VisualForce page to display and allow users to update the specic order of the records. I am going to cover this in it's most generic form and what you choose to do with it is up to your imagination. I was recently asked to create an interface for users to view a list of projects by department and provide them the ability to move these projects around based on priority etc and then update the list order. So our finshed result was very detailed lists with status icons for such things approved, priority level, color coded based on department and so on.

So lets get started. In your development account, the products object should already have an existing set of generic product records. So for the purposes of this tutorial I decided to go ahead and what was there, but you can use any existing or custom object of your choosing.

First thing we are going to do is add a new custom field to the object, I choose to name mine "sort order" and set the field type to number. Next lets create a new Apex class and name it ProductSort. In the Apex class lets go ahead and query the Product object and return a list ordering it by our new field which at the moment contains no data. Your code should look similar to this:

public  class ProductSort {
public list  p;
public Product2[] getp() {
 p=[Select Id,Name, sort_order__c From Product2  Order by sort_order__c];
 return p;
}
}


Next lets create a new VisualForce page (I just named mine Sort) and add a few references to the JQuery and JQuery UI libraries hosted on on Google. Then we are going to setup the JQuery Sortable list and embed an apex:repeat wthin it.



 



 


 

  • {!prod.Name}

Looking at the page you can see we have include statements to the JQuery and UI libraries, one to the Cupertino theme Style sheet(You can choose an existing theme or roll your own) and some minimal list styling. Next we have our function to intialize the sortable list based on the DOM Id of the object (#sortable). Within the <ul> tags we have an apex:repeat to display the list of products that we queried. If you look carefuly we are settting the id of each <li> to the record Id of the products, we are going to use this next to update the sort order within the product record.

Now if we take a look at the page you should see something simular to this:

You should be able to drag the products up and down within the list.

Now lets go ahead and tie into the Sortable method and update our sort order. There are alot of different ways you can go about this. For instance you could kick off an update routine within your Apex class, you could use a button on the page and allow the users to update all of the records at one time after they have finished getting all of the records sorted the way they want, and so on.

We are going to use Ajax to update the entire list each time a user changes the postion of a product. First we are going to add our includes to the Ajax libraries on our VisualForce page. Then on the update method of the sortable function we are going to iterate through our list and update the sort order id relative to its order within the array. You may also notice that there is a call to toggle within the script. I just created a div to provide feedback to the user that the list was updating and to please wait.

 
 


In this example we realy do not care what the previous sort order was and so we are just re-ordering the records each time. If you were filtering records out etc and wanted to just update the order of certain records while maintaining the exiting ones, you would just save the existing order into an array and then use that collection of values durring the update process. You can also add touchable events to the list which would provide the same functionality to users on an IPad or other touch based device.

8 comments:

  1. Hello, I just wanted to thank you for this tutorial! Could you do some more jQuery UI with salesforce tutorials? keep up the good work!

    ReplyDelete
  2. Thanks Mike. I have a few topics lined up but have been swamped the past few months on projects. Anything in particular you would like to see?

    ReplyDelete
  3. Very cool, and good walkthrough!

    ReplyDelete
  4. Hi ,I have been working on a similar scenario,Your post helped me a lot..Thanks..:)

    But I am facing an issue,in the update function where we are creating an instance of the Object,and passing the id and the field to be updated ,
    {!!!----product.id = result[i]----!!!!}
    the actual Salesforce record id's are not being passed. Hence records are not being updated.
    Could you please help me fix the issue...??

    ReplyDelete
    Replies
    1. Sure. if you want to shoot me some code I would be glad to look at it. First question I have is when you look at the rendered html are your record id's being applied to the LI tags in the list?

      Delete
    2. Hi ,Sorry for the delay in replying.,that part of work was freezed for sum time..I cud fix it myself by a little modification..result[i] returns the HTML id,all I had to do was dynamically assign Salesforce Id as the HTML Id.

      But I got another problem now..!! I am sure u can help me with this.
      Sortable works excellent with the code u have given up here, until an AJAX call happens on the list being retrieved. Once the list is modified with an AJAX call sortable stops working.
      Could u please help..??

      P.S :My code is almost same as above,just that the list has different name and returns a wrapper class items and further, has one select list which onchange (AJAX) filters the items with out refreshing the entire page.


      Delete
  5. She came to visit her neighbor and she showed me this site knowingall slots and sat with her all day at the computer, now we argue who will get the money faster. Cool site design a lot of slots and slot machines

    ReplyDelete