Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // array containing ID selected to anonimization
- var names = [];
- // array containing primary keys names for table (column captions)
- var pknames = [];
- // column headers (captions) of table
- var tHeadNames = [];
- // cell values for specific record
- var cellValues = [];
- // Index of pk in thead array
- var pkPositions = [];
- // row objects
- var rowDOMs = [];
- //tableID
- var tableId;
- // json result
- var json;
- // set counter (json inner array counter)
- var setCounter = 0;
- /// Button click - sprawdz checki
- $("#checkboxes").click(function () {
- console.log("array.Names: " + names);
- });
- // Function call on every row checkbox change
- function addRowToAnonimizeList(cb, boxId) {
- if (cb.checked) {
- names.push(boxId)
- } else {
- var index = names.indexOf(boxId);
- names.splice(index, 1);
- }
- }
- // function call on button click id="get" - Wyświetl
- // get table data from database and send result to div #dispresult in shared view _tableDisplay.cshtml
- function getTableFromDB(tableId) {
- $.ajax({
- url: "/tablenames/kar",
- data: {
- "id": tableId
- },
- type: 'GET',
- success: function (result) {
- $('#dispresult').html(result);
- },
- error: function (result) {
- console.log("Error with sending to tablenames controler");
- $('#dispresult').html("Error.");
- }
- });
- }
- //function call when button clicked to anonimize DB
- function anonimizeDBbyID(dbID) {
- var operationType = 1;
- var dataObject = JSON.stringify({
- 'dbId': dbID,
- 'opType': operationType
- });
- $.ajax({
- url: "/ManageActions/AnonimizeDatabase",
- method: 'POST',
- data: dataObject,
- contentType: 'application/json; charset=utf-8',
- success: function (result) {
- $('#dispresult').html("Sucessfuly Anonimized Database id" + dataObject);
- },
- error: function (result) {
- $('#dispresult').html("Error with anonimizing database");
- }
- });
- }
- // fucntion call when clicked anonimize witch selected rows (checkboxes from table)
- function AnonimizeRecordsByID(tableID) {
- tableId = tableID;
- if (names.length > 0) {
- getPrimaryKeysCaptions(tableID)
- } else {
- $('#dispresult').html("Nie zaznaczono żadnego rekordu.");
- }
- }
- ///////////////////////////////////function call on anonimize selectod only.
- // Step 1 - get Priamry keys names (captions only) for table.
- function getPrimaryKeysCaptions(tableID) {
- $.ajax({
- url: "/tablenames/getPKnames",
- data: {
- "id": tableID
- },
- type: 'GET',
- success: function (result) {
- if (result === "none") {
- $('#dispresult').html("Tabela nie posiada kluczy głównych. <br> Nie można wykonać operacji. ");
- } else {
- pknames = result.split(',');
- getRowsFromHTMLTable()
- }
- },
- error: function (result) {
- console.log("Error while getting PK from server" + error);
- }
- });
- }
- //Step 2. Selected single row values (id of rows selected by user - checkboxes)
- function getRowsFromHTMLTable() {
- var DOMtable = document.getElementById("databaseTable");
- // get rowDOM array from table
- for (var rowIterator = 0; rowIterator < names.length; rowIterator++) {
- var singleRow = DOMtable.rows[names[rowIterator]]
- rowDOMs.push(singleRow)
- }
- // Start building Json with data for server.
- // Table ID is same for every row.
- // function ddRowPKValuesToJson call (every row call) adds new object to JSON
- json = '{"opType": 1, "tableId":' + tableId + ', "UpdateKeyValuesParameters": [ '
- //json = '{['
- // get table columns names
- getHeadersFromHTMLTable(DOMtable)
- //for every row call function.
- rowDOMs.forEach(addRowPKValuesToJson)
- // Cut last coma before closing
- json = json.slice(0, json.length - 1);
- // Close JSON structure
- json = json + ']}'
- obj = JSON.parse(json)
- sendJSONtoDB()
- // display JSON to console.
- console.log(obj)
- // clear arrays and vars before next operation.
- tHeadNames.length = 0;
- pknames.length = 0;
- pkPositions.length = 0;
- rowDOMs.length = 0;
- cellValues.length = 0;
- json = "";
- setCounter = 0;
- }
- function getHeadersFromHTMLTable(DOMtable) {
- // get table column names
- var headersDOM = DOMtable.getElementsByTagName("th")
- // parse DOM object in array to strings
- for (var arrayIterator = 0; arrayIterator < headersDOM.length; arrayIterator++) {
- tHeadNames[arrayIterator] = headersDOM[arrayIterator].textContent
- }
- // delete first element from array - checkbox value. It doesnt exists in native database
- tHeadNames.splice(0, 1)
- }
- // function fired for every row (every element in rowsDOMs array.)
- function addRowPKValuesToJson(value, index) {
- // increment set (identifier for keys as they belong to one item-row in database)
- setCounter += 1;
- // parse row cells to array.
- var cellValuesDOM = value.getElementsByTagName("td")
- // parse DOM object in array to strings
- for (var arrayIterator = 0; arrayIterator < cellValuesDOM.length; arrayIterator++) {
- cellValues[arrayIterator] = cellValuesDOM[arrayIterator].textContent
- }
- // delete first element from array - checkbox value. It doesnt exists in native database
- cellValues.splice(0, 1)
- // Compare tables: theadNames with pknames. When compare is positive get value from cellValues by index from tHeadNames
- for (var arrayIterator = 0; arrayIterator < pknames.length; arrayIterator++) {
- pknames[arrayIterator] = pknames[arrayIterator].toUpperCase()
- for (var headersIterator = 0; headersIterator < tHeadNames.length; headersIterator++) {
- tHeadNames[headersIterator] = tHeadNames[headersIterator].toUpperCase()
- if (tHeadNames[headersIterator] == pknames[arrayIterator]) {
- // add to JSON key and its value.
- //json = json + '{"Name": "' + tHeadNames[headersIterator] + '", "Value": ' + cellValues[headersIterator]+'},'
- json = json + '{"Name": "' + tHeadNames[headersIterator] + '", "Value": '
- if (isNaN(cellValues[headersIterator])) {
- json = json + '"'+cellValues[headersIterator] + '"'
- }else {
- json = json + cellValues[headersIterator]
- }
- json = json + ', "set": ' + setCounter + '},'
- }
- }
- }
- }
- function sendJSONtoDB() {
- var dataJson = JSON.stringify(json)
- /// #region DEBUG - do usuniecia
- var SqlStatementParameter = [
- { Name: 'ID_ADRES', Value: 5, set: 1 }
- ];
- var ParamModel =
- { opType: 1, tableId: 225, UpdateKeyValuesParameters: SqlStatementParameter}
- var ParamStringified = JSON.stringify(ParamModel)
- var cont = '<h1><font color ="red">TODO.</font></h1><br>' +
- 'JSON jest budowany : <br><font color ="blue" style="font-family: Consolas; font-size:80%;"> '+ json +'</font><br>' +
- 'Jest problem w przesyłaniu do kontrolera <mark>/ManageActions/AnonimizeRows. </mark><br>' +
- 'JSON wyslany AJAXem nie jest odbierany w kontrolerze (przyjmowana wartosc jest null)' +
- '<br><br><font style="font-family: Courier; font-size:90%;">Do testow: Tabela UZYTKOWNICY2UPRAWNIENIA zawiera 3 klcuze PK <br> ' +
- 'tabela WZOR_OSOBY nie zawiera żadnych kluczy </font>' +
- '<br><br>stringified:: ' + dataJson
- $('#dispresult').html(cont);
- /// #endregion
- console.log("json ready to send: "+json)
- $.ajax({
- url: "/ManageActions/AnonimizeRows",
- method: 'POST',
- data: dataJson,
- contentType: 'application/json; charset=utf-8',
- dataType: 'json',
- success: function (result) {
- console.log("Sucessfuly Anonimized rows " + result);
- },
- error: function (result) {
- console.log("Error with anonimizing selected rows. "+result.statusText);
- }
- });
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement