简体   繁体   English

设置MUI切换按钮组的默认值

[英]Setting Default values for MUI toggle button group

I am using the Material UI library, and used the button toggle widget as a picker. 我正在使用Material UI库,并将按钮切换小部件用作选择器。

Here is my codesandbox - https://codesandbox.io/s/50pl0jy3xk 这里是我的codesandbox - https://codesandbox.io/s/50pl0jy3xk

There are a few interactions going on, but basically a user can click a membership type for an adult, child or infant. 正在进行一些交互,但是基本上用户可以单击成人,儿童或婴儿的成员资格类型。 There are 3 options of yes, no and maybe. 有3种选择,是,否和也许。 Once a user selects one or multiple options this gets submitted to api. 用户选择一个或多个选项后,便会提交给api。

I have added a default values from my receiving prop, so if a user has already got a membership it has them highlighted. 我从接收道具中添加了一个默认值,因此,如果用户已经拥有一个成员资格,则将其突出显示。

const selected = [
  {
    personId: "0001657",
    fullName: "Joe Bloggs",
    seniorStatus: "Yes",
    defaultStatus: [
      { membership: "Seniors", defaultvalue: "Yes" },
      { membership: "Juniors", defaultvalue: "No" }
    ]
  }
];

As you can see the defaultStatus field(above) has been added for the users current memberships. 如您所见,为用户当前的成员资格添加了defaultStatus字段(上方)。 All this needs to do is highlight the relevant toggle, the data if not changed does not need submission to api.(below) - I would guess that the value needs to be interrogated but the value is already being used in my case. 所有这一切需要做的是突出显示相关的切换,如果没有更改,数据不需要提交到api。

            <ToggleButtonGroup
                className={classes.toggleButtonGroup}
                value={value[rowIdx][cellIdx.value]}
                exclusive
                onChange={(event, val) =>
                  this.handleValue(event, val, rowIdx, cellIdx.value)
                }
              >
                <ToggleButton
                  className={`w-100 ${classes.toggleButton}`}
                  value="yes"
                >
                  Yes
                </ToggleButton>
                <ToggleButton
                  className={`w-100 ${classes.toggleButton}`}
                  value="no"
                >
                  No
                </ToggleButton>
                <ToggleButton
                  className={`w-100 ${classes.toggleButton}`}
                  value="maybe"
                >
                  Maybe
                </ToggleButton>
              </ToggleButtonGroup>

Any help or assistance would be fantastic! 任何帮助或协助都将非常棒!

  1. You are using "yes", "no" and "maybe" in ToggleButton group, but using "Yes", "No" etc in default value. 您在ToggleButton组中使用“是”,“否”和“也许”,但是在默认值中使用“是”,“否”等。 I guess that is a typo, both need to match. 我猜这是一个错字,两者都需要匹配。
  2. Now you can set the value like this: 现在,您可以像这样设置值:
    \nvalue={ 值= {\n  value[rowIdx]["defaultStatus"] && // check if defaultStatus exists value [rowIdx] [“ defaultStatus”] && //检查defaultStatus是否存在\n  value[rowIdx]["defaultStatus"].filter( // filter the defaultStatus value [rowIdx] [“ defaultStatus”]。filter(//过滤defaultStatus\n    ds => ds.membership === cellIdx.label // and look for the specific membership type ds => ds.membership === cellIdx.label //,然后查找特定的成员资格类型\n  )[0] // check if that exists )[0] //检查是否存在   \n    ?  value[rowIdx]["defaultStatus"].filter( 值[rowIdx] [ “defaultStatus”]。过滤(\n        ds => ds.membership === cellIdx.label ds => ds.membership === cellIdx.label\n      )[0].defaultvalue // set the value )[0] .defaultvalue //设置值\n    : null // otherwise null :null //否则为null\n} }\n

Here is your forked example: https://codesandbox.io/s/mjk9zy5rqp?fontsize=14 这是您的分叉示例: https : //codesandbox.io/s/mjk9zy5rqp? fontsize =14

PS: PS:

  • I noticed the logic of handleValue will not work anymore . 我注意到handleValue的逻辑将不再起作用 Looks like it was a 2D array previously and now you have defined it according to a new schema. 看起来它以前是2D数组,现在您已经根据新架构对其进行了定义。 May be you are in the middle of an edit. 可能是您正在编辑中。 But so you know. 但是,你知道。
  • I think it would be better if you make defaultStatus like this, instead of an array: 我认为最好是这样使defaultStatus而不是数组:
    \ndefaultStatus: { defaultStatus:{\n  Seniors: "yes", 老年人:“是”,\n  Juniors: "no" 大三:“不”\n} } 

This way you won't have to filter the array like I did in the example. 这样,您将不必像示例中那样过滤数组。 You could simply use value[rowIdx]["defaultStatus"][cellIdx.label] as value. 您可以简单地使用value[rowIdx]["defaultStatus"][cellIdx.label]作为值。

Update: 更新:

Your handleValue function is not working because you are setting the state in wrong place (probably from your old state design). 您的handleValue函数不起作用,因为您将状态设置在错误的位置(可能是从旧的状态设计得出的)。 You need to modify it to incorporate you new state design: 您需要对其进行修改以合并新的状态设计:

handleValue = (event, val, rowIdx, cellIdx) => {
    ...
    if (!newValue[rowIdx]["defaultStatus"]) {   // check if you have defaultStatus for that row
      newValue[rowIdx]["defaultStatus"] = [];   // create empty array if that doesn't exist
    }
    const updatable = newValue[rowIdx]["defaultStatus"].filter(  // find the column corresponding to the membership
      ds => ds.membership === cellIdx                       
    );

    if (updatable[0]) {                   // if such column exists
      updatable[0]["defaultvalue"] = val; // update the defaultvalue
    } else {
      newValue[rowIdx]["defaultStatus"].push({  // otherwise create a new defaultvalue
        membership: cellIdx,
        defaultvalue: val
      });
    }
    this.setState({
      value: newValue
    });
  };

See my updated code in the same sandbox url. 在相同的沙箱网址中查看我的更新代码。 Again, the logic could be much simplified with a plain object instead of an array. 同样,使用普通对象而不是数组可以大大简化逻辑。

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

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