如何使用Materialize表中的复选框

时间:2015-07-10 04:58:30

标签: html checkbox materialize

我正在使用Materialise,我正在尝试创建一个包含复选框的表。但是,似乎存在问题。表中包含的复选框似乎不起作用。我不确定这是一个错误,还是我没有做正确的事情:

<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>

如何使用Materialise在表格中包含一个复选框?

http://jsfiddle.net/qa37un79/

3 个答案:

答案 0 :(得分:18)

您的复选框没有id,标签符合for属性:

<input type="checkbox" id="myCheckbox" class="filled-in" />
<label for="myCheckbox"></label>

http://jsfiddle.net/xcmsLee9/1/

答案 1 :(得分:0)

我编写了一小段代码,它将嵌套的对象数组的数据转换为带有复选框的实体化表,因此如果您不想查看任何一列,只需选择或取消选择它。

   // 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"); } }

答案 2 :(得分:0)

尝试一下:

<label>
    <input type="checkbox" />
    <span>*** Need text here ***</span>
</label>

将所有内容包装在label元素的正下方span的情况下,这是我能够显示复选框的唯一方法。