复制重试的json数据并以文本格式粘贴到文本编辑器中

时间:2018-03-26 06:18:21

标签: javascript jquery html5 angular angular2-template



collegeDestails = [
      {
        "name": "Indirabai Meghe Mahila Mahavidyalaya,Amravati.",
        "address": "Morshi Road Amravati. 444602",
        "phone": 9423424787,
        "district": "Amravati",
        "email": "imc.amt@gmail.com"
      },
      {
        "name": "Indirabai Meghe Mahila Mahavidyalaya,Amravati.",
        "address": "Morshi Road Amravati. 444602",
        "phone": 9423424787,
        "district": "Amravati",
        "email": "imc.amt@gmail.com"
      },
      {
        "name": "Indirabai Meghe Mahila Mahavidyalaya,Amravati.",
        "address": "Morshi Road Amravati. 444602",
        "phone": 9423424787,
        "district": "Amravati",
        "email": "imc.amt@gmail.com"
      },

      {
        "name": "Indirabai Meghe Mahila Mahavidyalaya,Amravati.",
        "address": "Morshi Road Amravati. 444602",
        "phone": 9423424787,
        "district": "Amravati",
        "email": "imc.amt@gmail.com"
      },
      {
        "name": "Indirabai Meghe Mahila Mahavidyalaya,Amravati.",
        "address": "Morshi Road Amravati. 444602",
        "phone": 9423424787,
        "district": "Amravati",
        "email": "imc.amt@gmail.com"
      },
      {
        "name": "Indirabai Meghe Mahila Mahavidyalaya,Amravati.",
        "address": "Morshi Road Amravati. 444602",
        "phone": 9423424787,
        "district": "Amravati",
        "email": "imc.amt@gmail.com"
      },

      {
        "name": "Indirabai Meghe Mahila Mahavidyalaya,Amravati.",
        "address": "Morshi Road Amravati. 444602",
        "phone": 9423424787,
        "district": "Amravati",
        "email": "imc.amt@gmail.com"
      },
      {
        "name": "Indirabai Meghe Mahila Mahavidyalaya,Amravati.",
        "address": "Morshi Road Amravati. 444602",
        "phone": 9423424787,
        "district": "Amravati",
        "email": "imc.amt@gmail.com"
      },
      {
        "name": "Indirabai Meghe Mahila Mahavidyalaya,Amravati.",
        "address": "Morshi Road Amravati. 444602",
        "phone": 9423424787,
        "district": "Amravati",
        "email": "imc.amt@gmail.com"
      },
      {
        "name": "Indirabai Meghe Mahila Mahavidyalaya,Amravati.",
        "address": "Morshi Road Amravati. 444602",
        "phone": 9423424787,
        "district": "Amravati",
        "email": "imc.amt@gmail.com"
      }
    ]

<div class="card" *ngFor="let data of collegeDestails;let i = index">
                <div class="card-header">
                    <span class="font-weight-bold">{{ data.name }}</span>
                    <div class="form-check float-right">
                        <input class="form-check-input" type="checkbox" (change)="instituteChecked($event,i)">
                    </div>
                </div>
                <div class="card-body">
                    <div class="row">
                        <div class="col-md-6">
                            <div class="details-section">
                                <p class="font-weight-bold">Address: </p>
                                <p>{{ data.address }}</p>
                            </div>
                            <div class="details-section">
                                <p class="font-weight-bold">Phone: </p>
                                <p>{{ data.phone }}</p>
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="details-section">
                                <p class="font-weight-bold">District: </p>
                                <p>{{ data.district }}</p>
                            </div>
                            <div class="details-section">
                                <p class="font-weight-bold">Email:</p>
                                <p>{{ data.email }}</p>
                            </div>
                        </div>
                        <!-- <hr> -->
                    </div>
                </div>
            </div>
&#13;
&#13;
&#13;

我正在使用angular 2,我想实现一个功能,即我从JSON格式的服务器获取记录列表。当用户使用复选框&amp;选择多个或一个记录时点击复制按钮所有记录都应以文本格式复制,他/她可以粘贴在任何文本编辑器中。我该如何实现这个功能?提前谢谢你。image

enter image description here

1 个答案:

答案 0 :(得分:1)

您可以创建一个copyToClipboard方法,将您的记录字符串化并将其存储到剪贴板中:

copyToClipboard(record:Record){
  let content = JSON.stringify(record);
  document.addEventListener('copy', (e: ClipboardEvent) => {
    e.clipboardData.setData('text/plain', content);
    e.preventDefault();
    document.removeEventListener('copy');
  });
  document.execCommand('copy');
};