小编wol*_*olf的帖子

单击子元素时如何防止父div失去焦点

<parent onBlur={function}>
  <child>
    <child>
    </child>
  </child>
</parent>
Run Code Online (Sandbox Code Playgroud)

在这样的结构中,我想捕捉父元素是否失去焦点。我想在父 div 之外单击时触发该行为。但是,当我单击子元素时,父元素也会失去焦点。您可以将此结构视为下拉菜单。有没有办法在无状态反应组件内部实现这一点?我不能使用 jquery 或其他库。

html javascript reactjs

6
推荐指数
1
解决办法
1720
查看次数

如何忽略mongodb条件查询中的空参数

 List<Property> properties;
 Criteria criteria = new Criteria();

criteria.andOperator(
            Criteria.where("published").is(true),
            Criteria.where("location.district").regex(model.getLocation(),"i").orOperator(Criteria.where("location.city").exists(true).regex(model.getLocation(),"i")),
            Criteria.where("basicInfo.propertyType").is(model.getPropertyType()),
            Criteria.where("priceInfo.price").gte(prices[0]).lte(prices[1]),
            Criteria.where("bedspace").gte(model.getAdult()).orOperator(Criteria.where("bedspace").gte(model.getChild())),
            Criteria.where("unavailableDates").ne(model.getCheckinDate()).andOperator(Criteria.where("unavalibleDates").ne(model.getCheckoutDate())),
            Criteria.where("location.coords").within(box));

    Query query = new Query(criteria);
    properties = mongoTemplate.find(query, Property.class);
Run Code Online (Sandbox Code Playgroud)

我的代码基本上是这样工作的。我的目标是创建搜索机制,用户将输入位置信息来开始搜索,然后通过添加额外的过滤器自定义他/她的搜索。我将它们放入 andOperator 中,因为我希望将此搜索指定为位置参数。

例如:您搜索德国,然后添加价格过滤器,例如 0-100 之间,然后搜索应该执行:返回来自德国的价格在 0-100 之间的房屋

但有一个问题。当用户进入位置查询模型时,仅包含位置参数,这意味着查询模型中的其他参数为空,当用户进入andOperator时,会出现空指针异常。我希望它忽略空参数并继续处理其他参数。我怎样才能做到这一点?

我尝试添加 ne(null) 字段,但没有帮助

 Criteria.where("location.coords").ne(null).within(box));
Run Code Online (Sandbox Code Playgroud)

提前致谢,

java mongodb mongodb-query spring-boot

5
推荐指数
1
解决办法
6309
查看次数

如何积累观测值

我应该定义一个函数,它将返回 IObservable<'u>

accumulate : ('t -> 'a -> 't * 'u option) -> 't -> IObservable<'a> -> IObservable<'u>
Run Code Online (Sandbox Code Playgroud)

这样我的函数 ft obs' 将 obs 的可观察事件累积到类型为 't 的累加器中,并在观察事件 'a' 的 'snd (f acc a)' 计算结果为 'Some u' 时发出可观察事件 u。

到目前为止我已经实现了以下功能:

let accumulate (f:'t -> 'a -> 't * 'u option) t obs = 
 Observable.scan (fun _ x -> snd (f t x)) None obs
Run Code Online (Sandbox Code Playgroud)

我真的不明白这个可观察的扫描是如何工作的,在这种情况下我的函数返回 IObservable<'u option> 。我该如何解决这个问题?我走在正确的轨道上吗?

f# functional-programming

4
推荐指数
1
解决办法
378
查看次数

F#如何展平二进制搜索树

我有一棵树,结构如下:

type 'a Tree =| Leaf of 'a| Branch of 'a Tree * 'a Tree
Run Code Online (Sandbox Code Playgroud)

我在树上使用延续传递样式的尾递归并尝试将其展平.

let rec loop tree k acc = 
  match tree with
  | Leaf v -> v :: acc
  | Branch (tl,tr) -> loop tl (loop tr k) acc
loop xs id []


(Branch (Branch (Leaf 1.0,Leaf 2.0),Branch (Leaf 3.0,Leaf 4.0)))
Run Code Online (Sandbox Code Playgroud)

这只返回[1.0]

但是我只获得树中的第一片叶子,我的功能不适用于整棵树.我怎样才能做到这一点?

recursion f# functional-programming tail-recursion continuation-passing

3
推荐指数
1
解决办法
82
查看次数

如何使用memoize重复序列

let memoize (sequence: seq<'a>) =
    let cache = Dictionary()
    seq {for i in sequence -> 
         match cache.TryGetValue i with
         | true, v -> printf "cached"
         | false,_ -> cache.Add(i ,i)
    }
Run Code Online (Sandbox Code Playgroud)

我将在此函数中调用memoize函数:

let isCached (input:seq<'a>) : seq<'a> = memoize input
Run Code Online (Sandbox Code Playgroud)

如果给定的序列项被缓存,则应该打印缓存,否则它将继续向缓存添加序列值.

现在我有类型的问题.

当我尝试像这样调用我的函数时:

let seq1 = seq { 1 .. 10 }
isCached seq1
Run Code Online (Sandbox Code Playgroud)

它抛出一个错误

"The type int does not match the type unit"
Run Code Online (Sandbox Code Playgroud)

即使我返回printfn,我希望我的函数能够工作.是否有可能实现这一目标?虽然向缓存添加值是否适合为元组赋予相同的值?

例如:

| false,_ -> cache.Add(i ,i)
Run Code Online (Sandbox Code Playgroud)

functional-programming

1
推荐指数
1
解决办法
36
查看次数