I have a <select> HTML element with 3 options and a <p> element. In the <p> element I want to print index of the currently selected item in <select>. E.g. if I select the first option, it should print 0, if I select the second option, it should print 1, and so on. How do I proceed from the minimal code, which is given below?
import Html as H exposing (Html)
import Maybe
import Signal as S exposing (Address, (<~))
type alias Model = { selected : Maybe Int }
model = { selected = Nothing }
type Action = NoOp | Select Int
update action model =
case action of
NoOp -> model
Select n -> { model | selected <- Just n }
view address model =
H.div []
[ H.select [] [ H.option [] [ H.text "0" ]
, H.option [] [ H.text "1" ]
, H.option [] [ H.text "2" ]
]
, H.p [] [ H.text <| Maybe.withDefault ""
<| Maybe.map toString model.selected ]
]
actions = Signal.mailbox NoOp
main = view actions.address <~ S.foldp update model actions.signal
There's a lot of different events in elm-html 2.0.0, but nothing relevant to the <select> HTML element. So you definitely need a custom event handler, which you can create using on. It has a type:
on : String -> Decoder a -> (a -> Message a) -> Attribute
The event that is triggered every time you select an option inside the <select> is called “change”. What you need is targetSelectedIndex from elm-community/html-extra which ustilizes a selectedIndex property.
The final code would look like this:
Updated to Elm-0.18
import Html exposing (..)
import Html.Events exposing (on, onClick)
import Html.Attributes exposing (..)
import Json.Decode as Json
import Html.Events.Extra exposing (targetSelectedIndex)
type alias Model =
{ selected : Maybe Int }
model : Model
model =
{ selected = Nothing }
type Msg
= NoOp
| Select (Maybe Int)
update : Msg -> Model -> Model
update msg model =
case msg of
NoOp ->
model
Select s ->
{ model | selected = s }
view : Model -> Html Msg
view model =
let
selectEvent =
on "change"
(Json.map Select targetSelectedIndex)
in
div []
[ select [ size 3, selectEvent ]
[ option [] [ text "1" ]
, option [] [ text "2" ]
, option [] [ text "3" ]
]
, p []
[ text <|
Maybe.withDefault "" <|
Maybe.map toString model.selected
]
]
main : Program Never Model Msg
main =
beginnerProgram { model = model, view = view, update = update }
You can run it in browser here https://runelm.io/c/xum
Related
I have following data in cosmosDb
[
{
accountId : "12453"
tag : "abc"
},
{
accountId : "12453"
tag : "bcd"
},
{
accountId : "34567"
tag : "qwe"
},
{
accountId : "34567"
tag : "xcx"
}
]
And desired output is something like this
[
{
accountId : "123453"
tag : {
"abc",
"bcd"
},
{
accountId : "34567"
tag : {
"qwe",
"xcx"
}
]
I tried Join, array and group by in multiple ways but didn't work.
Below query gives similar output but rather than count I am looking for an array of tags
select c.accountId, count(c.tag) from c where c.accountId = "12453" group BY c.accountId
Also tried below query
select c.accountId, ARRAY(select c.tag from c IN f where f.accountId = "12453") as tags FROM f
This doesn't return any tag values and I get below output but I need distinct accountId and when I try DISTINCT on accountId, it gives an error.
[
{
accountId : "12,
tag : []
}
........
]
Can someone please help with correct query syntax
As M0 B said, this is not supported. Cosmos DB can't combine arrays across documents. You need to handle this on your client side.
In OPA it's clear how to query against condition AND condition:
values := {
"value1": {
"a": "one"
},
"value2": {
"a": "one",
"b": "two"
},
"value3": {
"a": "one",
"b": "one"
}
}
goodValues = [name |
value = values[name]
value.a == "one"
value.b == "one"
]
So that goodValues here will contain value3 only.
But how to query condition OR condition, so that goodValues will contain all 3 values, because they have either value.a == "one" OR value.b == "one"?
Joining multiple expressions together expresses logical AND. To express logical OR you define multiple rules or functions with the same name. There are a couple different ways this can work. This is covered in the introduction to OPA: https://www.openpolicyagent.org/docs/latest/#logical-or.
Option 1: Comprehensions & Functions
The conditions that you want to express against the value can be factored into helper functions and then the comprehension query can refer to the function.
goodValues = [name |
value := values[name]
value_match(value)
]
value_match(v) {
v.a == "one"
}
value_match(v) {
v.b = "two"
}
Option 2: Incremental Rules
In OPA/Rego, incremental rules assign a set of values to a variable. The rule definition provides the logic to generate the set values. Unlike comprehensions, you can overload the rule definition (providing multiple with the same name) and express logical OR like the other answer explains.
# goodValues is a set that contains 'name' if...
goodValues[name] {
value := values[name] # name is in values
value.a == "one" # value.a is "one"
}
# goodvalues is a set that contains 'name' if...
goodValues[name] {
value := values[name] # name is in values
value.b == "two" # value.b is "two"
}
Found an ugly answer so far, via incremental set:
goodValues[name] {
value = values[name]
value.a == "one"
}
goodValues[name] {
value = values[name]
value.b == "one"
}
But what if that common condition value = values[name] gets more complicated? Will need to extract it to a separate variable (and iterate over that in every condition statement)? Any better solution?
Okay so I'm lost on this one. I have a list of objects. Each object as a non-unique ID in it. I want to group on this ID but for the life of me I can't figure out how to do this.
This is what I have
type fooObject = {
Id : int
Info : string
}
let fooObjects: fooObject list
The data might look something like this
[ { Id = 1 ; Data = "foo" } ; { Id = 1 ; Data = "also foo" } ; { Id = 2 ; Data = "Not foo" } ]
I would like something like
let fooObjectsGroupedById : fooObject list list
So the final result would look like this
[ [{ Id = 1 ; Data = "foo" } ; { Id = 1 ; Data = "also foo" } ] ; [{ Id = 2 ; Data = "Not foo" }]]
Use List.groupBy:
let groupById fooObjects =
List.groupBy (fun foo -> foo.Id) fooObjects
|> List.map snd
Given: myList = [request:request,actions:actions] where request is an object and actions is a map.
Trying to get something like this:
{data:[
{a:'a',b:'b',actions:[c:'c',d:'d']},..
]}
where a and b are request properties while c and d are actions are map entries.
using:
render(contentType:"text/json"){
data = array {
myList.each { obj->
rqst = {
obj.request
actions = {actions: obj.actions}
}
}
}
}
obviously syntax here is wrong... but perhaps close?
I think the following accomplishes what you were looking for. PropsHolder is a simple inner class with two (a, b) properties
def request = new PropsHolder()
request.a = "a"
request.b = "b"
def actions = [c: "c", d: "d"]
def myList = [request: request, actions: actions]
render(contentType:"text/json")
{
[data: [
a: myList.request.a,
b: myList.request.b,
actions: myList.actions
]
]
}
The json result looks like this when output to a web page:
{"data":{"a":"a","b":"b","actions":{"c":"c","d":"d"}}}
I wasn't quite sure with the [a, b, actions] collection if you were looking for a map or an array. Tough to tell from the output, I went with map.
I'm considering using Grails for my current project. I have a couple of requirements that I'm hoping I can do in Grails.
First, I have the following database table:
TagType
---------
tag_type_id
tag_type
Sample Data: TagType
--------------------
1,title
2,author
Based on that data, I need to generate a data entry form like this which
will save its data to another table.
Tile _ _ _ _ _ _ _ _ _ _ _
Author _ _ _ _ _ _ _ _ _ _ _
save cancel
Can I do that in Grails? Can you point me in the right direction?
Thanks!
More Details
I'm building a digital library system that supports OIA-PMH which is a standard for sharing metadata about documents. The standard states that every element is optional and repeatable. To support this requirement I have the following database design.
I need to generate the user GUI (data entry form) based primarily on the contents
of the TagType Table (see above). The data from the form then get's saved to
the Tags (if the tag is new) and Item_Tags tables.
Items
---------
item_id
last_update
Tags
--------
tag_id
tag_type_id
tag
TagType
---------
tag_type_id
tag_type
Item_tags
---------
item_id
tag_id
Sample Data: Items
------------------
1,2009-06-15
Sample Data: TagType
--------------------
1,title
2,author
Sample Data: Tags
------------------
1,1,The Definitive Guide to Grails
2,2,Graeme Rocher
3,2, Jeff Brown
Sample Data: Item_tags
-----------------------
1,1
1,2
1,3
I am not completely sure what you are asking here in regards to "save its data to another table", but here are some thoughts.
For the table you have, the domain class you'd need is the following:
class Tag {
String type
}
ID field will get generated for you automatically when you create the scaffolding.
Please add more information to your question if this is insufficient.
I'm really liking grails. When I first started playing with it a couple of weeks ago, I didn't realize that it's a full fledged language. In fact it's more than that. It's a complete web stack with a web server and a database included. Anyway, the short answer to my question is yes. You might even say yes, of course! Here's the code for the taglib I created:
import org.maflt.flashlit.pojo.Item
import org.maflt.flashlit.pojo.ItemTag
import org.maflt.flashlit.pojo.Metacollection
import org.maflt.flashlit.pojo.SetTagtype
import org.maflt.flashlit.pojo.Tag
import org.maflt.flashlit.pojo.Tagtype
/**
* #return Input form fields for all fields in the given Collection's Metadataset. Does not return ItemTags where TagType is not in the Metadataset.
*
* In Hibernate, the
*
* "from ItemTag b, Tag a where b.tag= a"
*
* query is a cross-join. The result of this query is a list of Object arrays where the first item is an ItemTag instance and the second is a Tag instance.
*
* You have to use e.g.
*
* (ItemTag) theTags2[0][0]
*
* to access the first ItemTag instance.
* (http://stackoverflow.com/questions/1093918/findall-not-returning-correct-object-type)
**/
class AutoFormTagLib {
def autoForm = {attrs, body ->
//def masterList = Class.forName(params.attrs.masterClass,false,Thread.currentThread().contextClassLoader).get(params.attrs.masterId)
def theItem = Item.get(attrs.itemId)
def theCollection = Metacollection.get(attrs.collectionId)
def masterList = theCollection.metadataSet.setTagtypes
def theParams = null
def theTags = null
def itemTag = null
def tag = null
masterList.each {
theParams = [attrs.itemId.toLong(),it.tagtype.id]
theTags = ItemTag.findAll("from ItemTag d, Item c, Tag b, Tagtype a where d.item = c and d.tag = b and b.tagtype = a and c.id=? and a.id=?",theParams)
for (int i=0;i<it.maxEntries;i++) {
out << "<tr>\n"
out << " <td>${it.tagtype.tagtype}</td>\n"
out << " <td>\n"
if (theTags[i]) {
itemTag = (ItemTag) theTags[i][0]
//item = (Item) theTags[i][1]
tag = (Tag) theTags[i][2]
//itemTag = (Tagtype) theTags[i][3]
out << " <input name='${it.tagtype.tagtype}_${i}_${itemTag.id}' value='${tag.tag}' />\n";
}
else
out << " <input name='${it.tagtype.tagtype}_${i}' />\n";
out << " </td>\n"
out << "</tr>\n"
}
}
}
}