Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs 反应表排序和搜索_Reactjs - Fatal编程技术网

Reactjs 反应表排序和搜索

Reactjs 反应表排序和搜索,reactjs,Reactjs,我有这样的数据 const data =[ { name:'gacvuc', pot1[ { density:1 } ] pot2[ { density:2 } ] } ..... ] 现在我做了一个切换,我们想看pot1密度还是pot2密度 用这个做了一张桌子 <tablebody> data.map(a,in)=>{ const show= toggleChoice === 0 ? a.pot1 : a.pot2; } return( <tablecell>{

我有这样的数据

const data =[
{
name:'gacvuc',
pot1[
{
density:1
}
]
pot2[
{
density:2
}
]
}
.....
]
现在我做了一个切换,我们想看pot1密度还是pot2密度 用这个做了一张桌子

<tablebody>
data.map(a,in)=>{
const show= toggleChoice === 0 ? a.pot1 : a.pot2;
}
return(
<tablecell>{a.name}</tablecell>
<tablecell>{show.density}</tablecell>
</tablebody>
);

data.map(a,in)=>{
const show=toggleChoice==0?a.pot1:a.pot2;
}
返回(
{a.name}
{show.density}
);

现在我想做的是,我想按密度升序或降序排序,或者按名称搜索。有人能告诉我怎么做吗?

首先,我建议大家查看一下数据的格式并查找错误

要对数据进行排序,只需使用内置的排序方法并传入一个函数,该函数给出如何对数据中的两个不同对象进行排序的说明

data.sort(function(a, b) {
  // you can define this based on your toggle variable
  const pot = "pot1"; 
  
  // densities of both object
  const aDensity = a[pot][0].density;
  const bDensity = b[pot][0].density;
  
  // compare them and then return -1, 0, or 1 accordingly
  if (aDensity == bDensity) { // if they are equal, return 0
    return 0;
  }
  else if(aDensity > bDensity) { // > return 1
    return 1;
  }
  else { // < return -1
    return -1;
  }
})
data.sort(函数(a,b){
//您可以根据您的切换变量来定义它
const pot=“pot1”;
//两种物体的密度
常数密度=a[pot][0]。密度;
常数b密度=b[pot][0]。密度;
//比较它们,然后相应地返回-1、0或1
如果(aDensity==bDensity){//如果它们相等,则返回0
返回0;
}
else if(aDensity>bDensity){/>返回1
返回1;
}
else{/

在这个片段中,您将获得两个任意对象(a和b)的密度,然后根据密度值返回-1、0或1。在这个例子中。我按升序对它们进行了排序,但如果你想降序,你可以在代码中用1替换-1,反之亦然。

no我想对整个表进行排序。我不确定你的确切意思。这将对整个数据集以及表格进行排序。我遗漏了什么吗?你能告诉我这将如何工作吗?我想要一个按升序或降序排序的按钮。我可以告诉你,我需要更多地了解你在使用什么。在这个项目中,您是将react钩子用于功能组件,还是将基于类的react组件用于正常的react生命周期方法?是的,我在这个项目中使用react钩子