Partage
  • Partager sur Facebook
  • Partager sur Twitter

Import et export d'un fichiers Json via Jquery

modification d'un json via une interface en jquery

    18 janvier 2017 à 15:21:39

    bonjours je vais essayée d'être clair , en gros j'ai crée un tableau qui permet de transformer toute les information entrée ou modifier de celui-ci en Json .

    Je dois crée un bouton(importée) qui permet de lire un fichier Json de l'afficher et de pouvoir le modifier a ma guise puis en appuyant sur exporter , il enregistre les information changer dans le fichier Json remplacent la fonction actuel qui affiche juste le json en dessous.

    Cela m'éviterais de copier coller a chaque fois les informations du Json.

    voila les code actuel merci toutes aide est la bienvenue

    <!DOCTYPE html>
    <html >
    <head>
      <meta charset="UTF-8">
      <title>Tableau éditable</title>
      
      
      <link rel='stylesheet prefetch' href='http://ajax.googleapis.com/ajax/libs/jqueryui/1.11.2/themes/smoothness/jquery-ui.css'>
    <link rel='stylesheet prefetch' href='http://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css'>
    
          <link rel="stylesheet" href="css/style.css">
    
      
    </head>
    
    <body>
      <div class="container">
      <h1><center> Tableau éditable </center></h1>
      <div id="table" class="table-editable">
        <span class="table-add glyphicon glyphicon-plus"></span>
        <table class="table">
          <tr>
            <th>nom</th>
            <th>Valeur</th>
            <th></th>
            <th></th>
          </tr>
          <tr>
            <td contenteditable="true">a remplir</td>
            <td contenteditable="true">a remplir</td>
            <td>
              <span class="table-remove glyphicon glyphicon-remove"></span>
            </td>
            <td>
              <span class="table-up glyphicon glyphicon-arrow-up"></span>
              <span class="table-down glyphicon glyphicon-arrow-down"></span>
            </td>
          </tr>
          <!-- tableau cloné -->
          <tr class="hide">
            <td contenteditable="true">a remplir</td>
            <td contenteditable="true">a remplir</td>
            <td>
              <span class="table-remove glyphicon glyphicon-remove"></span>
            </td>
            <td>
              <span class="table-up glyphicon glyphicon-arrow-up"></span>
              <span class="table-down glyphicon glyphicon-arrow-down"></span>
            </td>
          </tr>
        </table>
      </div>
      
      <center><button id="export-btn" class="btn btn-primary">exportée les données</button></center>
      <p id="export"></p>
    </div>
      <script src='http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js'></script>
    <script src='http://ajax.googleapis.com/ajax/libs/jqueryui/1.11.2/jquery-ui.min.js'></script>
    <script src='http://netdna.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js'></script>
    <script src='http://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.6.0/underscore.js'></script>
    
        <script src="js/index.js"></script>
    
    </body>
    </html>
    
    var $TABLE = $('#table');
    var $BTN = $('#export-btn');
    var $EXPORT = $('#export');
    
    $('.table-add').click(function () {
      var $clone = $TABLE.find('tr.hide').clone(true).removeClass('hide table-line');
      $TABLE.find('table').append($clone);
    });
    
    $('.table-remove').click(function () {
      $(this).parents('tr').detach();
    });
    
    $('.table-up').click(function () {
      var $row = $(this).parents('tr');
      if ($row.index() === 1) return; // Don't go above the header
      $row.prev().before($row.get(0));
    });
    
    $('.table-down').click(function () {
      var $row = $(this).parents('tr');
      $row.next().after($row.get(0));
    });
    
    jQuery.fn.pop = [].pop;
    jQuery.fn.shift = [].shift;
    
    $BTN.click(function () {
      var $rows = $TABLE.find('tr:not(:hidden)');
      var headers = [];
      var data = [];
    
      $($rows.shift()).find('th:not(:empty)').each(function () {
        headers.push($(this).text().toLowerCase());
      });
    
      $rows.each(function () {
        var $td = $(this).find('td');
        var h = {};
    
        headers.forEach(function (header, i) {
          h[header] = $td.eq(i).text();   
        });
        
        data.push(h);
      });
    
      $EXPORT.text(JSON.stringify(data));
    
    });
    var $TABLE = $('#table');
    var $BTN = $('#export-btn');
    var $EXPORT = $('#export');
    
    $('.table-add').click(function () {
      var $clone = $TABLE.find('tr.hide').clone(true).removeClass('hide table-line');
      $TABLE.find('table').append($clone);
    });
    
    $('.table-remove').click(function () {
      $(this).parents('tr').detach();
    });
    
    $('.table-up').click(function () {
      var $row = $(this).parents('tr');
      if ($row.index() === 1) return; // Don't go above the header
      $row.prev().before($row.get(0));
    });
    
    $('.table-down').click(function () {
      var $row = $(this).parents('tr');
      $row.next().after($row.get(0));
    });
    
    jQuery.fn.pop = [].pop;
    jQuery.fn.shift = [].shift;
    
    $BTN.click(function () {
      var $rows = $TABLE.find('tr:not(:hidden)');
      var headers = [];
      var data = [];
    
      $($rows.shift()).find('th:not(:empty)').each(function () {
        headers.push($(this).text().toLowerCase());
      });
    
      $rows.each(function () {
        var $td = $(this).find('td');
        var h = {};
    
        headers.forEach(function (header, i) {
          h[header] = $td.eq(i).text();   
        });
        
        data.push(h);
      });
    
      $EXPORT.text(JSON.stringify(data));
    
    });

    voila tout ^^


    • Partager sur Facebook
    • Partager sur Twitter

    Import et export d'un fichiers Json via Jquery

    × Après avoir cliqué sur "Répondre" vous serez invité à vous connecter pour que votre message soit publié.
    × Attention, ce sujet est très ancien. Le déterrer n'est pas forcément approprié. Nous te conseillons de créer un nouveau sujet pour poser ta question.
    • Editeur
    • Markdown