/
SoRusV
/
src_xendil
Обзор
Документация
Войти
/
SoRusV
/
src_xendil
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
html/src/dynamicTable.js
325 строк
12 KB
ruslan
Initial commit
29 окт 2024, 11:18
29 окт 2024, 11:18
5c4dcf5
Код
Авторство
О чём код?
function CopyRow(RowFrom, RowTo) { // console.log(RowFrom) //console.log(RowTo) let EsFrom = RowFrom.getElementsByTagName("*"); let EsTo = RowTo.getElementsByTagName("*"); for (let i = 0; i < EsFrom.length; i++) { let EF = EsFrom.item(i); // let ET = EsTo.item(i) if (EF.id) { let ET; for (let i2 = 0; i2 < EsFrom.length; i2++) if (EsTo.item(i2) && EF.id == EsTo.item(i2).id) { ET = EsTo.item(i2); break; } else if (!EsTo.item(i2)) i = i; if (!ET) continue; // console.log(EF.id) // console.log(ET.id) // console.log(EF.tagName) if (EF.type == "checkbox") ET.checked = EF.checked; // else if (E.tagName == "SELECT") // E.T.selectedIndex // if (E.tagName == "DIV") // return E.innerHTML else if (EF.tagName == "INPUT") { ET.value = EF.value // ET.value = i // EF.value = i } } } } function FindIndexRow(TBody, Obj) /// return index of row in table with consist Obj { for (let iR = 0; iR < TBody.rows.length; iR++) { let Row = TBody.rows[iR]; // if (Row == Obj.parent) // alert(iR) let elements = Row.getElementsByTagName("*"); for (let i = 0; i < elements.length; i++) { if (elements.item(i) == Obj) { // alert(iR) return iR } } } } function ReIndexRow(TBody) { // if(LockReIndexRow) // return; for (let iR = 0; iR < TBody.rows.length; iR++) { let Row = TBody.rows[iR]; // if (Row == Obj.parent) // alert(iR) let elements = Row.getElementsByTagName("*"); for (let i = 0; i < elements.length; i++) { if (elements.item(i).id == "@T{Index}") { ElementSetValue(elements.item(i), iR); break; } } // if (typeof MakeRow !== "undefined" && iR<GetDynamicTBodyRowCount(TBody)) // MakeRow(Row, iR, TBody.id); } } function onIndexRowChange(idTBody, ObjInputIndex) { TBody = document.getElementById(idTBody); // console.log("+++") // console.log(TBody) // console.log(ObjInputIndex) if (ObjInputIndex.tagName == "INPUT") { iROld = FindIndexRow(TBody, ObjInputIndex); iRNew = ObjInputIndex.value; // console.log(iRNew) iRNew = Number(iRNew); // console.log(iRNew) if (iRNew < 0 || iRNew >= TBody.rows.length) iRNew = iROld; // console.log(iROld) // console.log(iRNew) if (iROld != iRNew) TBody.insertBefore(TBody.rows[Math.max(iRNew, iROld)], TBody.rows[Math.min(iROld, iRNew)]); // if (iROld > iRNew) // TBody.insertBefore(TBody.rows[iROld], TBody.rows[iRNew]); ObjInputIndex.Value = iRNew; ReIndexRow(TBody) } } let LockPaintRow = false; function PaintRow(TBody, RowCount) /// this function show or hide rows[0] { // if(LockPaintRow) // return; // if (DynamicTBodyRowCount < 0) // return; // console.log("DynamicTBodyRowCount =" + DynamicTBodyRowCount) // console.log("RowCount=" + RowCount) if (RowCount > 0)// && TBody.rows.length <= 1) /// show row { console.log(1); let elements = TBody.getElementsByTagName("*"); // for (let i = 0; i < elements.length; i++) { // // if (elements.item(i).id == "@T{Index}") // elements.item(i).style.display = "inline"; // // if (RowCount ==1)// && TBody.rows.length <= 1) /// show row // // { // // if (elements.item(i).nodeName != "TD" && elements.item(i).nodeName != "TR" && elements.item(i).nodeName != "BUTTON") // // // if (elements.item(i).nodeName == "TD" && elements.item(i).className == "") // // elements.item(i).style.display = "inline"; // // } // } for (let i = 0; i < elements.length; i++) if (elements.item(i).nodeName != "TD" && elements.item(i).nodeName != "TR" && elements.item(i).className != "add" && elements.item(i).className != "del" && elements.item(i).nodeName != "NOBR") // if (elements.item(i).nodeName == "TD" && elements.item(i).className == "") elements.item(i).style.display = "inline" } else if (RowCount == 0 && TBody.rows.length > 0) /// hide row { console.log(2); let elements = TBody.getElementsByTagName("*"); for (let i = 0; i < elements.length; i++) if (elements.item(i).nodeName != "TD" && elements.item(i).nodeName != "TR" //&& elements.item(i).nodeName != "BUTTON" && elements.item(i).className != "add" && elements.item(i).className != "del" && elements.item(i).nodeName != "NOBR") // if (elements.item(i).nodeName == "TD" && elements.item(i).className == "") elements.item(i).style.display = "none" } // DynamicTBodyRowCount = RowCount // console.log("DynamicTBodyRowCount =" + DynamicTBodyRowCount) } function TBodyRowCountSet(TBody, RowCount) { // PaintRow(TBody, RowCount) //let T= document.getElementById("dynamic"); LockPaintRow = true; let RowCountNow = GetDynamicTBodyRowCount(TBody); // if(RowCountNow==RowCount) // return; let R = TBody.rows[TBody.rows.length - 1]; let elements = R.getElementsByTagName("*"); // if(RowCountNow<RowCount) for (let i = 0; i < elements.length; i++) if (elements.item(i).id == "idButAdd") { let E = elements.item(i); while (TBody.rows.length < RowCount) E.click(); break; } while (TBody.rows.length > RowCount) { R = TBody.rows[TBody.rows.length - 1]; elements = R.getElementsByTagName("*"); let L = TBody.rows.length; for (i = 0; i < elements.length; i++) if (elements.item(i).id == "idButDel") elements.item(i).click(); if (L == 1) break; } LockPaintRow = false; PaintRow(TBody, RowCount); } //TBodyRowInterchange() function GetDynamicTBodyRowCount(TBody) { if(!TBody) { let a = 0; } if (TBody.rows.length != 1) return TBody.rows.length; let elements = TBody.rows[0].getElementsByTagName("*"); for (let i = 0; i < elements.length; i++) if (elements.item(i).id == "@T{Index}") { let E = elements.item(i); if (E.style.display == "inline") return 1; } return 0; } let DynamicTBody = (function (GLOB) { let RID = 0; let DynamicTBodyRowCount = 1; // let A; return function (TBody) { /* Если ф-цию вызвали не как конструктор фиксим этот момент: */ if (!(this instanceof arguments.callee)) { return new arguments.callee.apply(arguments); } //Делегируем прослушку событий элементу tbody TBody.onclick = function (e) { let evt = e || GLOB.event, trg = evt.target || evt.srcElement; // let v=trg.className.indexOf("add"); let t = trg.parentNode;//.parentNode; if (trg.className && trg.className.indexOf("add") != -1) { while (t && t.nodeName != "TR") t = t.parentNode; _addRow(t, TBody); // A(trg); } else if (trg.className && trg.className.indexOf("del") != -1) { //TBody.rows.length > 1 && while (t && t.nodeName != "TR") t = t.parentNode; _delRow(t, TBody); } }; let _rowTpl = TBody.rows[0].cloneNode(true); // Корректируем имена элементов формы let _correctNames = function (row) { let elements = row.getElementsByTagName("*"); for (let i = 0; i < elements.length; i += 1) { if (elements.item(i).name) { if (elements.item(i).type && elements.item(i).type === "radio" && elements.item(i).className && elements.item(i).className.indexOf("glob") !== -1) { //+ elements.item(i).value = RID; // elements.item(i).style.backgroundColor="#00FF00"; // elements.item(i).style.Color="#00FF00"; } else { //+ elements.item(i).name = elements.item(i).name+ "["+RID+"]"; ///elements.item(i).style.backgroundColor="#FF0000"; // elements.item(i).style.Color="#FF0000"; // elements.item(i).value = elements.item(i).value+ "["+RID+"]"; } } // if(elements.item(i).className=="add") // elements.item(i).onload(elements.item(i)); } RID++; return row; }; //function OnLoadRow(Row) //{ // let elements = Row.getElementsByTagName("*"); // for (let i = 0; i < elements.length; i += 1) // if(elements.item(i).className=="add") // { // if(elements.item(i).onload) // elements.item(i).onload(elements.item(i)); // break; // } //}; let _addRow = function (before, TBody) // let _addRow = function (before, TBody) { let newNode; // let bNeedAdd=false; // if(TBody.rows.length ==1){ // // let t=TBody.rows[0].getElementById("@T{Index}"); // let elements = TBody.rows[0].getElementsByTagName("*"); // for (let i = 0; i < elements.length; i++) // if (elements.item(i).id == "@T{Index}") { // let E=elements.item(i); // bNeedAdd= (E.style.display == "inline"); // break; // } // } let RC = GetDynamicTBodyRowCount(TBody); // if (TBody.rows.length > 1 || bNeedAdd) if (RC > 0) { newNode = _correctNames(_rowTpl.cloneNode(true)); TBody.insertBefore(newNode, before.nextSibling); //console.log("++++++++++++++") //console.log(newNode.id) //console.log(newNode) //console.log(newNode.parentNode) //console.log(newNode.parentNode.parentNode) //console.log(before) //console.log("--------------") CopyRow(before, newNode); } else newNode = TBody.rows[0]; PaintRow(TBody, RC + 1); console.log(newNode); ReIndexRow(TBody); if (typeof MakeRow !== "undefined") for (let iR = 0; iR < TBody.rows.length; iR++) if(newNode == TBody.rows[iR]){ MakeRow(newNode, iR, TBody.id); break; } // aa(newNode) // if (MakeRow) // MakeRow(newNode); // OnLoadRow(newNode); }; let _delRow = function (row, TBody) { if (TBody.rows.length > 0) { // console.log("del") PaintRow(TBody, TBody.rows.length - 1); if (TBody.rows.length > 1) TBody.removeChild(row); } ReIndexRow(TBody); // if (MakeRow) // MakeRow(newNode); }; _correctNames(TBody.rows[0]); // OnLoadRow(tBody.rows[0]); }; })(this);