jQuery表没有删除行

时间:2014-03-30 05:57:51

标签: c# javascript jquery twitter-bootstrap datatable

我在我的代码中使用jQuery,datatable和bootstrap。我的代码相当简单,没有标签丢失,但它仍然没有正常运行。在追加行之前,我试图删除除第一行之外的所有行,但不删除。以下是我的代码

HTML代码

<table id="attendanceDetails" class="table table-bordered table-hover">
          <thead>
            <tr>
                <th>Date</th>
                <th>Time In</th>
                <th>Time Out</th>
                <th>Permission Reason</th>
                <th>Absence Hrs</th>
                <th>Late Hrs</th>
                <th>Overtime Hrs</th>
                <th>Official Hrs</th>
                <th>Personal Hrs</th>
            </tr>
          </thead>
          <tbody>
          </tbody>
        </table>

我在click事件jQuery中调用的代码

function getMyAttandance(flag) {
if (flag == 1) {
  if ($("#selAttendMonth").val() == "") {
    alert("Please select valid Month");
    return false;
  }
  if ($("#selAttendYear").val() == "") {
    alert("Please select valid Year");
    return false;
  }
}
$("#divLoading").show();
$('#attendanceDetails tbody').html('');
  $.ajax({
    type: "POST",
    url: "/_layouts/15/selfService.aspx/LoadAttendance",
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    data: "{'loginName':'" + loginName.split("\\")[1] + "','month':'" + $("#selAttendMonth").val() + "','year':'" + $("#selAttendYear").val() + "'}",
    async: false,
    cache: false,
    success: function (msg) {
      var data = JSON.parse(msg.d)
      $.each(data, function (key, value) {
        if (key == "AttendanceDetails") {
          $.each(value, function (i, j) {
            if ((parseDate(j.Date).getDay() == 5) || (parseDate(j.Date).getDay() == 6))
              $("#attendanceDetails > tbody:last").append('<tr class="weekend"><td>' + j.Date + '</td><td>' + j.TimeIn + '</td><td>' + j.TimeOut + '</td><td>' + j.PermissionReason + '</td><td>' + j.ABSHours + '</td><td>' + j.LateHours + '</td><td>' + j.OverTimeHours + '</td><td>' + j.OfficailHours + '</td><td>' + j.PersonalHours + '</td></tr>');
            else
              $("#attendanceDetails > tbody:last").append('<tr><td>' + j.Date + '</td><td>' + j.TimeIn + '</td><td>' + j.TimeOut + '</td><td>' + j.PermissionReason + '</td><td>' + j.ABSHours + '</td><td>' + j.LateHours + '</td><td>' + j.OverTimeHours + '</td><td>' + j.OfficailHours + '</td><td>' + j.PersonalHours + '</td></tr>');
          });
        }
        else
          $("#" + key).text(value);
      });
      $('#attendanceDetails').dataTable({
        "iDisplayLength": 10,   // records per page
        //"sDom": "t<'row'<'col-md-6'i><'col-md-6'p>>",
        "sPaginationType": "bootstrap",
        "bFilter": false,
        "bLengthChange": false,
        "bDestroy": true
      });
      $("#divLoading").hide();
    }
  });
  }

1 个答案:

答案 0 :(得分:0)

我使用下面的帮助我

if($('#attendanceDetails').dataTable() != null)     
$('#attendanceDetails').dataTable().fnDestroy();
$('#attendanceDetails tbody').empty();