简体   繁体   English

我怎样才能 select 在表格(网格)中只有一行?

[英]How can I select only one row in a table(grid)?

I have a grid that I used to Selection inside( https://www.telerik.com/kendo-react-ui/components/grid/selection/ ).I actually have an array of data for example:我有一个网格,我曾经在里面选择( https://www.telerik.com/kendo-react-ui/components/grid/selection/ )。我实际上有一个数据数组,例如:

[{title:'test' ,id:1 ,selected :false}
 {title:'test2' ,id:2 ,selected :false}
 {title:'test2' ,id:3 ,selected :false}]

With this method, when the user clicks on a line, it (selected) becomes true:使用此方法,当用户单击一行时,它(已选中)变为 true:

  selectionChange = (event) => {
        const data = this.state.items.map(item=>{
            if(item.Id === event.dataItem.Id){
                item.selected = !event.dataItem.selected;
            }
            return item;
        });    
    }

In this case, the user can select any of the rows, but I want to be able to select only one row..... What I want is for only one line to be selected.In fact, whatever line is selected, the other lines will be selected:false在这种情况下,用户可以 select 任何一行,但我希望能够 select 只有一行.....我想要的是只选择一行。事实上,无论选择什么行,将selected:false

An easy way is to set all the items=> selected as false, and true to the only matching item.一个简单的方法是将所有的 items=> selected 设置为 false,并将 true 设置为唯一匹配的项目。

Here if the item is selected, then it will change to false and all the other as false, if the item is not selected, then will change to true and all the other selected as false.这里如果item被选中,则变为false,其他全部为false,如果item没有被选中,则变为true,其他全部选中为false。

   const data = this.state.data.map(item=>{         
        if(item.ProductID === event.dataItem.ProductID){
            item.selected = !event.dataItem.selected;
            return item;
        }
       item.selected = false;
        return item;
    });

See the updated fiddle https://stackblitz.com/edit/react-7wmlfp?file=app/main.jsx请参阅更新的小提琴https://stackblitz.com/edit/react-7wmlfp?file=app/main.jsx

selectionChange = (event) => {
    const data = this.state.items.map(item =>{
        item.selected = item.Id === event.dataItem.Id;
        
        return item;
    });
}

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

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