简体   繁体   English

如何从动态创建的输入更新总和?

[英]How to update sum from dynamically created inputs?

I have multiple inputs that are created dynamically (Can add or delete inputs).我有多个动态创建的输入(可以添加或删除输入)。 and a text field that shows the sum of the inputs.和一个显示输入总和的文本字段。 How can I attach observable dynamically and how can my text subscribe to all of them?如何动态附加 observable 以及我的文本如何订阅所有这些?

Since I am not able to figure out how to create and attach observables to field or how to access them using their id maybe.因为我无法弄清楚如何创建和附加 observables 到字段,或者如何使用他们的 id 访问它们。 I don't have much code to post!我没有太多代码要发布! Any input ?任何输入?

Function has loop and returns list of obeservables函数具有循环并返回可观察的列表

    var source = Rx.Observable.fromEvent(cellVal, 'keyup',(evt) => evt.target.value).startWith(cellVal.value);
    arr.push(source); 
    }
    return arr;


    values = funct() //calls function that returns array of obervables
    var example = Rx.Observable.combineLatest(values);
    example.subscribe(val => {
        console.log('Sum:', val);
    });

可以创建或删除输入,并相应地更新总和

If you want to add/delete observables over time to an array that means you need a state (because of add/delete).如果您想随时间向数组添加/删除 observable,这意味着您需要一个状态(因为添加/删除)。 If you want to stick to rxjs the only non-side-effect way I know is to use the scan operator.如果您想坚持使用 rxjs,我所知道的唯一无副作用的方法是使用scan运算符。 Following code shows an implementation:以下代码显示了一个实现:

Interface界面

interface MappableObservable <T>{
  key: number,
  observable: Observable<T>
}

Functions职能

// Function that adds to an array of MappableObservables
const addToState = (update: MappableObservable<string>) => (state: MappableObservable<string>[]): MappableObservable<string>[] => 
  [...state, update];

// Function that deletes all items with given key from an array of MappableObservables
const deleteFromState = (key: number) => (state: MappableObservable<string>[]): MappableObservable<string>[] =>
  state.filter(item => item.key !== key);

// Function that executes the above add or delete function inside a scan
const scanFn = (state: MappableObservable<string>[], fn: (state: MappableObservable<string>[]) => MappableObservable<string>[]) =>
  fn(state)

Execution执行

const DEFAULT_MAPPABLE_OBSERVABLE_STATE: MappableObservable<string>[] = [];
const add$: Subject<MappableObservable<string>> = new Subject();
const delete$: Subject<number> = new Subject();

const source$: Observable<string[]> = merge(
  add$.pipe(map(addToState)),
  delete$.pipe(map(deleteFromState))
).pipe(
  scan(scanFn, DEFAULT_MAPPABLE_OBSERVABLE_STATE),
  map(state => state.map(mappableObservable => mappableObservable.observable)),
  switchMap(observables => combineLatest(observables))
)

Add or delete some observable to this source$添加或删除一些可观察到的这个源$

add$.next({key: 1, observable: of('uno')}) // Output: 'uno'
add$.next({key: 2, observable: of('duo')}) // Output: 'uno', 'duo'
add$.next({key: 3, observable: of('tri')}) // Output: 'uno', 'duo', 'tri'
add$.next({key: 2, observable: of('duo-duo')}) // Output: 'uno', 'duo', 'tri' 'duo-duo'
delete$.next(2); // Output: 'uno', 'tri'

FYI: The observables only directly if you replay them or use of(...) like i did in this example.仅供参考:只有在您像我在这个例子中所做的那样重放它们或使用 (...) 时才能直接观察到。 If you have non replayed or instant observables they will only emit, when they all newly complete如果您有不可重播或即时的可观察对象,它们只会在它们全部完成后才会发出

Running stackblitz运行堆栈闪电战

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM