如何在 Materialise 中使用复选框 table
How to use a checkbox in Materialize table
我正在使用 Materialize,我正在尝试制作一个包含复选框的 table。但是,似乎有问题。 table 中包含的复选框似乎不起作用。我不确定这是错误还是我没有做正确的事情:
<form action="#">
<table>
<thead>
<tr>
<th>Name</th>
<th>Status</th>
</tr>
</thead>
<tbody>
<tr>
<td>My Label</td>
<td><input type="checkbox" class="filled-in" /><label></label></td>
</tr>
</tbody>
</table>
</form>
如何使用 Materialize 创建包含在 table 中的复选框?
您没有复选框的 id
和标签的匹配 for
属性:
<input type="checkbox" id="myCheckbox" class="filled-in" />
<label for="myCheckbox"></label>
我写了一小段代码,它将嵌套对象数组的数据转换为带有复选框的物化 table,所以如果您不想查看任何一列,只需 select 或 deselect .
// Using the Materilize css Check boxes change the functionality of the check bocx accordingly
var tab= document.getElementById("TransformTable");
//Get The No of Cols from the JSON data
var NoOfCols=Object.getOwnPropertyNames(data[0]); //Insert the row for the table headings var row = tab.insertRow(0); //set the attribute row.setAttribute("id",0+"_row"); /** * Loop for No of Cols and set the heading and append the CheckBox */ for(var i=0;i<NoOfCols.length;i++){ var cell1 =row.insertCell(i); cell1.setAttribute("id","0_row"+i+"_cell"); cell1.innerHTML="<b>"+NoOfCols[i]+"</b>";
$("#checkBoxDiv").append("<input type='checkbox' id="+i+" checked='checked' style='margin:10px;'/><label for="+i+" style='margin-right:10px;'>"+NoOfCols[i]+"</label>"); } /** *Loop for the Table data */ // Loop for the no Of rows for(var j=0;j<data.length;j++){ var row2=tab.insertRow(j+1); row.setAttribute("id",(j+1)+"_row"); // Lopp for the No of Cols in a row for(var k=0;k<NoOfCols.length;k++){ var cell2=row2.insertCell(k); cell2.setAttribute("id",(j+1)+"_row"+k+"_cell"); cell2.innerHTML=data[j][NoOfCols[k]]; } } /** event Listener for the check Boxes */ $("input").click(function(event){ var id = event.target.id; if($("#"+id).prop("checked")){ DisplayTable(id); }else{ HideTable(id); } }); /** * [HideTable Hide the col of a table]
*/ function HideTable(id){ var ColId =id; for(var t=0;t<=data.length;t++){ $("#"+t+"_row"+ColId+"_cell").css("display","none"); } } /** * [DisplayTable Display the col of a table]
*/ function DisplayTable(id){ var ColId =id; for(var t=0;t<=data.length;t++){ $("#"+t+"_row"+ColId+"_cell").css("display","table-cell"); } }
试试这个:
<label>
<input type="checkbox" />
<span>*** Need text here ***</span>
</label>
将所有内容包装在 label
元素中,并在其下方添加 span
是我能够显示复选框的唯一方法。
我正在使用 Materialize,我正在尝试制作一个包含复选框的 table。但是,似乎有问题。 table 中包含的复选框似乎不起作用。我不确定这是错误还是我没有做正确的事情:
<form action="#">
<table>
<thead>
<tr>
<th>Name</th>
<th>Status</th>
</tr>
</thead>
<tbody>
<tr>
<td>My Label</td>
<td><input type="checkbox" class="filled-in" /><label></label></td>
</tr>
</tbody>
</table>
</form>
如何使用 Materialize 创建包含在 table 中的复选框?
您没有复选框的 id
和标签的匹配 for
属性:
<input type="checkbox" id="myCheckbox" class="filled-in" />
<label for="myCheckbox"></label>
我写了一小段代码,它将嵌套对象数组的数据转换为带有复选框的物化 table,所以如果您不想查看任何一列,只需 select 或 deselect .
// Using the Materilize css Check boxes change the functionality of the check bocx accordingly
var tab= document.getElementById("TransformTable");
//Get The No of Cols from the JSON data
var NoOfCols=Object.getOwnPropertyNames(data[0]); //Insert the row for the table headings var row = tab.insertRow(0); //set the attribute row.setAttribute("id",0+"_row"); /** * Loop for No of Cols and set the heading and append the CheckBox */ for(var i=0;i<NoOfCols.length;i++){ var cell1 =row.insertCell(i); cell1.setAttribute("id","0_row"+i+"_cell"); cell1.innerHTML="<b>"+NoOfCols[i]+"</b>";
$("#checkBoxDiv").append("<input type='checkbox' id="+i+" checked='checked' style='margin:10px;'/><label for="+i+" style='margin-right:10px;'>"+NoOfCols[i]+"</label>"); } /** *Loop for the Table data */ // Loop for the no Of rows for(var j=0;j<data.length;j++){ var row2=tab.insertRow(j+1); row.setAttribute("id",(j+1)+"_row"); // Lopp for the No of Cols in a row for(var k=0;k<NoOfCols.length;k++){ var cell2=row2.insertCell(k); cell2.setAttribute("id",(j+1)+"_row"+k+"_cell"); cell2.innerHTML=data[j][NoOfCols[k]]; } } /** event Listener for the check Boxes */ $("input").click(function(event){ var id = event.target.id; if($("#"+id).prop("checked")){ DisplayTable(id); }else{ HideTable(id); } }); /** * [HideTable Hide the col of a table]
*/ function HideTable(id){ var ColId =id; for(var t=0;t<=data.length;t++){ $("#"+t+"_row"+ColId+"_cell").css("display","none"); } } /** * [DisplayTable Display the col of a table]
*/ function DisplayTable(id){ var ColId =id; for(var t=0;t<=data.length;t++){ $("#"+t+"_row"+ColId+"_cell").css("display","table-cell"); } }
试试这个:
<label>
<input type="checkbox" />
<span>*** Need text here ***</span>
</label>
将所有内容包装在 label
元素中,并在其下方添加 span
是我能够显示复选框的唯一方法。