0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Bootstrap4 メニュー作成関数

Posted at

メモ用

あれ?うまく動かないや?
なぜだ…、ま、いっか。

index.html
<script>
    ceate_menu("#get_dept_id", dept_id, 10, {arrKySrt:dept_srt});   
</script>

<div class="btn-group">
    <button class="btn dropdown-toggle" type="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
        お菓子メーカ一覧
    </button>
    <div id="sel_dept" class="dropdown-menu"></div>
</div>
javascript.js
/*
Bootstrap4 メニュー作成関数
tarSel: Bt4 対象のメニューのセレクタ
arrKyVal: {key:val,key:val,} 
actId: アクティブなID
obj:{
	arrKySrt: {1:KeyX,2:KeyY,} keyがソート順 ValがarrKyValのKeyにあたる(dept_id)
}
*/
function ceate_menu(tarSel,arrKyVal,actId,obj){
	let snippet = "";
	//console.log([tarSel,arrKyVal,actId,obj]);
	for(var i in obj.arrKySrt){

		if(arrKyVal[obj.arrKySrt[i] ]===undefined){
			snippet +=	'<div class="dropdown-divider"></div>\n';
		}else{
			snippet += 	'<button type="button" class="dropdown-item '+ (actId==obj.arrKySrt[i]?"active":"") +'" type="button" value="'
				+ 	obj.arrKySrt[i]+'">'+arrKyVal[obj.arrKySrt[i] ]+'</button>\n';
		}
	}
	$(tarSel+" .dropdown-menu").append(snippet);
}


const dept_id={
	8  :"グリコ",
	7  :"ブルボン",
	9  :"ロッテ",
	11 :"カルビ"
}
const dept_srt={
	1: 11, 
	2: 8, 
	3: 7, 
	4: 0, 
	5: 9
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?