将项目添加到表格一次

时间:2017-03-08 10:27:24

标签: javascript jquery ajax

我有一个上传控件,每次成功上传文件时都会点击。用户可以随时上传一个或多个文件。以下function OnFileUploadComplete(s, e){}函数称为List<T>。在此函数中,进行ajax调用并获取function successCallBack(result) { var table = $("#attachments-table"); $.each(result.Attachment, function (index) { var base64String = btoa(String.fromCharCode.apply(null, new Uint8Array(result.Attachment[index].Thumbnail))); var rows = $('<tr></tr>'), button = $('<input class="btn btn-danger delete-attachment">').attr({ type: 'button', id: result.Attachment[index].Id, name: result.Attachment[index].Id, value: result.Attachment[index].Id }); rows.append('<td>' + result.Attachment[index].AttachmentName + '</td>'); rows.append('<td><img class="img-responsive" src="data:image/png;base64,' + base64String + '"></td>'); $('<td></td>').html(button).appendTo(rows); table.append(rows); }); } 个值。我想从我的ajax响应中循环结果集并将它们显示到我通过执行以下操作获得的表中

func getASCIIString(from binaryString: String) -> String? {

    guard binaryString.characters.count % 8 == 0 else {
        return nil
    }

    var asciiCharacters = [String]()
    var asciiString = ""

    let startIndex = binaryString.startIndex
    var currentLowerIndex = startIndex

    while currentLowerIndex < binaryString.endIndex {

        let currentUpperIndex = binaryString.index(currentLowerIndex, offsetBy: 8)
        let character = binaryString.substring(with: Range(uncheckedBounds: (lower: currentLowerIndex, upper: currentUpperIndex)))
        asciiCharacters.append(character)
        currentLowerIndex = currentUpperIndex
    }

    for asciiChar in asciiCharacters {
        if let number = UInt8(asciiChar, radix: 2) {
            let character = String(describing: UnicodeScalar(number))
            asciiString.append(character)
        } else {
            return nil
        }
    }

    return asciiString
}

let binaryString = "010010000110010101111001"

if let asciiString = getASCIIString(from: binaryString) {
    print(asciiString) // Hey
}

这很好用。但是,如果我有多个项目,则会一遍又一遍地添加它们。所以我的问题是如何只添加表中尚未存在的项目。

注意:页面加载时表为空,并由ajax响应数据填充

1 个答案:

答案 0 :(得分:1)

尝试这样的事情

    function successCallBack(result) {
  var table = $("#attachments-table");

  $.each(result.Attachment, function (index) {
      var base64String = btoa(String.fromCharCode.apply(null, new Uint8Array(result.Attachment[index].Thumbnail)));
       var rows = $('<tr></tr>'),
           button = $('<input class="btn btn-danger delete-attachment">').attr({
                  type: 'button',
                  id: result.Attachment[index].Id,
                  name: result.Attachment[index].Id,
                  value: result.Attachment[index].Id });

            rows.append('<td>' + result.Attachment[index].AttachmentName + '</td>');
            rows.append('<td><img class="img-responsive" src="data:image/png;base64,' + base64String + '"></td>');
            $('<td></td>').html(button).appendTo(rows);
 if($("#"+id).length==0){
            table.append(rows);}
  });
}