I have a ChoiceBox that I want to set with an ObservableList. In JavaFX I might do:
ObservableList genres = FXCollections.observableArrayList(
"Chamber",
"Country",
"Cowbell",
"Metal",
"Polka",
"Rock"
);
choiceBox.setItems(genres);
But I can't find any equivalent examples in ScalaFX. What would that look like?
ScalaFX prides wrappers for JavaFX collections that are compatible with Scala collections. You can do following:
var genres = ObservableBuffer(
"Chamber",
"Country",
"Cowbell",
"Metal",
"Polka",
"Rock"
)
choiceBox.items = genres
You will find more example in ProScalaFX project: https://github.com/scalafx/ProScalaFX
Related
I'm just looking for an idea for converting this pseudo code into a reactive style.
var records = new ArrayList<>();
var query = new Query();
var results = query.executeQuery();
records.addAll(results.getRecords());
while (results.hasMore()) {
query = new Query(results.offset())
deals = hubspotQuery.executeQuery(Deals.class);
records.addAll(results.getRecords());
}
The idea is to collect all records into a Flux
Here is one possible solution. Maybe there is others, but this one is simple.
This is not real code, but it describe the logic.
Flux<Records> query = createFluxQuery();
query.expand(record -> (record.hasMore()) ? createFluxQuery(record.offset) : Flux.empty());
I've a List that contains a records below:
name:apple
category:fruit
name:choysum
category:vegetable
name:chicken
category: poultry
name: lamb
category: meat
...
I need to order this list by category below:
vegetable
fruit
poultry
meat..
Could someone please help me how above is possible using LINQ. I'm thinking of creating a another property called ordinal and tag as 1,2,3,4 for above categories. Then order by ordinal.
Yes, you could do something like:
var orderedCategories = new List<string> { "fruit", "vegetable", "poulty", "meat" };
var ordered = list.OrderBy(x => orderedCategories.IndexOf(x.Category))
.ToList();
If you have lot of categories, you might want to use a Dictionary<string, int> (or a Dictionary<Category, int> if you have a separate class for categories).
I have an xml file that looks something like this
<questions>
<question>
<text>What color is an orange?</text>
<answer>blue</answer>
<answer>yellow</answer>
<answer>orange</answer>
</question>
<question>
<text>What color is a banana?</text> ...
I've managed to figure out how to read attributes and values into the properties using the public methods for the object, but how would i get a "Question" object that would contain "Answer" objects, would it be better to just serialize than use linq-to-xml
This is using linq:
var data = from query in questionData.Descendants("question")
select new Quiz.Question
{
QuestionTitle = (string)query.Attribute("title"),
QuestionText = query.Element("text") != null ? query.Element("text").Value.Trim() : string.Empty,
QuestionImage = query.Element("image") != null ? query.Element("image").Attribute("src").Value : string.Empty
...
in linq how do I go about serializing another node as another object, say i have a list of "answer" object in "question"?
You can use serialization for this, but if you want to have a totally custimizable way of doing this I would recommend this:
In Question class:
public static Question FromXmlElement(XElement el)
{
return new Question
{
Text = el.Element("Text").Value,
Answers = el.Elements("Answer").Select(a=>a.Value);
};
}
and when you want to read:
var xdoc = XDocument.Parse(xml);
var questions = xdoc.Element("Questions").Elements("Question")
.Select(e=> Question.FromXmlElement(e));
from inside the FromXmlElement you can call same method of another complex type if your class has a property of a complex type and so on.
I'm trying to use the WebGrid html helper in ASP.NET MVC 3 to autogenerate the columns according to the information found in the ModelMetadata. For example the code in a view that accepts a list of objects would be:
var grid = new WebGrid(Model);
#grid.GetHtml(columns: ViewData.ModelMetadata.Properties.Single.Properties
.Select(p => grid.Column(
columnName: p.PropertyName,
header: p.ShortDisplayName
)));
This actually works like a charm (I was surprised it was that easy actually). What happens here is that from the properties of the model I use the ShortDisplayName as the column's header.
The problem? I need to apply a default format to all columns. Basically I want to use the Html.Raw extension for all the data that my grid will display. An attempt would be something like that :
var grid = new WebGrid(Model);
#grid.GetHtml(columns: ViewData.ModelMetadata.Properties.Single.Properties
.Select(p => grid.Column(
columnName: p.PropertyName,
header: p.ShortDisplayName,
format: (item) => Html.Raw(GetPropertyValue(item, p.PropertyName))
)));
where the method GetPropertyValue would read the value of the property using reflection or whatever (I need to remind here that item is dynamic and its value is actually the object that is being displayed in the current row).
Is there any better way to do this?
Thanks,
Kostas
I suggest you looking into MVCContrib Grid project: http://mvccontrib.codeplex.com/wikipage?title=Grid
Don't know if you still need some help with this question, but I had a problem just like yours and that's what I did to solve it:
I Used a Foreach loop to iterate through the properties
Filled a variable with the name of the property
Formated the column
The code that I got was something like this:
var columns = List<WebGridColumn>();
foreach (var p in ViewData.ModelMetadata.Properties.Single.Properties) {
String propertyName = p.PropertyName;
columns.Add(grid.Column(p.PropertyName, p.ShortDisplayName, format: item => Html.Raw(GetPropertyValue(item.Value, propertyName))));
}
#grid.GetHtml(columns: columns.ToArray());
And that's how I get the property's value:
public static object GetPropertyValue(object obj, String propertyName)
{
if (propertyName.Contains("."))
{
int index = propertyName.IndexOf(".");
object prop = obj.GetType().GetProperty(propertyName.Substring(0, index)).GetValue(obj, null);
return GetPropertyValue(prop, propertyName.Substring(index + 1));
}
return obj.GetType().GetProperty(propertyName).GetValue(obj, null);
}
I really don't know if this is the best way, but it's working pretty good for me.
OK, another LINQ question. How do I do an "IN" condition using LINQ. I have an IEnumerable list of myObject and want to do something like myObject.Description in('Help', 'Admin', 'Docs'). How can I accomplish this? Thanks
IN in sql is equivalent is Contains in LINQ
string[] countries = new string[] { "UK", "USA", "Australia" };
var customers =
from c in context.Customers
where countries.Contains(c.Country)
select c;
Use Contains on a collection:
string[] descriptions = { "Help", "Admin", "Docs" };
var query = from foo in list
where descriptions.Contains(foo.Description)
select ...;
(For larger collections, a HashSet<T> might be better.)