Ewl » Canonical The canonical version of EWL Read More
Clone URL:  
Pushed to 2 repositories · View In Graph Contained in tip and canonical

In SelectList, disabled Chosen when the placeholder item is not first.

Changeset 975f523e937b

Parent c9909248b98d

by Profile picture of William GrossWilliam Gross

Changes to one file · Browse files at 975f523e937b Showing diff from parent c9909248b98d Diff from another changeset...

 
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
352
353
354
 
331
332
333
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
@@ -331,24 +331,30 @@
  return "";     var placeholderItem = items.SingleOrDefault( i => i.IsPlaceholder ); - return "$( '#{0}' ).chosen( {{ {1} }} ){2};".FormatWith( - selectControl.ClientID, - StringTools.ConcatenateWithDelimiter( - ", ", - placeholderItem != null && placeholderItem.IsValid ? "allow_single_deselect: true" : "", - placeholderItem != null - // Don't let the placeholder value be the empty string since this seems to confuse Chosen. - ? "placeholder_text_single: '{0}'".FormatWith( - placeholderItem.Item.Label.Any() ? HttpUtility.JavaScriptStringEncode( placeholderItem.Item.Label ) : " " ) - : "", - "search_contains: true", - "width: '{0}'".FormatWith( width.HasValue ? width.Value.ToString() : "" ) ), - // Do this after .chosen since we only want it to affect the native select. - placeholderItem != null - ? ".children().eq( {0} ).text( '{1}' )".FormatWith( - items.IndexOf( placeholderItem ), - HttpUtility.JavaScriptStringEncode( placeholderItem.Item.Label ) ) - : "" ); + + // Chosen’s allow_single_deselect only works if the placeholder is the first item. + var chosenLogic = placeholderItem == null || placeholderItem == items.First() + ? ".chosen( {{ {0} }} )".FormatWith( + StringTools.ConcatenateWithDelimiter( + ", ", + placeholderItem != null && placeholderItem.IsValid ? "allow_single_deselect: true" : "", + placeholderItem != null + // Don't let the placeholder value be the empty string since this seems to confuse Chosen. + ? "placeholder_text_single: '{0}'".FormatWith( + placeholderItem.Item.Label.Any() ? HttpUtility.JavaScriptStringEncode( placeholderItem.Item.Label ) : " " ) + : "", + "search_contains: true", + "width: '{0}'".FormatWith( width.HasValue ? width.Value.ToString() : "" ) ) ) + : ""; + + // Do this after .chosen since we only want it to affect the native select. + var placeholderTextLogic = placeholderItem != null + ? ".children().eq( {0} ).text( '{1}' )".FormatWith( + items.IndexOf( placeholderItem ), + HttpUtility.JavaScriptStringEncode( placeholderItem.Item.Label ) ) + : ""; + + return ( chosenLogic + placeholderTextLogic ).Surround( "$( '#{0}' )".FormatWith( selectControl.ClientID ), ";" );   }     FormValue FormValueControl.FormValue { get { return formValue; } }