简体   繁体   English

Angular:如何映射'mat-checkbox'数组以获取与选中复选框相关联的字符串

[英]Angular: How to map an array of 'mat-checkbox's to get strings associated with the checked checkboxes

To preface, I have found a solution that works for me in the situations I have tried it, but I am fairly new to javascript and RXJS and don't fully understand what the solution is doing so I can't be sure if it will work in every instance. 作为前言,我已经找到了一个适合我的解决方案,但是我对javascript和RXJS相当新,并且不完全理解解决方案正在做什么,所以我不能确定它是否会在每个实例中工作。

I am using reactive forms and have arrays of checkboxes, what I would like to do is get an array of all of the keys used to generate the checkboxes, but without any of the unchecked boxes. 我正在使用被动表单并且有复选框数组,我想要做的是获取用于生成复选框的所有键的数组,但没有任何未选中的框。 Ideally, I could use this to return additional values as well such as a user-readable string, but that is far from important as I can do that in a separate step fairly easily. 理想情况下,我可以使用它来返回其他值,例如用户可读的字符串,但这远非重要,因为我可以相当容易地在单独的步骤中执行此操作。 I have come up with a few methods of my own, but they don't seem to be very robust or performant. 我已经提出了一些自己的方法,但它们似乎并不是非常强大或高效。

I would have replied on this thread , but I lack the reputation. 我会在这个帖子上回复,但我缺乏声誉。

This is the best solution I have found, which I would be totally happy to use, but I don't have much experience with RXJS or maps in javascript so I am not totally sure what it is doing: 这是我找到的最好的解决方案,我会非常乐意使用它,但是我没有太多使用RXJS或javascript中的地图的经验,所以我不完全确定它在做什么:

this.controlNames = Object.keys(this.checkboxGroup.controls).map(_=>_); //This seems to just produce an object of control keys as properties and their value
this.selectedNames = this.checkboxGroup.valueChanges.pipe(map(v => Object.keys(v).filter(k => v[k]))); //Some sort of magic happens and an array is generated and contains only the keys whose values are 'true'

I have tried breaking that snippet apart and using console.log to test what it is doing in each step, but it really didn't give me much useful information. 我试过打破这个片段并使用console.log来测试它在每一步中做了什么,但它确实没有给我很多有用的信息。 Any advice or or better ideas would be thoroughly appreciated, there seem to be a lot of conventions in javascript that people adhere to and it can be hard to sort through what is a convention and what is actually doing something. 任何建议或更好的想法都会被完全理解,似乎人们坚持使用javascript中的许多约定,并且很难对什么是约定进行排序以及实际在做什么。

I think I found a way to break it down and get a grip on it and want to post my explanation for anyone who comes looking. 我想我找到了一种方法来分解并抓住它,并希望为任何想要看的人发布我的解释。


In this part, it is just creating an iterable map of the 'checkboxGroup.controls' object. 在这部分中,它只是创建'checkboxGroup.controls'对象的可迭代映射。 This could have been used to loop over in the template and make all of the checkboxes. 这可以用于在模板中循环并创建所有复选框。 Since my form structure is already generated from arrays of objects with known properties, I don't need this. 由于我的表单结构已经从具有已知属性的对象数组生成,因此我不需要这样做。 The underscores aren't doing anything special here, people just like to use them for private variables. 下划线在这里没有做任何特别的事情,人们只是喜欢将它们用于私有变量。

this.controlNames = Object.keys(this.checkboxGroup.controls).map(_=>_);

For those who are new to arrow functions or some of the conventions of javascript, the code above is not quite, but essentially shorthand for this: 对于那些不熟悉箭头函数或javascript的一些约定的人来说,上面的代码并不完全,但基本上是简写:

this.controlNames = [];
Object.keys(this.checkboxGroup.controls).forEach(function(key) {
  this.controlNames.push(key);
}

I have changed the short variables to longer ones to make them easier to understand in this second part. 我已将短变量更改为较长变量,以便在第二部分中更容易理解。 This maps the value changes observable as an iterable 'changesObj', retrieves the keys, and filters the keys by instances where the key has a true value. 这将值更改可映射为可迭代的'changesObj',检索键,并按键具有true值的实例过滤键。 The code filter(key => changesObj[key]) returns the key if the key is not null, undefined, or false. 如果密钥不为null,未定义或false,则代码filter(key => changesObj[key])将返回密钥。

this.selectedNames = this.checkboxGroup.valueChanges.pipe(map(changesObj => Object.keys(changesObj).filter(key => changesObj[key])));

This is essentially just shorthand for this: 这基本上只是简写:

function propNotFalse (changes, prop) {
  return changes[prop] == true;
}

this.selectedNames = this.alternateFilter = Object.keys(this.checkboxGroup.valueChanges).filter(this.propNotFalse.bind(null, this.checkboxGroup.valueChanges));

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

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