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 ajaxFun = () => {
  const stuffs = [
    {
      stuff: 'person_1',
      scale: '甲',
      salary: '6000'
    },
    {
      stuff: 'person_2',
      scale: '乙',
      salary: '5000'
    },
    {
      stuff: 'person_3',
      scale: '丙',
      salary: '9000'
    }
  ];
  return stuffs;
}

// 初始化dom
const staffData = ajaxFun();
$APP.innerHTML =
  `
<input id="search" placeholder="请输入 staff,回车确定" />
<div id="all"></div>
<div id="one"></div>
`;
const $SEARCH = document.getElementById('search');
const $ALL = document.getElementById('all');
const $ONE = document.getElementById('one');

// 渲染表格模板方法
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;
};

// 加载所有数据表格
const renderAll = staffData =>
renderTable(['stuff', 'scale'], staffData) + renderTable(['stuff', 'salary'], staffData) + renderTable(['stuff', 'scale', 'salary'], staffData);

// 渲染搜索指定数据表格并修改数据
const filterStuff = params => staffData.filter(item => item.stuff == params);
const findStuff = stuff => staffData.find(item => item.stuff == stuff);
const handleModify = e => {
  const target = e.target;
  const stuff = target.dataset.stuff;
  const type = target.dataset.type;
  const value = target.value;
  const stuffInfo = findStuff(stuff);
  stuffInfo[type] = value;
  $ALL.innerHTML = renderAll(staffData)
  console.log('target, type, stuff, stuffInfo', stuffInfo, staffData);
}
const renderSearchTable = params => {
  const childrenTemple = params.map(item =>
    `
    <tr>
    <td>${item.stuff}</td>
    <td><input data-stuff=${item.stuff} data-type="scale" value=${item.scale} onchange="handleModify(event)"/></td>
    <td><input data-stuff=${item.stuff} data-type="salary" value=${item.salary} onchange="handleModify(event)"/></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 handleSearch = e => {
  const value = e.target.value;
  console.log('value', value)
  $ONE.innerHTML = renderSearchTable(filterStuff(value));
}

// 初始加载数据 
$ALL.innerHTML = renderAll(staffData);

$SEARCH.addEventListener('change', handleSearch, false);
              
            
!
999px

Console