Get NSComboBox to work like an HTML Combo - cocoa

Is it possible to have the values displayed in an NSComboBox be different from what is retrieved with objectValueOfSelectedItem?
So, it will display an object's string name but it's int recordId is returned instead of the string name? Other than using selected index and getting it from the source array, of course...

Are you looking for NSPopUpButton? It's more similar to an HTML <select>. An NSComboBox lets users directly type in new values, as well as pick them from a list, but you can't do that with plain HTML.
In any event, you should be able to give either control a bunch of custom objects that have a recordid property, and then retrieve the selection using [[comboBox objectValueOfSelectedItem] recordid]. You'll also need to write a custom formatter for these objects.

PopupButton is it.
objectValueOfSelectedItem wont work for me since it returns what is displayed and I want a string displayed but int returned.

Related

How do I get Dropdown SelectedItem text in view model in ASP.NET MVC 3

This is my Razor view in ASP.NET MVC3 Application
#Html.DropDownListFor(m => m.Leave_Type, Model.Leave_Types, new { #class = "input-append" })
On POST when I read Model.LeaveType, it returns selected value. I want to get the selected text instead, how do I get it? I appreciate any help. Thanks a lot in advance!!
The answer is.. it depends. A drop down is just a standard HTML dropdown control, and it can only post the selected value. It will never post the text, and there is no way to change that.
Do you need the selected value as well? If so, then you can't get both like that. You would have to look up the Leave_Type text in the Leave_Types object based on the selected value returned.
If you don't need the Leave_Type, then you can simply use the text for both the value and text.
Another option might be to write some javascript that copies the selected item text to a hidden field whenever the value of the dropdown changes. Then that hidden field will be posted and you can check that.
I would, however, simply look up the value in whatever method you use to create the Leave_Types collection in the first place.

Linq2SQL and IQueryable

Still new to LINQ2SQL so please forgive my ignorance ...
I have one user component which includes one Textbox and one button. The component is used as a generic ListOfValue Filter.
The component has one function to set a IQueryable which is passed to a form that is opened when the user clicks a button on the control.
The form consists of a grid (c1flexgrid) which is filled with the data from the IQueryable. There is a bindingsource on the form that gets the IQueryable as datasource.
The user can select inside the grid and after he selected an entry the dialog is closed and the selected row (or better the selected LINQ2SQL object from the row) is passed back to the control.
On this control i want to show one specific field out of that selected object. The name of that field is passed in to the user control as string.
My problem is, i don't know how to get that field data from an "generic" LINQ2SQL object.
In debugger i can see, that the selected object is of a specific enity type (corresponding to the query)
Probably somthing similar to
Workaround for lack of 'nameof' operator in C# for type-safe databinding?
but just the opposit way :)
Any help would be very welcome
I'm assuming in your IQueryable you don't know at design time the type T. If this is correct, you need to use reflection to get the value you want.
var value = typeof(T).GetProperty("MyField").GetValue(instance, null);
Alternatively, cast the instance to a common base type that implements your field.
CommonBase castInstance = (CommonBase)instance;
var value = castInstance.MyField;

Struts2 radio buttons internationalization

I have a JSP page that allows user to choose a color using radio button. The available colors are in the following enum
enum Colors{RED, BLUE, GREEN};
and I'm displaying them using s:radio tag
<s:radio name="selectedColor" list="#com.example.Colors#values()" ></s:radio>
This works fine and displays the list of all colors present in enum. Now I want to internationalization this list so that the lables with each radio button should be displayed using getText() in listValue
<s:radio name="selectedColor" list="#com.example.Colors#values()" listValue="getText(XYZ)"></s:radio>
what should I write in place of XYZ to get the desired result?
I'd probably do the I18N lookup on the business side of things (outside of the view layer) and just pass in a list of value/text objects, or a map of label/value pairs (I usually just use a map to avoid having to create a transfer object).
IIRC the listValue attribute would be a property name on the list object; I don't recall if you could actually specify a method like that.

Ajax Autocomplete field: display one value, but use another?

I've got an autocompleter in a CakePHP app that provides a dropdown list of neighborhoods to attach to a place. I'm pulling from a table 'neighborhoods' with name and id fields. I want the user to see the name of the neighborhood, and I want the app to see the id of the neighborhood and insert it into the places table. How can I make this happen?
Another way of doing this would be to storing the id. Then when a user presses submit you replace the input field's value with your stored value.
assign the correct (hidden) value to a hidden input field
This type of functionality is normally achieved with a combobox, which is like an autocomplete, with an underlying select elememt, see here

How do I use an NSFormatter subclass with an NSPopUpButton

I want to use an NSFormatter subclass to format the contents of an NSPopUpButton I'm using to display a list of choices.
Basically I have an NSArray of MyObjects which is bound to the NSPopUpButton via the Content Values binding and I want to display something in the pop up menu other than what is returned by -description. I don't really want to use a transformer because then I have to create an entirely new array and transform each object into a string before adding it to the new array.
Using setFormatter: on the NSPopUpButton itself via either IB or code doesn't work, I suspect because only the formatter for the individual cell is applied to the items in the list.
Is there an easy way to set a formatter for all the cells of the NSPopUpButton? Basically I want to just be able to set it once and forget about it.
Typically you'd bind your popup button to an array controller that contains custom model objects (through the content binding), and use the content values binding to specify a keypath on those object with the string you want to use as the title.
From what I understand, you have an array of plain strings you want to use as the data source, only you want to display a different string for the title, right?
I'm not sure why making a data transformer wouldn't work if set up like the above-- or maybe I'm misunderstanding something? You'd have one binding to the actual string, and another binding to the string using a transformer, but both of them would be using the same array controller. Your other options are creating a model class to wrap around the strings and provide a title property, or creating a category on NSString that returns your title to use as the display value binding. In all of these cases you can create your own NSFormatter in code and use it to return the correct string title.

Resources