@Preview
@Composable
fun TableView(
modifier: Modifier = Modifier,
headers:List<String> = listOf("col1","col2","col3"),
columnWeights:List<Float> = listOf(0.5f,0.5f,0.5f),
dataList:List<List<String>> = listOf(listOf("data1","data2"), listOf("data3","data4"))
) {
Column (modifier=modifier){
var size = headers.size
var sizeWeight = columnWeights.size
if(size>sizeWeight)
Log.d("scoreInfo","input settings is wrong!")
else
{
LazyColumn {
item {
Row(
modifier=Modifier.fillMaxWidth().background(Color.Gray)
) {
headers.forEachIndexed { index, header ->
UserTableCell(modifier=Modifier.weight(columnWeights[index]),text= header,isHeader= true)
}
}
}
dataList.forEach {
item {
var isSelected by remember { mutableStateOf(false) }
Row(
modifier = Modifier.fillMaxWidth()
.clickable {
isSelected=!isSelected
}
.background(if(isSelected) Color.LightGray else Color.White)
) {
var sizeData = it.size
it.forEachIndexed{ index,data->
UserTableCell(modifier=Modifier.weight(columnWeights[index]),text= data, isSelected = isSelected)
}
for(i in it.size .. size-1)
{
UserTableCell(modifier=Modifier.weight(columnWeights[i-1]),text= "", isSelected = isSelected)
}
}
}
}
}
}
}
}
val textStyle = androidx.compose.ui.text.TextStyle(
fontSize = 20.sp,
fontWeight = FontWeight.Normal,
color = Color.Black,
letterSpacing = 0.1.sp,
)
val selectedTextStyle = androidx.compose.ui.text.TextStyle(
fontSize = 20.sp,
color = Color.Red,
letterSpacing = 0.1.sp,
)
val headersTextStyle = androidx.compose.ui.text.TextStyle(
fontSize = 20.sp,
color = Color.Black,
fontWeight = FontWeight.Bold,
letterSpacing = 0.1.sp,
)
@Composable
fun UserTableCell(
modifier: Modifier=Modifier,
text: String="",
isHeader:Boolean=false,
isSelected:Boolean=false
) {
Text(
modifier = modifier.fillMaxSize()
.border(1.dp, Color.Black)
.padding(8.dp)
,
text = text,
style = if(isHeader) headersTextStyle else if(isSelected) selectedTextStyle else textStyle,
textAlign = TextAlign.Center
)
}