多層級表格js
阿新 • • 發佈:2018-06-13
asi sub list IV als HR helper per save
self.FishConfigList = ko.observableArray();
self.GetFishConfigList = function (dept_code, report_id) {
self.FishConfigList.removeAll();
var objdata = { DEPT_CODE: dept_code, REPORT_ID: report_id };
KEAPIHelper.GetMeaasge(new ActionUrl.Quality().GetFishConfigList, objdata, function (data) {
//Success
if (data.Data) {
self.FishConfigList(data.Data);
//將數據加載進來
var _data = $.map(data.Data, function(item) {
return item;
});
function loadHtml(_data){
var str = ‘‘;
$.each(_data,function(i, item) {
var _string = ‘‘;
var _strings = ‘‘;
if(item.WIDGETLIST==null){
}else{
$.each(item.WIDGETLIST,function(i, ite){
_string += ite.WIDGET_NAME+‘$‘;
_strings+= ite.WIDGET_ID+‘$‘;
});
}
var pid = item.PARENT_ID ? ‘data-tt-parent-id=‘ + item.PARENT_ID : ‘‘;
str += ‘<tr data-tt-id="‘ + item.ID + ‘" ‘ + pid + ‘ data-tt-branch="‘ + (item.LEAF ? "false" : "true") + ‘">‘ +
‘<td class="titles" data-select="‘+ _string +‘" , data-widid="‘+ _strings +‘">‘ +
(item.LEAF ? ‘<span class="file" style=" padding: 13px 0 13px 0;"></span>‘ : ‘<span class="folder" style=" padding: 13px 0 13px 0;"></span>‘) + item.FISH_NAME + ‘</td>‘ +
‘<td class="active show-name">‘ + item.FISH_SHOW_NAME + ‘</td>‘ +
‘<td class=" fish-level">‘ + item.FISH_LEVEL + ‘</td>‘ +
‘<td ><a href="#" class="del">刪除</a> <a href="#" class="add">添加子節點</a></td>‘ +
‘</tr>‘;
});
return str;
}
var html = loadHtml(_data);
//添加開始
$(‘#example-basic‘).find(‘tbody‘).html(loadHtml(_data));
$(function(){
var counter = 0;
//添加
self.add = function add(e) {
e.preventDefault();
var id = $(this).closest("tr").data("ttId");
var FISH_LEVEL = parseFloat($(this).closest("tr").find(‘td:eq(2)‘).text());
var FISH_NAME = $(this).closest("tr").find(‘td:eq(0) input‘).val()|| $(this).closest("tr").find(‘td:eq(0)‘).text();
var index = 0;
// 換圖標
$.map(_data, function (item, i) {
if (item.ID === id) {
if (_data[i + 1] && typeof _data[i + 1].ID === ‘string‘) {
counter = _data[i + 1].ID.split(‘-‘)[1];
}
_data[i].LEAF = false;
index = i;
}
});
// 請求新建選擇項
self.GetFishConfigSubList(FISH_NAME, FISH_LEVEL, function(data) {
_data.splice(index + 1, 0, {
PARENT_ID: id,
FISH_LEVEL:FISH_LEVEL+1,
ID: 0,
REPORT_ID:0,
FISH_NAME:‘請選擇...‘,
FISH_SHOW_NAME: ‘‘,
LEAF: true,
DEPT_CODE: "54",
WIDGETLIST: $.extend({}, data),
});
console.log(_data);
$(‘#example-basic‘).find(‘tbody‘).html(loadHtml(_data));
$("#example-basic").treetable("destroy");
$("#example-basic").treetable({expandable: true});
//添加字節點,讓點擊的節點展開
$("#example-basic").treetable("expandNode", id);
});
};
});
//添加結束
$(‘#example-basic‘).find(‘tbody‘).html(html);
//
$("#example-basic").treetable("destroy");
// //添加字節點,讓點擊的節點展開
$("#example-basic").treetable({
expandable: true,
//展開
onNodeExpand: function () {
var node = this;
}
})
//中間td添加input
.on("click", "td.active", function () {
var $input = $(this).find(‘input‘);
var value = $(this).text();
if ($input.length > 0) {
value = $input.val();
return;
}
$(this).html(‘<input> ‘);
$(this).find(‘input‘).val(value).focus();
//添加下拉菜單
})
// 第一列有按鈕的td 添加input
.on("click", "td.titles", function (e) {
var $input = $(this).find(‘input‘);
var value = $(this).text().trim();
var $html = $(this).find(‘span‘).clone(true);
if ($input.length > 0) {
value = $input.val();
return;
}
$(this).html(‘‘);
$(this).append($html);
$(this).append(‘<input type="text" style="max-width:56%;" disabled="disabled" / >‘);
$(this).find(‘input‘).val(value).focus();
// //添加下拉菜單
$(this).append(‘<span class="icon-down" style="display:flex; z-index:25; /*padding-left: 285px;*/ padding-left:80%;"></span><ul style="display: none;" class="drop-menus" ></ul>‘);
if( $(this).attr(‘data-select‘)==‘‘){
//第一行和倒數第一行沒有數據
$(this).find(‘ul.drop-menus‘).removeAttr(‘style‘);
$(this).find(‘ul.drop-menus‘).attr(‘style‘,‘display:none‘);//沒有隱藏
}else if( $(this).attr(‘data-select‘)!==‘‘){
//有數據行
$(this).find(‘span.icon-down‘).show();
console.log($(this).attr(‘data-select‘));
$(this).attr(‘data-select‘).split(‘$‘);
var arrs = $(this).attr(‘data-select‘).split(‘$‘);
var liids = $(this).attr(‘data-widid‘).split(‘$‘);
for(var i=0,j=0; i<arrs.length,j<liids.length; i++,j++){
$(this).find(‘ul.drop-menus‘).append("<li class=‘drop-item‘ id="+liids[j]+ ">"+ arrs[i] +"</li>");
}
}
})
// 第一列有按鈕的td 添加input
.on("click", "td .del", self.delete)
.on("click", "td .add", self.add)
.on(‘click‘,‘td span.icon-down‘,function(e) { //點擊下拉按鈕讓ul顯示,別的ul隱藏
e.stopPropagation();
// $(‘ul.drop-menus‘).hide();
console.log($(this));
var tds =$(this).closest(‘td‘)
tds.parent().siblings().find(‘td:eq(0)‘).find(‘ul.drop-menus‘).hide();
tds.find(‘li.drop-item‘).parent(‘ul.drop-menus‘).toggle();
})
.on(‘click‘,‘li.drop-item‘,function(e){ //點擊li將值傳input,同時讓ul隱藏
e.stopPropagation();
var _this = $(this);
var litext =_this.text();
var inputVal =_this.closest(‘td‘).find(‘input‘);
var tdText = _this.closest(‘td‘);
inputVal.val(litext);
_this.closest(‘td‘).find(‘ul.drop-menus‘).hide();
})
.on(‘click‘,‘tr‘,function(e){ //點擊表格任意處讓下拉菜單隱藏
// e.stopPropagation();
$(‘ul.drop-menus‘).hide();
})
}
else {
self.FishConfigList.removeAll();
}
}, function (e) {
//error
self.FishConfigList.removeAll();
}, false);
};
//獲取可選子項配置項list
self.FishConfigSubList = ko.observableArray();
self.GetFishConfigSubList = function (FISH_NAME, FISH_LEVEL, callback) {
self.FishConfigSubList.removeAll();
var objdata = { FISH_NAME: FISH_NAME, FISH_LEVEL: FISH_LEVEL, REPORT_ID: self.selected_report_id() };
KEAPIHelper.GetMeaasge(new ActionUrl.Quality().GetFishConfigSubList, objdata, function (data) {
//Success
if (data.Data) {
self.FishConfigSubList(data.Data);
if(callback) {
callback(data.Data);
}
}
else {
self.FishConfigSubList.removeAll();
}
}, function (e) {
//error
self.FishConfigSubList.removeAll();
}, false);
}
//保存
self.save = function save() {
console.log(‘1111111111111‘);
var data = [];
// 找出所有的行
var $trArr = $("#example-basic tbody tr");
// 循環每一行
$.map($trArr, function(tr) {
// 找出每一列
var $tr = $(tr);
var tdArr = $tr.children();
// 當前列中的input
var input = $tr.find(‘input‘);
// 當前行中的數據
var child = {};
child.DEPT_CODE = self.dept_code();
child.ID = child.REPORT_ID = self.selected_report_id();
// child.ID = $tr.attr(‘data-tt-id‘);
child.PARENT_ID = $tr.attr(‘data-tt-parent-id‘);
child.LEAF = $tr.attr(‘data-tt-branch‘);
$.map(tdArr, function(td, i) {
var $td = $(td);
var ipt = $td.find(‘input‘);
var text = (ipt.length > 0) ? ipt.val() : $td.text();
if($td.hasClass(‘titles‘)) {
child.FISH_NAME =text
} else if($td.hasClass(‘show-name‘)) {
child.FISH_SHOW_NAME = text
} else if($td.hasClass(‘fish-level‘)) {
child.FISH_LEVEL = text
}
});
data.push(child);
});
//console.log(data, ‘this is data‘);
return data;
};
//刪除
self.delete = function () {
var id = $(this).closest("td").parent().data("ttId");
parent.layer.confirm(‘刪除操作無法恢復,確定要刪除所有子節點?‘, {
btn: [‘刪除‘, ‘取消‘], //按鈕
shade: true,
shade:0.3,//不顯示遮罩
}, function () {
//確定
var objdata = { ID: id };
KEAPIHelper.GetMeaasge(new ActionUrl.Quality().DeleteFishConfig, objdata, function (data) {
//Success
if (data.Data == true) {
KECore.UI.success("刪除成功");
self.GetFishConfigList(self.dept_code(), self.selected_report_id());
}
else {
KECore.UI.error("刪除失敗!");
}
layer.closeAll(‘dialog‘);//關閉彈出框
}, function (e) {
//error
KECore.UI.error("刪除失敗!");
}, false);
}, function () {
//取消
});
}
多層級表格js