data-set

Exemple 1

  1. <button data-toggle-id="calendar">
  2.   Show calendar
  3. </button>

  4. <form id="calendar" hidden>
  5.   mois de mai !
  6. </form>


  1.   document.addEventListener('click', function(event) {
  2.     let id = event.target.dataset.toggleId;
  3.     if (!id) return;

  4.     let elem = document.querySelector(`'#${id}'`);
  5.     elem.hidden = !elem.hidden;
  6.   });


https://jsbin.com/wagipuj/edit?html,js,output

Exemple 2

  1. <div id="menu">

  2.   <button data-action="save">Save</button>

  3.   <button data-action="load">Load</button>

  4.   <button data-action="search">Search</button>

  5. </div>


  1. class Menu {

  2.     constructor(elem) {

  3.       this._elem = elem;

  4.       

  5.     }


  6.     save() {

  7.       alert('saving');

  8.     }


  9.     load() {

  10.       alert('loading');

  11.     }


  12.     search() {

  13.       alert('searching');

  14.     }


  15.     onClick(event) {

  16.       let action = event.target.dataset.action;

  17.       if (action) {

  18.         this[action]();

  19.       }

  20.     };

  21.   }


  22.   new Menu(menu);

https://jsbin.com/dufunaf/5/edit?html,css,js,console,output

Evolution : comparaison

 


node

element

function buildTable(data) {

    var table = document.createElement("table");

  

    var fields = Object.keys(data[0]);

  console.log(fields);

    var headRow = document.createElement("tr");

    fields.forEach(function(field) {

      var headCell = document.createElement("th");

      headCell.textContent = field;

      headRow.appendChild(headCell);

    });

    table.appendChild(headRow);


    data.forEach(function(object) {

      var row = document.createElement("tr");

      fields.forEach(function(field) {

        var cell = document.createElement("td");

        cell.textContent = object[field];

        if (typeof object[field] == "number")

          cell.style.cssText = "background-color:black;color:white;text-align:right;"

        row.appendChild(cell);

      });

      table.appendChild(row);

    });


    return table;

  }


  document.body.appendChild(buildTable(PERSON));


function buildTable(data) {

  const table = document.createElement("table"),

    fields = Object.keys(data[0]);


  let template = `<tr><th>${fields[0]}</th><th>${fields[1]}</th><th>${fields[2]}</th></tr>`;

  table.insertAdjacentHTML("afterBegin", template);


  for (let { name, age, country } of data) {

    template = `<tr><td>${name}</td><td>${age}</td><td>${country}</td></tr>`;

    table.insertAdjacentHTML("beforEEnd", template);

  }

  return table;

}


document.body.appendChild(buildTable(PERSON));


code

code


high level function

   function elt(name, attrs, ...children) {
     let dom = document.createElement(name);
     for (let attr of Object.keys(attrs)) {
       dom.setAttribute(attr, attrs[attr]);
     }
     for (let child of children) {
       dom.appendChild(child);
     }
     return dom;
   }

let text = document.createTextNode("topc");

let u = elt("p",{},...[text]);

document.querySelector(".test").appendChild(u);


const tab = ["lun","mard","mercredi"];


let v = elt("div",{},...tab.map( (jour)=> {
  let p = elt("p",{class:"red"})
  p.innerHTML = jour;
  return p;
} ));

document.querySelector(".map").appendChild(v);


En action

à comparer avec

Différence entre screenX, clientX, pageX


yellow Screen → the full screen of the monitor (screenX/Y)
Position will always be relative to the physical screen's viewport.

Blue Client → the client viewport of the browser (clientX/Y)
If you click in the left top corner the value will always be (0,0) independent on scroll position.

Red Document → the complete document/page (pageX/Y)
Note that pageX/pageY on the UIEvent object are not standardized.

All values are in pixels.

screen snapshot with extended page illustration


Simulateur



https://jsbin.com/dilumuz/3/edit?html,css,js,console,output

attachShadow : isole le CSS