Sorting is not working when it is required field

Oct 4, 2012 at 5:26 PM
Edited Oct 4, 2012 at 6:58 PM

Hi,

I applied the sorting to my lookup fields, its does the sorting but it defaults to default value not the according to the sorting order.Please find the below example.

When the Field is required the sorting shows default value as Closed

1.Open

2.Closed

3.InProgress

When the field is not required the sorting is working fine

 

 

 

Below is the code i am using

<script type="text/javascript" src="../../jquery-1.7.1.min.js"></script>
<script type="text/javascript" src="../../jquery.SPServices-0.7.1a.js"></script>
<script type="text/javascript">

$(document).ready(function() {
$().SPServices.SPFilterDropdown({
relationshipWebURL: "", // The source list for the Recommendation site Column is in the root site
relationshipList: "Status",
relationshipListColumn: "Title",
relationshipListSortColumn: "SortOrder",
columnName: "Status",
CAMLQuery: "<Neq><FieldRef Name='Title' /><Value Type='Text'></Value></Neq>", // Get all values by specifying a non-blank Title
debug: true // Debug mode on while we're developing
});
});
</script>

Thanks in Advance
Bindu

Oct 4, 2012 at 6:58 PM
Edited Oct 4, 2012 at 6:59 PM

Thanks

Bindu

Coordinator
Oct 5, 2012 at 4:40 PM

Bindu:

Can you show some deatils about the Status list? Does it just have the columns Title and SortOrder? What do those values look like?

M.

Oct 5, 2012 at 5:16 PM

Marc,

 

Status list contains the Title field and it has the following values

Open

Closed

InProgress

 

SortOrder column also exists in the list.When the field is not required sorting is working as expected but when the field is required sorting works but default value will according to Alphabet.

 

Thanks

Bindu

Coordinator
Oct 6, 2012 at 1:57 AM

What kind of column is SortOrder and what values does it contain?

M.

Oct 8, 2012 at 8:00 PM

SortOrder is just single line of text,it just contain 1,2,3,...values in it

Coordinator
Oct 10, 2012 at 3:57 PM

I'm not sure what's going on. It seems like you have everything in place correctly.

M.

Oct 10, 2012 at 4:09 PM

ya looks like sorting gets applied but default value gets assigned.Do u have any suggestions for doing some trials

Coordinator
Oct 10, 2012 at 4:17 PM

It's hard to say. The first thing I'd do is look at how the page loads with no script applied. The add the script and step through it to see what it's doing.

M.

Oct 10, 2012 at 6:00 PM

Sorting works as expected after applying the script ,this fails only when the field is required

Thanks

Bindu

Oct 16, 2012 at 2:08 PM

Marc,

Could please suggest me some way where I can work on sorting,it is very important for me to fix.Please suggest me some solutions by which I can fix

Thanks in Advance

Bindu

Coordinator
Oct 17, 2012 at 4:03 AM

Bindu:

I'm having a bit of trouble following what problem you're actually having, so I'm not sure what to suggest.

M.

Oct 17, 2012 at 2:25 PM

Marc,

Here is my problem

1.I have a list called Status which has two columns 'Title' and 'SortOrder' both are Single line of text fields.

2.I have another list which has look up field 'Status',which is in the relation ship with the above list 'Status' title field.

3. For sorting the status look up field i used the below code

<script type="text/javascript" src="../../jquery-1.7.1.min.js"></script>
<script type="text/javascript" src="../../jquery.SPServices-0.7.1a.js"></script>
<script type="text/javascript">

$(document).ready(function() {
$().SPServices.SPFilterDropdown({
relationshipWebURL: "", // The source list for the Recommendation site Column is in the root site
relationshipList: "Status",
relationshipListColumn: "Title",
relationshipListSortColumn: "SortOrder",
columnName: "Status",
CAMLQuery: "<Neq><FieldRef Name='Title' /><Value Type='Text'></Value></Neq>", // Get all values by specifying a non-blank Title
debug: true // Debug mode on while we're developing
});
});
</script>

4.Sorting works as expected .When i made the Status field as mandatory field sorting is not working as expected.Instead of providing the below order

1.Open 2.Closed 3.In Progress it provides as 1.Closed 2.Open 3.In Progress

I am facing the sorting issue when the look up field is marked as mandatory field.

Let me know for any corrections

Thanks in Advance

Bindu

Oct 17, 2012 at 3:54 PM
Edited Oct 17, 2012 at 3:56 PM

I've not tried the CAML query syntax you used above but I'm wondering if that might be causing some conflict.  Have you tried modifying the CAML query to ignore null values instead of trying to ignore an empty string?  For instance....

CAMLQuery: "<IsNotNull><FieldRef Name='Title' /></IsNotNull>",

It may be a shot in the dark but worth a try.

Geoff

Oct 17, 2012 at 7:44 PM
Edited Oct 17, 2012 at 7:49 PM

Geoff,

Thanks for the response

I tried changing the caml query but still it did not work.I think i observe here is when the field is made as Mandatory field its sorts according to the Sortorder but the dropdown value is defaulted according to alphabetic.I am not able to attach a screen shot here.

 

Thanks

Bindu

Oct 18, 2012 at 12:20 PM

OK, I see what's happening now. I was misunderstanding your issue as described.  I originally thought you were saying the sorting was wrong when the 'Status' select item is required.  I see that's not the case.  The sorting works correctly for both cases (status required or optional).  When the 'Status' field is required and it is a select field, it will default to something.  In this case, it appears SharePoint is taking over and providing the default as the first item alphabetically.  In this case, that just happens to be the second choice according to your 'relationshipListSortColumn' argument.  To overcome this, you can force the selection to the first choice after all of the processing is complete, at the end of your '$(document).ready(function() ' (but still within it).  Add the below line to your NewForm.aspx code to force the 'Status' field to select your first choice as the default and override whatever SharePoint selected or you.

$("select[title='Status']").children().first().attr("selected", "selected");

Don't add that line to your "EditForm.aspx or it will update every record you edit!

Geoff

Oct 18, 2012 at 7:57 PM
Edited Oct 18, 2012 at 7:58 PM

Thanks Geoff,  it worked for me.

Can we also set our own default values to the look up's?

Thanks

Bindu

Oct 19, 2012 at 6:55 PM

When you're setting up properties for a "Lookup" field type, setting a default is not an option.  I believe SharePoint does this because it can't guarantee a user won't delete that value from the lookup list (thus breaking the default lookup relationship).  So, as far as I know, the only way to set a default on a lookup item is similar to the method above.  If it's the first item in the list, that method works as is.  If it's something in the middle of the list, you'll have to employ a more creative jQuery selector to get to the option you want.  for instance, if the option you want has a value attribute of 3, your line would be...

$("select[title='Status']").children("[value=3]").attr("selected", "selected");});

This will only work under two conditions:  Your lookup list isn't modified in such a way as to change the value of your target and the lookup list is <= 20 items.  Once you exceed 20 items on your lookup list, SharePoint changes the game and renders the control on the form as in input element with a small button next to it to render a dropdown list.  The process for selecting an item in that type of list is quite different.  Of course, with the SPServices tools, you have the option to override that as well using the '$().SPServices.SPComplexToSimpleDropdown' function.

You could try to use a ':contains' selector to get to the option you want. That would be independent of the value attribute but contains can give you multiple results.  For instance, if you lookup list includes the items "Open" and "Open Tomorrow", something like the following would return both items:

$("select[title='Status']").children("option:contains('Open')").attr("selected", "selected");

 

Oct 22, 2012 at 6:37 PM

Thanks Geoff , it was really helpful

 

Thanks

Bindu

Nov 13, 2012 at 4:45 PM

hi Geoff,

 

I applied above condition for showing the default value ,it works fine in the newform but in the editform even if i have selected some other value ,after editing the form it shows default values instead of selected values.

 

let me know if i need to include any condition for edit form.

 

Thanks in Advance

Bindu

Nov 13, 2012 at 7:27 PM

I mentioned above you shouldn't use this method in the EditForm.aspx because it will edit every record you open with that form.  There's no need to have a default value for an edit form.  You have that in the NewForm.aspx because you're dealing with a new record.  Once the record is saved, the defaults are no longer a factor.  The saved data is what it is.

Nov 13, 2012 at 7:44 PM

Thanks for the response.

When the look up fields are changed to required fields by default it gets the value assigned,can we show as blank and force the user to select the value by setting a field as similar to choice fields.

 

Thanks

Bindu

Coordinator
Nov 21, 2012 at 3:43 AM

See my reply on your other post.

M.