Page 1 of 2

Sorting in Mobile app

PostPosted: Mon Feb 11, 2013 11:38 am
by vincentc
Hello,

Lists on mobile side are sorted by table Ids (by default). Is there a way to change the field on which I would like to sort ?

By example, I would like to sort the companies list by company_name asc but it seems to not work in mobile.

Thank you.

Re: Sorting in Mobile app

PostPosted: Mon Feb 11, 2013 12:30 pm
by patrick
On mobile you don't have a real database that understands a SQL "Order By" clause. You can sort the foundset by using a sort function. Something like

Code: Select all
function fsSort(record1, record2) {
   var result = 0;
   if (record1.company_name > record2.company_name) {
      result = 1;
   } else if (record1.company_name < record2.company_name) {
      result = -1;
   }
   return result;
}

fs.sort(fsSort);

Re: Sorting in Mobile app

PostPosted: Mon Feb 11, 2013 4:56 pm
by vincentc
Hello patrick,

Thank you very much for your answer.

This solution doesn't work for me, but I think this is because my Mobile form is based on the "entites" table. To access my field, I have to write "entites_to_companies.company_name".

And I think that this method of sorting doesn't work with related fields, right ?

Re: Sorting in Mobile app

PostPosted: Mon Feb 11, 2013 7:06 pm
by patrick
Why shouldn't this work:

var record = foundset.getSelectedRecord(); // your entity
var fs = record.entites_to_companies; // the foundset you want to sort

fs.sort(fsSort);

Re: Sorting in Mobile app

PostPosted: Mon Feb 18, 2013 11:02 am
by vincentc
We have to do this on the mobile side ? Sorry, I can not make it work :(

Re: Sorting in Mobile app

PostPosted: Mon Feb 18, 2013 1:38 pm
by patrick
Can you show the code that you are using now?

Re: Sorting in Mobile app

PostPosted: Thu Feb 21, 2013 4:02 pm
by vincentc
I don't use relation anymore, but just a table companies, so in theory it is very simple. I use the code that you gave me first. the field's name is "comp_name", so in my mobile side I have :

Code: Select all
function fsSort(record1, record2) {
   var result = 0;
   if (record1.comp_name > record2.comp_name) {
      result = 1;
   } else if (record1.comp_name < record2.comp_name) {
      result = -1;
   }
   return result;
}

and in my onShow() method :
fs.sort(fsSort);

Re: Sorting in Mobile app

PostPosted: Thu Feb 21, 2013 11:24 pm
by patrick
hm. Not sure if that is too late. What if you do this outside of the form you want to show sorted and then say forms.xy.controller.showRecord(sortedFs) to show it?

Re: Sorting in Mobile app

PostPosted: Tue Mar 05, 2013 10:39 am
by vincentc
I don't success to make it work ... "The property controller is private".

Nobody else has a solution ?

I think I will wait the final version of Servoy 7 to try again. Thank you so much for your answers.

Re: Sorting in Mobile app

PostPosted: Tue Mar 05, 2013 11:32 am
by jcompagner
that means that you accessed the "controller" property of a form that has that controller encapsulated (see the forms properties)
You can change this to public

But where did you access the controller property in this sort problem?

Re: Sorting in Mobile app

PostPosted: Tue Mar 05, 2013 12:20 pm
by vincentc
Ok I changed the encapsultation but it doesn't work. The problem is that I need to make a sort on the FirstForm of my solution (companies). In this case, where it is better to call the forms.companies.controller.showRecord(sortedFs)

Johan, for you, is it possible to sort a list in a Mobile app ? if yes, how ?

Re: Sorting in Mobile app

PostPosted: Tue Mar 05, 2013 2:51 pm
by jcompagner
that should all just work
in companies:

foundset.sort(mysortfunction)

that should sort and display the order in a form. If you have a solution where you don't see that, create a case with that solution.

ofcourse

var justafoundset = xxxxx;
justafoundset.sorT(mysortfunction)
controller.showRecords(justafoundset)

should also just work

Re: Sorting in Mobile app

PostPosted: Tue Mar 05, 2013 6:21 pm
by jgarfield
Sorting a foundset via a function seems cool, but is something I haven't heard of before... Is this Mobile only, or can we do this anywhere?

Re: Sorting in Mobile app

PostPosted: Tue Mar 05, 2013 6:38 pm
by Joas
jgarfield wrote:Sorting a foundset via a function seems cool, but is something I haven't heard of before... Is this Mobile only, or can we do this anywhere?

That can be done anywhere, check the sample code for JSFoundset.sort(function).
I think this is possible since 6.0.

Re: Sorting in Mobile app

PostPosted: Tue Mar 05, 2013 6:52 pm
by vincentc
Finally, I understood why it didn't work !
Sage CRM add some space characters (" ") in the field values of the database ... That is why the sorting wasn't working. I had to trim the values in the ws_read, and now it works like a charm.
Thank you for your answers !