Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel is required to process package imports. If you need a different preprocessor remove all packages first.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div id="app"></div>
<script>
        const $APP = document.getElementById('app');
</script>
              
            
!

CSS

              
                
              
            
!

JS

              
                const leafView_1  = (arr) => {
  const renderTable = arr => params => {
    const childrenTemple = params.map(param =>
                                      `
<tr>
${arr.map(item => `<td>${param[item]}</td>`).join('')}
</tr>`).join('');
    const table =
          `
<table>
<thead>
<tr>
${arr.map(item => `<td>${item}</td>`).join('')}
</tr>
</thead>
<tbody>
</tbody>
${childrenTemple}
</table>
`;
    return table;
  };
  return {
    render: renderTable(arr)
  }
}

const leafView_2 = (Control) => {
  const $ONE = document.getElementById('one');
  let $SCALE;
  let $SALARY;
  const renderSearchTable = params => {
    const childrenTemple = params.map(item =>
      `
      <tr>
      <td>${item.stuff}</td>
      <td><input id="scale" data-stuff=${item.stuff} data-type="scale" value=${item.scale} /></td>
      <td><input id="salary" data-stuff=${item.stuff} data-type="salary" value=${item.salary} /></td>
      </tr>`).join('');
    const table =
      `
      <table>
      <thead>
      <tr>
      <th>staff</th>
      <th>Scale</th>
      <th>Salary</th>
      </tr>
      </thead>
      <tbody>
      ${childrenTemple}
      </tbody>
      </table>
      `;
    return table;
  };
  const handleModify = e => {
    const target = e.target;
    const stuff = target.dataset.stuff;
    const type = target.dataset.type;
    const value = target.value;
    const params = {
      stuff,
      type,
      value
    }
    Control.modifyStuffInfo(params);
  };
  const bindEvent = () => {
    $SCALE.addEventListener('change',handleModify, false);
    $SALARY.addEventListener('change', handleModify, false);
  };
  return {
    render: params => {
      $ONE.innerHTML = renderSearchTable(params);
      $SCALE = document.getElementById('scale');
      $SALARY = document.getElementById('salary');
      bindEvent();
    }
  }
}

const branchView = (Control) => {
  let $SEARCH, $ALL;
  const Views = [];
  const addView = View => {
    Views.push(View)
  };
  const render = (stuffData) => {
   $ALL.innerHTML = Views.reduce((sum,View) => {
     return sum + View.render(stuffData);
    }, '')
  };
  const handleSearch = event => {
    const value = event.target.value;
    Control.filter.call(this, value);
  };
  const bindEvent = () => {
    $SEARCH.addEventListener('change', handleSearch, false);
  }
  const initDOM = () => {
      $APP.innerHTML =
        `
        <input id="search" placeholder="请输入 staff,回车确定"/>
        <div id="all"></div>
        <div id="one"></div>
        `;
      $SEARCH = document.getElementById('search');
      $ALL = document.getElementById('all');
      bindEvent();
  };
  initDOM();
  return {
    render,
    addView,
  }
};

const Model = () => {
  let stuffData = [];
  let filterData = [];
  let Views = {};
  const ajaxFun = () => [
      {
        stuff: 'person_1',
        scale: '甲',
        salary: '6000'
      },
      {
        stuff: 'person_2',
        scale: '乙',
        salary: '5000'
      },
      {
        stuff: 'person_3',
        scale: '丙',
        salary: '9000'
      }
    ];
  const setStuffData = () => {
    stuffData = ajaxFun();
  }; 
  const filterStuff = params => stuffData.filter(item => item.stuff == params);
  const setFilterStuff = params => {
    filterData = filterStuff(params)
  }
  const findStuff = stuff => stuffData.find(item => item.stuff == stuff);

  const modifyStuffData = params => {
    const stuffInfo = findStuff(params.stuff);
    stuffInfo[params.type] = params.value;
  }

  const register = (name, branchView) => {
    (!Views[name]) && (Views[name] = [])
    Views[name].push(branchView)
  };
  const notify = (name, params) => {
    const ViewMapModel = {
      'View_1': stuffData,
      'View_2': filterData
    };
    Views[name].forEach(View => {
      View.render(ViewMapModel[name]);
    })
  }
  return {
    setStuffData,
    notify,
    register,
    setFilterStuff,
    modifyStuffData
  }
}

const Control = () => {
  const ModelInstance = Model();
  const View_1 = Control => {
    const ViewInstance = branchView(Control);
    const stuff_scale = leafView_1(['stuff', 'scale']);
    const stuff_salary = leafView_1(['stuff', 'salary']);
    const stuff_scale_salary = leafView_1(['stuff', 'scale', 'salary']);
    ViewInstance.addView(stuff_scale);
    ViewInstance.addView(stuff_salary);
    ViewInstance.addView(stuff_scale_salary);
    return ViewInstance;
  };
  const View_2 = Control => leafView_2(Control);
  const init = function(){
    ModelInstance.register('View_1', View_1(this));
    ModelInstance.register('View_2', View_2(this))
    ModelInstance.setStuffData();
    ModelInstance.notify('View_1');
  };
  const filter = value => {
    ModelInstance.setFilterStuff(value);
    ModelInstance.notify('View_2');
  }
  const modifyStuffInfo = params => {
    ModelInstance.modifyStuffData(params);
    ModelInstance.notify('View_1');
  }
  return {
    init,
    filter,
    modifyStuffInfo
  }
};

const ControlInstance = Control();
ControlInstance.init();

              
            
!
999px

Console