Advertisement
Guest User

Untitled

a guest
Oct 21st, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. $(document).ready(function () {
  2.      $("#select_bank img,.chooseBanks").click(function(){
  3.             $(this).toggleClass("active");
  4.         })
  5. })
  6.  
  7. // функция отрисовки
  8. function drawChecbox(arr, type, insertInDiv) {
  9.     for (let i = 0; i < arr.length; i++) {
  10.         let div_checkbox = document.createElement("div");
  11.         div_checkbox.setAttribute("class", type)
  12.  
  13.         let checkbox = document.createElement("input");
  14.         checkbox.setAttribute("type", "checkbox");
  15.        
  16.         checkbox.setAttribute("id", type + arr[i]["id"]);
  17.  
  18.         let label = document.createElement("label");
  19.         label.setAttribute("for", arr[i]["id"]);
  20.    
  21.         if (type == "bank") {
  22.             label.innerHTML = label.innerHTML = "<img id='img-"+ arr[i].id +"'src='http://osno.online/"+arr[i]['logo']+"'>";   
  23.         } else if (type=="tag") {
  24.             label.innerHTML = arr[i]["name"];
  25.         }
  26.  
  27.         div_checkbox.appendChild(checkbox);
  28.         div_checkbox.appendChild(label);
  29.         document.getElementById(insertInDiv).appendChild(div_checkbox);
  30.     }
  31. }
  32.  
  33. // рисую банки и таги
  34. drawChecbox(banks, "bank", "select_bank");
  35. drawChecbox(tags, "tag", "tags");
  36.  
  37.  
  38.  
  39. // функция отрисовки выбранного
  40. function drawChoose(i, innerDiv) {
  41.     var div_rate = document.createElement("div");
  42.     div_rate.setAttribute("class", "rate");
  43.  
  44.     var name_rate = document.createElement("h5");
  45.     name_rate.innerHTML = "[" + rates[i].id + "]" + rates[i].name;
  46.  
  47.     var p_rate = document.createElement("p");
  48.     p_rate.innerHTML = rates[i].description+"<br><br><strong> Обслуживание: "+rates[i].price+" руб.</strong>";
  49.  
  50.     var bank_rate = document.createElement("p");
  51.     bank_rate.setAttribute("class", "bank_logo");
  52.     bank_rate.innerHTML = "<img src='http://osno.online/"+banks[rates[i].id_bank-1].logo+"'>";
  53.  
  54.     var a_rate = document.createElement("a");
  55.     a_rate.innerHTML = "Подробнее" + "[" + rates[i].tags + "]";
  56.  
  57.     div_rate.appendChild(name_rate);
  58.     div_rate.appendChild(bank_rate);
  59.     div_rate.appendChild(p_rate);
  60.     div_rate.appendChild(a_rate);
  61.     document.getElementById(innerDiv).appendChild(div_rate);
  62. }
  63.  
  64.  
  65. for (let i = 0, end = rates.length; i < end; i++) {
  66.     rates[i]["cov"] = 0;
  67. }
  68.  
  69.  
  70.  
  71. for (let i = 0, endI = rates.length - 1; i < endI; i++) {
  72.     let wasSwap = false;
  73.     for (let j = 0, endJ = endI - i; j < endJ; j++) {
  74.         if (parseInt(rates[j].price) > parseInt(rates[j+1].price)) {
  75.             [rates[j], rates[j+1]] = [rates[j+1], rates[j]];
  76.             wasSwap = true;
  77.         }
  78.     }
  79.    
  80.     if (!wasSwap) break;
  81. }
  82.  
  83.  
  84. go('Все предложения');
  85.  
  86. function go(labelResult) {
  87.     // обновление 3х блоков
  88.     document.getElementById("p100").innerHTML ="<h5 style='width: 100%;'>" + labelResult + "</h5>";
  89.     document.getElementById("from1to99p").innerHTML = "<h5 style='width: 100%;'>Похожие результаты</h5>";
  90.     document.getElementById("other_banks").innerHTML = "<h5 style='width: 100%;'>Предложения от других банков</h5>";
  91.  
  92.     // counter
  93.     let countTag = 0;
  94.  
  95.     for (let i = 0; i < tags.length; i++) {
  96.  
  97.         if (document.getElementById("tag" + tags[i].id).checked) {
  98.             countTag += 1;
  99.             for (let j = 0; j < rates.length; j++) {
  100.                    
  101.                 if (rates[j].tags.split(",").includes(tags[i].id)) {
  102.                     rates[j].cov += 1;
  103.                 }
  104.             }
  105.         }
  106.    
  107.     }
  108.    
  109.     // allBanksChecked
  110.     let checkBanks = document.getElementsByClassName("active");
  111.  
  112.     let checkBanksId = [];
  113.  
  114.  
  115.     for (var i = 0; i < checkBanks.length; i++) {
  116.         checkBanksId.push(document.getElementsByClassName("active")[i].id.split("-")[1]);
  117.     }
  118.    
  119.     console.log('checkBanksId :');
  120.     console.log(checkBanksId);
  121.  
  122.     let p100 = 0;
  123.     let from1to99p = 0;
  124.     let other_banks = 0;
  125.  
  126.     // drawing response
  127.     for (var i = 0; i < rates.length; i++) {
  128.     console.log(rates[i].tags.split(","));
  129.         if (countTag) {
  130.             rates[i].cov = (rates[i].cov / countTag) * 100;
  131.         }
  132.         console.log(checkBanksId.includes(rates[i].id_bank));  
  133.                
  134.         if (checkBanksId.length == 0 || checkBanksId.includes(rates[i].id_bank)) {
  135.             console.log('RISUEM');
  136.             if (rates[i].cov == 100 ||(checkBanksId.length == 0 && rates[i].cov==0) || (checkBanksId.length != 0 && rates[i].cov==0)) {
  137.                 p100 += 1
  138.                 drawChoose(i, "p100");
  139.  
  140.             } else if (rates[i].cov < 100 && rates[i].cov != 0 && from1to99p != 3) { // Условия для отбора похожих можно сменить на не лояльные
  141.                 from1to99p += 1;
  142.                 drawChoose(i, "from1to99p");
  143.             }
  144.         } else {
  145.             if (rates[i].cov == 100) {
  146.                 other_banks += 1;
  147.                 drawChoose(i, "other_banks");
  148.             }
  149.         }
  150.         rates[i].cov = 0;
  151.     }
  152.  
  153.     if (!p100) {
  154.         document.getElementById("p100").innerHTML = "<h5 style='width: 100%;'>Результатов не найдено</h5>";
  155.     }
  156.  
  157.     if (!from1to99p) {
  158.         document.getElementById("from1to99p").innerHTML = "";
  159.     }
  160.  
  161.     if (!other_banks) {
  162.         document.getElementById("other_banks").innerHTML = "";
  163.     }
  164. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement