Parcourir la source

量表添加scaleType为2的类型和互斥

zhangxc il y a 5 ans
Parent
commit
6a12fa820a
2 fichiers modifiés avec 162 ajouts et 52 suppressions
  1. 16 1
      src/html/scale.html
  2. 146 51
      src/js/scale.js

Fichier diff supprimé car celui-ci est trop grand
+ 16 - 1
src/html/scale.html


+ 146 - 51
src/js/scale.js

@@ -35,62 +35,28 @@ $(function () {
           }
         }
 
-        $('input').on('change', function (e) {
-          var indexList = $(this).attr('data-index').split(',')
-          for (var i = 0; i < scaleList.group[indexList[0]].rows[indexList[1]].row[indexList[2]].details.length; i++) {
-            scaleList.group[indexList[0]].rows[indexList[1]].row[indexList[2]].details[i].select = 0
-          }
-          scaleList.group[indexList[0]].rows[indexList[1]].row[indexList[2]].details[indexList[3]].select = 1
-          const className = $(this).parent().parent().parent().attr('data-group')
-          for (let i = 0; i < $('.' + className).length; i++) {
-            $('.' + className).eq(i).removeClass('noSelect')
-          }
-        })
-
-        $('.calcuBtn').on('click', function () {
-          let allSelect = true
-          for (let i = 0; i < scaleList.group.length; i++) {
-            for (let j = 0; j < scaleList.group[i].rows.length; j++) {
-              if (scaleList.group[i].rows[j].required == 1) {
-                let itemSelect = false
-                for (let x = 0; x < scaleList.group[i].rows[j].row.length; x++) {
-                  for (let y = 0; y < scaleList.group[i].rows[j].row[x].details.length; y++) {
-                    if (scaleList.group[i].rows[j].row[x].details[y].select == 1) {
-                      itemSelect = true
-                    }
-                  }
-                }
-                if (!itemSelect) {
-                  allSelect = false
-                  $('.' + i + j).addClass('noSelect')
-                }
-              }
+        
 
-            }
-          }
-          if (!allSelect) {
-            $('.modal').css('display', 'block');
-            let timer = setTimeout(() => {
-              $('.modal').css('display', 'none');
-            }, 2000);
-            $('.closeModal').click(function () {
-              $('.modal').css('display', 'none');
-              if (timer) {
-                clearTimeout(timer)
-              }
-            })
-          }
-          if (allSelect) {
-            getCalcuResult(scaleList)
-          }
-        })
+        
       }
     })
   }
 })
 
 function renderCalcu(scaleList) {
+  
   $("h1").html(scaleList.scaleName);
+  if(+scaleList.scaleType === 1 && +scaleList.Calc === 1) {
+    renderScaleType1(scaleList)
+    bindScaleType1(scaleList)
+  } else if(+scaleList.scaleType === 2 && +scaleList.Calc === 0) {
+    renderScaleType2(scaleList)
+    bindScaleType2(scaleList)
+  }
+  
+}
+
+function renderScaleType1(scaleList) {
   var str = ''
   for (var j = 0; j < scaleList.group.length; j++) {
     str += '<div class="groupBox"> <div class="groupName">' + scaleList.group[j].groupName + '</div>';
@@ -98,17 +64,23 @@ function renderCalcu(scaleList) {
       for (var y = 0; y < scaleList.group[j].rows[x].row.length; y++) {
         var str2 = ''
         for (var z = 0; z < scaleList.group[j].rows[x].row[y].details.length; z++) {
-          str2 += '<span class="groupRowRadio"><input  type="radio" data-index="' + j + ',' + x + ',' + y + ',' + z + '"  name="' + j + x + y + '" id="' + j + x + y + z
+          str2 += '<span class="groupRowRadio"><input  type="radio" data-index="' + j + '_' + x + '_' + y + '_' + z + '"  name="' + j +'_'+ x  +'_'+ y + '" id="' + j +'_' + x +'_' + y  +'_'+ z + '"'
           if (scaleList.group[j].rows[x].row[y].details[z].select == 1) {
             str2 += ' checked="checked"'
           }
-          str2 += '" />' + '<label for="' + j + x + y + z + '">' + scaleList.group[j].rows[x].row[y].details[z].detailName + '(' + scaleList.group[j].rows[x].row[y].details[z].score + ')' + '</label>'
+          str2 += ' />' + '<label for="' + j  +'_'+ x +'_' + y +'_' + z + '">' + scaleList.group[j].rows[x].row[y].details[z].detailName + '(' + scaleList.group[j].rows[x].row[y].details[z].score + ')' + '</label>'
           if (scaleList.group[j].rows[x].row[y].details[z].state == 1) {
             str2 += '<span class="groupRowRecommend">智能推荐</span>'
           }
           str2 += '</span>'
         }
-        str += '<div class="groupRowWrapper ' + j + x + '" data-group="' + j + x + '" id="' + j + x + y + '"><div class="groupRowName"> <span class="groupRowIndex">'
+        str += '<div class="groupRowWrapper ' + j  +'_'+ x + '" data-group="' + j +'_' + x + '" id="' + j  +'_'+ x  +'_'+ y + '">'
+        
+        if(scaleList.group[j].rows[x].row.length > 1 && y=== 0) {
+          str += '<div class="tips">本组选项可多选或根据需要选择某一条选项</div>'
+        }
+        
+        str +='<div class="groupRowName"> <span class="groupRowIndex">'
         if (y == 0) {
           str += (x + 1) + '.'
         }
@@ -127,10 +99,133 @@ function renderCalcu(scaleList) {
     calcuStr += scaleList.calculate.result.value + ' ' + scaleList.calculate.result.text
   }
   calcuStr += '</span><span class="calcuBtn">得分</span></div>'
+  if($(".calcuWrapper")[0]) {
+    $(".calcuWrapper").html(str + calcuStr)
+  } else {
+    var allStr = '<div class="calcuWrapper">' + str + calcuStr + '</div>'
+    $('.content').append(allStr)
+  }
+  
+}
+
+function bindScaleType1(scaleList) {
+  $('input').on('change', function (e) {
+    var indexList = $(this).attr('data-index').split('_')
+    for (let i = 0; i < scaleList.group[indexList[0]].rows[indexList[1]].row[indexList[2]].details.length; i++) {
+      scaleList.group[indexList[0]].rows[indexList[1]].row[indexList[2]].details[i].select = 0
+    }
+    if(+scaleList.group[indexList[0]].rows[indexList[1]].metux === 1) {
+      for(let i = 0; i < scaleList.group[indexList[0]].rows[indexList[1]].row.length; i++) {
+        for(let j = 0; j < scaleList.group[indexList[0]].rows[indexList[1]].row[i].details.length; j++) {
+          scaleList.group[indexList[0]].rows[indexList[1]].row[i].details[j].select = 0
+        }
+      }
+    }
+
+    scaleList.group[indexList[0]].rows[indexList[1]].row[indexList[2]].details[indexList[3]].select = 1
+    const className = $(this).parent().parent().parent().attr('data-group')
+    for (let i = 0; i < $('.' + className).length; i++) {
+      $('.' + className).eq(i).removeClass('noSelect')
+    }
+    renderScaleType1(scaleList)
+    bindScaleType1(scaleList)
+  })
+
+  $('.calcuBtn').on('click', function () {
+    let allSelect = true
+    for (let i = 0; i < scaleList.group.length; i++) {
+      for (let j = 0; j < scaleList.group[i].rows.length; j++) {
+        if (scaleList.group[i].rows[j].required == 1) {
+          let itemSelect = false
+          for (let x = 0; x < scaleList.group[i].rows[j].row.length; x++) {
+            for (let y = 0; y < scaleList.group[i].rows[j].row[x].details.length; y++) {
+              if (scaleList.group[i].rows[j].row[x].details[y].select == 1) {
+                itemSelect = true
+              }
+            }
+          }
+          if (!itemSelect) {
+            allSelect = false
+          }
+        }
+
+      }
+    }
+    if (!allSelect) {
+      $('.modal').css('display', 'block');
+      let timer = setTimeout(() => {
+        $('.modal').css('display', 'none');
+      }, 2000);
+      $('.closeModal').click(function () {
+        $('.modal').css('display', 'none');
+        if (timer) {
+          clearTimeout(timer)
+        }
+      })
+    }
+    if (allSelect) {
+      getCalcuResult(scaleList)
+    }
+  })
+}
+
+function renderScaleType2(scaleList) {
+  let str = ''
+  for( let i = 0; i < scaleList.rows.length; i++){
+    let str2 = ''
+    for(let j = 0; j < scaleList.rows[i].details.length; j++) {
+      str2 += '<div class="groupRowRadio groupRowRadioType2"><input  type="radio" data-index="' + i + '_' + j +'"  name="' + i + '_' + '" id="' + i+'_' + j
+          if (scaleList.rows[i].details[j].select == 1) {
+            str2 += ' checked="checked"'
+          }
+          str2 += '" />' + '<label for="' + i +'_'+ j  + '">' + scaleList.rows[i].details[j].detailName + '(' + scaleList.rows[i].details[j].result + ')' + '</label>'
+          if (scaleList.rows[i].details[j].state == 1) {
+            str2 += '<span class="groupRowRecommend">智能推荐</span>'
+          }
+          str2 += '</div>'
+    }
+    str += `
+      <div class="groupBox groupBoxType2">
+        <div class="groupName">
+          ${scaleList.rows[i].name}
+        </div>
+        ${str2}
+
+      </div>
+    `
+    
+  }
+  var calcuStr =`<div class="calcResult"></div>`
   var allStr = '<div class="calcuWrapper">' + str + calcuStr + '</div>'
   $('.content').append(allStr)
 }
 
+function bindScaleType2(scaleList) {
+  $('input').on('change', function (e) {
+    var indexList = $(this).attr('data-index').split('_')
+    for (var i = 0; i < scaleList.rows[indexList[0]].details.length; i++) {
+      scaleList.rows[indexList[0]].details[i].select = 0
+    }
+    scaleList.rows[indexList[0]].details[indexList[1]].select = 1
+
+    let resultStr ="结果:"
+    let resultStrNum = 0
+    for(let i = 0; i < scaleList.rows.length; i++) {
+        for(let j = 0; j < scaleList.rows[i].details.length; j++) {
+          if(scaleList.rows[i].details[j].select === 1) {
+            resultStrNum++
+            resultStr += `${scaleList.rows[i].details[j].detailName}(${scaleList.rows[i].details[j].result}),`
+          }
+        }
+    }
+    if(resultStrNum > 0) {
+      resultStr = resultStr.slice(0,-1)
+    }
+    $('.calcResult').html(resultStr)
+  })
+}
+
+
 function getCalcuResult(data) {
   const param = {
     type: 1,