在JSX中基于条件显示复选框。

huangapple go评论66阅读模式
英文:

Show Checkboxes based on a Condition in JSX

问题

我对JSX还不熟悉,本周的任务是在特定条件为真时隐藏复选框。条件是当有帐户登录时,不应显示复选框。

下面是复选框及其条件的代码:

<TableRow>
    {!isOpsAdmin || !isOmiAdmin || !isRider || !isRunner && (
        <TableCell padding="checkbox">
            <Checkbox
                // eslint-disable-next-line no-console
                onChange={() => console.log("checked all")}
            />
        </TableCell>
    )}
</TableRow>

目前,只有当我登录到isOpsAdmin帐户时,复选框才会隐藏。但每当我尝试登录到其他帐户(isOmiAdminisRiderisRunner)时,复选框都会可见,这不是预期的结果。可以有人帮助吗,因为我对JSX非常陌生,因此您的帮助将非常感激。

英文:

I am new to JSX and my task for this week is to hide the checkbox whenever a certain condition is true. The conditions are when an account is logged in, the checkboxes shouldn't be visible on that account.

The code below is the code for the checkboxes and its condition:

&lt;TableRow&gt;
            {!isOpsAdmin || !isOmiAdmin || !isRider || !isRunner &amp;&amp; (
              &lt;TableCell padding=&quot;checkbox&quot;&gt;
                &lt;Checkbox
                  // eslint-disable-next-line no-console
                  onChange={() =&gt; console.log(&quot;checked all&quot;)}
                /&gt;
              &lt;/TableCell&gt;
            )}

Right now, the checkboxes are only hidden when I login on the isOpsAdmin account, but whenever I try logging in on the other accounts (isOmiAdmin, isRider, isRunner), the checkboxes would be visible and that is not the expected result. Can anyone help as I'm very new to JSX therefore your help will be very much appreciated.

答案1

得分: 2

我认为您可能希望更新显示复选框的逻辑条件:

!(isOpsAdmin || isOmiAdmin || isRider || isRunner) && ...

如果任何条件为真,此条件将为假。

您还可以考虑使用HTML的hidden属性,例如:

<TableCell 
  padding="checkbox"
  hidden={isOpsAdmin || isOmiAdmin || isRider || isRunner}
>
  <Checkbox
    // eslint-disable-next-line no-console
    onChange={() => console.log("checked all")}
  />
</TableCell>

您可以隐藏单元格本身,或者只在Checkbox组件内使用hidden属性,如果只想隐藏复选框。

英文:

I think you might want to update the logic condition for showing the checkbox:

!(isOpsAdmin || isOmiAdmin || isRider || isRunner) &amp;&amp; ...

this condition will be false if any of the conditions are true.

you might also want to consider using the html hidden attribute like:

  &lt;TableCell 
     padding=&quot;checkbox&quot;
     hidden={isOpsAdmin || isOmiAdmin || isRider || isRunner}
  &gt;
    &lt;Checkbox
      // eslint-disable-next-line no-console
      onChange={() =&gt; console.log(&quot;checked all&quot;)}
    /&gt;
  &lt;/TableCell&gt;

You can either hide the cells itself, or just use the hidden attribute inside the Checkbox component if you just want to hide the checkbox

huangapple
  • 本文由 发表于 2023年6月1日 00:22:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/76375552.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定