cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

Pen Settings

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

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

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

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

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.

            
              <div class="control">
	<form action="#" id="search" onsubmit="return false">
		<select name="sort">
			<option value="name">名前順</option>
			<option value="price">価格順</option>
		</select>
		<input type="text" name="name" placeholder="キーワード" >
		<button type="submit">検索</button>
	</form>
	<button type="button" data-script="all-check" data-target="input[name*=delete]">すべて選択</button>
	<button type="button" data-script="all-clear" data-target="input[name*=delete]">すべて解除</button>
</div>

<table class="datatable">
	<colgroup>
		<col class="input">
	</colgroup>
	<thead>
		<tr>
			<th class="cell_input"></th>
			<th class="cell_name">名前</th>
			<th class="cell_price">価格</th>
		</tr>
	</thead>
	<tbody data-script="json-list" data-source="myScripts.data">
		<tr data-master>
			<td class="cell_input"><input type="checkbox" name="delete" value="" disabled="disabled"></td>
			<td class="cell_name" data-attr="name"></td>
			<td class="cell_price" data-unit="円" data-attr="price"></td>
		</tr>
	</tbody>
</table>
            
          
!
            
              @import url('//fonts.googleapis.com/css?family=Roboto');
html, * {
	font-family: 'Roboto';
	box-sizing: border-box;
}

.control {
	padding: 1em;
	
	button:disabled {
		opacity: .2;
	}
}

table.datatable {
	width: 100%;
	table-layout: fixed;
	
	col.input {
		width: 2em;
	}
	
	th,
	td {
		border-top: 1px solid #ccc;
		border-bottom: 1px solid #ccc;
		padding: 0.5em;
	}
	
	td {
		&.cell_input {
			text-align: center;
		}
		&.cell_name {
		}
		&.cell_price {
			text-align: right;
			
			&::after {
				content: attr(data-unit);
				margin-left: .25em;
			}
		}
	}
	
	thead th {
		background: #eee;
	}
}

            
          
!
            
              if(typeof myScripts === 'undefined') {
  myScripts = {};
}

(function(window, document, undefined) {
  
  var ui_allCheck = function($control, option) {
    this.$control = $control;
    this.init(option);
  };
  
  ui_allCheck.prototype = {
    init: function(option) {
      var _self = this;
      
      _self.option = option;
      
      _self.targetSelector = this.$control.getAttribute(option.targetAttr);
      
      _self.setup();
    },
    destory: function() {
      var _self = this;
      _self.$control.removeEventListener('click', _self.update.bind(_self));
      
      var $target = [].slice.call(document.querySelectorAll(_self.targetSelector));
      if(!$target[0]) return;
      
      Array.prototype.forEach.call($target, function($input, index){
        $input.removeEventListener('change', _self.change.bind(_self), false);
      });
    },
    setup: function() {
      var _self = this;
      
      _self.$control.addEventListener('click', _self.update.bind(_self), false);
      
      _self.$target = _self.getTarget();
      if(!_self.$target[0]) return;
      
      Array.prototype.forEach.call(_self.$target, function($input, index){
        $input.addEventListener('change', _self.change.bind(_self), false);
      });
      
    },
    change: function() {
      var _self = this;
      _self.$target = _self.getTarget();
      if(!_self.$target[0]) return;
      var invalid = true;
      Array.prototype.forEach.call(_self.$target, function($input, index){
        if($input.checked != _self.option.checked) invalid = false;
      });
      _self.$control.disabled = invalid;
    },
    update: function() {
      var _self = this;
      _self.$target = _self.getTarget();
      if(!_self.$target[0]) return;
      
      Array.prototype.forEach.call(_self.$target, function($input, index){
        $input.checked = _self.option.checked;
        
        var event = document.createEvent('HTMLEvents');
        event.initEvent('change', true, false);
        $input.dispatchEvent(event);
      });
    },
    dispatch: function($target, eventname) {
      var _self = this;
      var event = document.createEvent('HTMLEvents');
      event.initEvent(eventname, true, false);
      $target.dispatchEvent(event);
    },
    getTarget: function() {
      var _self = this;
      var target_name = _self.$control.getAttribute(_self.option.targetAttr);
      return [].slice.call(document.querySelectorAll(target_name));
    }
  };
  var setup_allCheck = function(option) {
    this.init(option);
  };
  
  setup_allCheck.prototype = {
    init: function(option) {
      var _self = this;
      
      var o = {
        control: '[data-script*="all-chec"][data-target]',
        targetAttr: 'data-target',
        checked: true
      };
      
      if(option) {
        for (var key in o) {
          if(option[key] != undefined) {
            o[key] = option[key];
          }
        }
      }
      
      _self.$control = document.querySelectorAll(o.control);
      if(!_self.$control[0]) return;
      
      myScripts.allCheck = myScripts.allCheck || [];
      Array.prototype.forEach.call(_self.$control, function(object, index) {
        myScripts.allCheck.push(new ui_allCheck(object, o));
      });
    }
  };
  
  var act_load = function() {
    var setupAllCheck = new setup_allCheck();
    var setupAllClear = new setup_allCheck({
      control: '[data-script*="all-clear"][data-target]',
      checked: false
    });
  };
  document.addEventListener('DOMContentLoaded', act_load, false);
  
})(this, this.document);

// json list
// ------------------------------------------------------------
(function(window, document, undefined) {
  
  var ui_jsonList = function($component, option) {
    this.$component = $component;
    this.init(option);
  };
  
  ui_jsonList.prototype = {
    init: function(option) {
      var _self = this;
		
      var dataName = _self.$component.getAttribute(option.target);
      _self.data = eval(dataName).body;
      
      if(_self.data == undefined) return;
      
      var master = document.querySelector(option.master);
      if(!master) return;
		
      _self.$search = document.getElementById(option.search);
      if(!_self.$search) return;
		
      _self.$soat = _self.$search.querySelector('select');
      
      _self.master = master.cloneNode(true);
      master.parentNode.removeChild(master);
      
      _self.setup(option.attr);
      
    },
    setup: function(attr) {
      var _self = this;
      
      _self.setList(attr, _self.data);
		
      _self.$component.addEventListener('load', function(e) {
        console.log('load');
      }, false);
		
	  _self.$search.addEventListener('submit', function(e) {
		  
		if(e.currentTarget.sort.value == 'price') _self.data.sort(function(a, b) {
		  if(+a.price < +b.price) return 1;
		  if(+a.price > +b.price) return -1;
		  return 0;
		});
		else if(e.currentTarget.sort.value == 'name') _self.data.sort(function(a, b) {
		  a.kana = _self.kanaCode(a.kana.toString());
		  b.kana = _self.kanaCode(b.kana.toString());
		  if(a.kana < b.kana) return -1;
		  else if(a.kana > b.kana) return 1;
		  return 0;
		});
	
		var filterList = [];
		if(e.currentTarget.name.value != '') {
		  var query = new RegExp(e.currentTarget.name.value, 'g');
		  filterList = _self.data.filter(function(item) {
            return item.name.match(query);
          });
		}
		else {
		  filterList = _self.data;
		}
		  
		   
    	_self.setList(attr, filterList);
	  }, false);
      
	  if(_self.$soat) {
		  _self.$soat.addEventListener('change', function(e) {
			var event = document.createEvent('HTMLEvents');
			event.initEvent('submit', true, false);
			_self.$search.dispatchEvent(event);
		  }, false);
	  }
    },
	setList: function(attr, data) {
      var _self = this;
		
	  _self.$component.innerHTML = '';
		 
      Array.prototype.forEach.call(data, function(item, index) {
        var line = _self.master.cloneNode(true);
        var attrs = line.querySelectorAll('[' + attr + ']');
        attrs = [].slice.call(attrs);
        Array.prototype.forEach.call(attrs, function(object, i) {
          var a = object.getAttribute(attr);
          if(item[a]) object.innerHTML = item[a];
        });
        var disabled = line.querySelectorAll('[disabled]');
        disabled = [].slice.call(disabled);
        Array.prototype.forEach.call(disabled, function($input, i) {
          $input.disabled = false;
        });
        
        _self.$component.appendChild(line);
        
        if(index == (_self.data.length - 1)) {
          Array.prototype.forEach.call(myScripts.allCheck, function(script, index){
            script.destory();
            script.setup();
          });
        }
      });
	},
	kanaCode: function(src) {
      var _self = this;
	  return src.replace(/[\u30a1-\u30f6]/g, function(match) {
	    var char = match.charCodeAt(0) - 0x60;
	  return String.fromCharCode(char);
	});
	}
  };
	
  var setup_jsonList = function(option) {
    this.init(option);
  };
  
  setup_jsonList.prototype = {
    init: function(option) {
      var _self = this;
      
      var o = {
        component: '[data-script*="json-list"][data-source]',
        target: 'data-source',
        attr: 'data-attr',
        master: '[data-master]',
		search: 'search'
      };
      
      if(option) {
        for (var key in o) {
          if(option[key] != undefined) {
            o[key] = option[key];
          }
        }
      }
      
      _self.$component = document.querySelectorAll(o.component);
      if(!_self.$component[0]) return;
      
      myScripts.jsonList = myScripts.jsonList || [];
      Array.prototype.forEach.call(_self.$component, function(object, index) {
        myScripts.jsonList.push(new ui_jsonList(object, o));
      });
    }
  };
  
  var act_load = function() {
    var setupJsonList = new setup_jsonList();
  };
  document.addEventListener('DOMContentLoaded', act_load, false);
  
})(this, this.document);

myScripts.data = {
  "body": [
    {
      "name": "林檎",
      "kana": "リンゴ",
      "price": 1000,
      "id": 0
    },
    {
      "name": "蜜柑",
      "kana": "ミカン",
      "price": 9000,
      "id": 1
    },
    {
      "name": "葡萄",
      "kana": "ブドウ",
      "price": 2000,
      "id": 2
    },
    {
      "name": "桃",
      "kana": "モモ",
      "price": 3000,
      "id": 3
    },
    {
      "name": "梨",
      "kana": "ナシ",
      "price": 1000,
      "id": 4
    }
  ]
};

            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.
Loading ..................

Console