简体   繁体   English

如果选中具有相同ID的另一个复选框,如何禁用复选框

[英]How to disable checkboxes if another checkbox with same id is checked

I have a table with a checkbox in one of the columns. 我有一个表,其中一个列中有一个复选框。 When a checkbox is checked I push the row into an array. 选中复选框后,我将行推入数组。

I have tried to explain this better in terms of ..real world down below 我试图在下面的..real world中更好地解释这个

The table can have rows with duplicate id(column of the row ( flat_id ) is different). 该表可以包含重复id的行(行的列( flat_id )不同)。 The duplicate id I mentioned above is user_map_id . 我上面提到的重复ID是user_map_id

When I check(click on the checkbox) a row, I want other rows with same user_map_id to be disabled. 当我检查(单击复选框)一行时,我希望禁用具有相同user_map_id其他行。

So when a row is checked I store the user_map_id s in an array. 因此,当检查一行时,我将user_map_id存储在一个数组中。 And then I do this: 然后我这样做:

ng-disabled="selectedUserMapIdArray.indexOf(flat.user_map_id) >= 0

ie I disable the row if the user_map_id is present in the array with selected user_map_id s but this disables the one I checked too and I can't uncheck it if I want to. 即如果user_map_id存在于具有所选user_map_id的数组中,则禁用该行,但这会禁用我检查的那个,如果我愿意,我也无法取消选中它。

So I pushed the flat_id s in an array too and did this: 所以我也将flat_id s推入数组中并执行此操作:

ng-disabled="selectedUserMapIdArray.indexOf(flat.user_map_id) >= 0 && selectedFlatNumArray.indexOf(flat.flat_id) < 0 "

ie Disable row if the row's user_map_id is present in the selectedUserMapIdArray (which stores the user_map_id ) array and not present in the selectedFlatNumArray (which stores the flat_id ) 即禁用行,如果该行的user_map_id存在于selectedUserMapIdArray (其存储user_map_id )阵列和不存在于selectedFlatNumArray (其存储flat_id

But now when I check a row that has a particular flat_id which is common with one of the duplicate user_map_id , flat_id , and then again check one in that group, the one with the common flat_id does not get disabled because the flat_id I checked above is present in the selectedFlatNumArray (which stores the flat_id ). 但现在当我检查具有特定的行flat_id这是常见的有重复的一个user_map_idflat_id ,然后再次检查一个组中,一个与普通flat_id没有得到禁用,因为flat_id我上面的检查是存在于selectedFlatNumArray (存储flat_id )中。

Explaining in terms of real world 根据现实世界进行解释

Here's a screenshot: 这是一个截图: 图片

The rows in the table are a list of flat owners and occupants(both have user_map_id ). 表中的行是平面所有者和占用者的列表(两者都具有user_map_id )。 A flat owner can have multiple flats(hence duplicate user_map_id s in the table). 扁平所有者可以有多个平面(因此表中的user_map_id重复)。 The flat owner can have multiple flats(different flat_ids ). 公寓业主可以有多个公寓(不同的flat_ids )。

But the flat owner can have an occupant(same flat_id different user_map_id ). 但扁平所有者可以具有乘员(相同flat_id不同user_map_id )。 The occupant can reside in one of the many flats of the flat owner(same flat_id different user_map_id ). 乘员可以驻留在平坦所有者的许多单位中的一个(相同flat_id不同user_map_id )。

I want to be able to check any of the flat owner(others get disabled) and also an occupant(if I want to). 我想能够检查任何一个公寓老板(其他人被禁用)和一个占用者(如果我想)。

If I check row with one of the flats of the flat owner, the other rows of belonging to same flat owner get disabled and works ok. 如果我检查平面所有者的一个平面的行,属于同一个平面所有者的其他行被禁用并且正常工作。

But when I check a row with an occupant and then again check a flat owner of that occupant (which does not have same flat_id ) the row with same flat_id won't get disabled because the flat_id is present in the selectedFlatNumArray (which stores the flat_id) 但是当我检查一个占用者的行,然后再次检查该占用者的平坦所有者(没有相同的flat_id )时,具有相同flat_id行将不会被禁用,因为flat_id存在于selectedFlatNumArray (存储flat_id)中)

How do I do this? 我该怎么做呢?

The table: 桌子:

 <tbody>
    <tr ng-repeat="flat in someObj.flatsArray | filter:searchFlat">
       <td>{{ flat.occupant_name || flat.owner_name}}</td>
       <td>{{flat.flat_no}}</td>
       <td class="text-center">
     <input class="" name="{{flat.flat_no}}" ng-attr-title="{{(selectedUserMapIdArray.indexOf(flat.user_map_id) >= 0 && selectedFlatNumArray.indexOf(flat.flat_id) < 0) ? 'This user has been already invited' : ''}}"
         ng-disabled="selectedUserMapIdArray.indexOf(flat.user_map_id) >= 0 && selectedFlatNumArray.indexOf(flat.flat_id) < 0 "
     type="checkbox" ng-change="checkChanged(flat.isChecked, flat)" ng-model="flat.isChecked" />
              </td>
</tr>
</tbody>

I feel that your ng-disabled and your ng-title should be simplified so it would be easier to follow. 我觉得你的ng-disabled和你的ng-title应该简化,以便更容易理解。 I propose to remove your arrays and iterate through your original to see if it meets your conditions when one flat is changed. 我建议删除你的阵列并迭代你的原件,看看它是否符合你的条件更改一个flat It would be easier to debug and avoid mistakes. 调试和避免错误会更容易。

<input name="{{flat.flat_no}}" 
       ng-attr-title="{{flat.title}}"
       ng-disabled="flat.isDisabled"
       type="checkbox" 
       ng-change="flatChange(flat)" 
       ng-model="flat.isChecked" />

I would propose to iterate through your object when you change a checkbox to make the changes to other flats. 当您更改复选框以对其他单位进行更改时,我建议迭代您的对象。 Beware, it's untested code so it may need some tweaks. 请注意,它是未经测试的代码,因此可能需要一些调整。

$scope.flatChange = function(changedFlat) {
    $scope.someObj.flatsArray.forEach(function(flat) {

        if (changedFlat.checked) {
            if (flat.user_map_id === changedFlat.user_map_id && flat.flat_id !== changedFlat.flat_id) {
                if (changedFlat.idChecked) // If flat is checked, uncheck each one with the same `user_map_id` but not the same `flat_id`.
                    flat.isChecked = false;
                }

                flat.isDisabled = !changedFlat.checked; // `disabled` propriety is the opposite of `checked`.
                flat.title = changedFlat.checked
                            ? 'This user has been already invited'
                            : '';
         }
     });
 };

I don't fully understand your question, but I am getting the sense that you want to treat flat owners (users who own flats and can own many flats) differently than flat occupants (users who occupy flats and can only occupy a single flat). 我并不完全理解你的问题,但是我觉得你想要对待扁平业主(拥有单位且可以拥有许多单位的用户)的方式不同于平面居住者(占用单位并且只能占用单个单位的用户) 。 You are not going to be able to do that unless you store flat owners and flat occupants in different fields, which it looks like you are not doing. 除非您将业主和平面居住者存放在不同的领域,否则您无法做到这一点,看起来您似乎没有这样做。

Your desired behavior is unclear to me. 我不清楚你想要的行为。 Are you selecting flats or people? 你在选择公寓或人吗? Why are you disabling flats when you want to allow people occupying the flats to be selected? 当你想让人们占据公寓时,你为什么禁用单位?

I think probably you want to just disable rows based on the user_id, giving you something like 我想你可能只想根据user_id禁用行,给你一些类似的东西

ng-disabled="selectedUserMapIdArray.indexOf(flat.user_map_id) >= 0 && !flat.isChecked"

您无法取消选中具有相同ID属性的复选框,因为id是唯一的,请尝试将ng-model值从true更改为false

from your first method: "disabling all rows with same id" 从您的第一个方法:“禁用具有相同ID的所有行”

you can put an onClick function in which you can run some code ( with a 10ms delay to make sure your first function for disabling is done ) , send $this element into the function and enable this checkbox again. 你可以放一个onClick函数,你可以在其中运行一些代码(延迟10ms以确保完成禁用的第一个函数),将$ this元素发送到函数中并再次启用此复选框。

in this method when you click on a checkbox all same check boxes will be disabled then the checkbox you click on will be enabled again. 在此方法中,当您单击复选框时,将禁用所有相同的复选框,然后将再次启用您单击的复选框。

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

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