CheckBox componentization


//allFlag,是否有全选
//elementId,
//dataList,[{id:3,showValue:'test3'},{id:4,showValue:'test4'}];

function makeCheckBox(allFlag, elementId, dataList) {
  var html = "";
  if (allFlag) {
    html = html + "<input  id='checkBox_all_" + elementId + "' type='checkbox' class='group_" + elementId + "' value='0' checkType='0'>";
    html = html + "<label for='checkBox_all_" + elementId + "' >全部</label>";
  }
  $.each(dataList, function (i) {
    html = html + "<input id='" + elementId + '_' + dataList[i].id + "' type='checkbox' class='group_"+ elementId + "' value='" + dataList[i].id + "' checkType='1'>";
    html = html + "<label for='" + elementId + '_' + dataList[i].id + "'>" + dataList[i].showValue + "</label>";
  });
  $('#' + elementId).html(html);

  //全选处理
  $('#' + elementId + ' input[checkType=0]').on("click", function () {
    var id = $(this).attr('id');
    var className = $(this).attr('class');
    if ($('#' + id).is(':checked')) {
      $('input[class=' + className + ']').prop("checked", true);
    } else {
      $('input[class=' + className + ']').prop("checked", false);
    }
  });     var className = $(this).attr('class');   $('#' + elementId + ' input[checkType=1]').on('click', function () {

  //Processing each option


    var len1 = $('input[class=' + className + ']' + '[checkType=1]').length;
    var len2 = $('input[class=' + className + ']' + '[checkType=1]:checked').length;

    if (len1 == len2) {
      $('input[class=' + className + ']' + '[checkType=0]').prop("checked", true);
    } else {
      $('input[class=' + className + ']' + '[checkType=0]').prop("checked", false);
    }
  });
}

function setCheckBoxByIdList(elementId, idList) {
  var len = idList.length;

  $('#' + elementId + ' input[class=group_' + elementId + ']').prop("checked", false);
  if (len == 0) {
    return;
  }

  for (var i = 0; i < len; i++) {
    $('#' + elementId + ' input[class=group_' + elementId + ']' + '[value=' + idList[i] + ']').prop("checked", true);
  }

  var len1 = $('#' + elementId + ' input[class=group_' + elementId + ']' + '[checkType=1]').length;
  var len2 = $('#' + elementId + ' input[class=group_' + elementId + ']' + '[checkType=1]:checked').length;

  if (len1 == len2) {
    $('#' + elementId + ' input[class=group_' + elementId + ']' + '[checkType=0]').prop("checked", true);
  } else {
    $('#' + elementId + ' input[class=group_' + elementId + ']' + '[checkType=0]').prop("checked",false);
  }
}

function setCheckBoxByIdsString(elementId, idsString) {
  if (!idsString) {
    console.error("idsString = " + idsString + ",不符合要求(idsString='1,2,3,4...)'");
    return;
  }

  if (idsString == '') {
    $('#' + elementId + ' input[class=group_' + elementId + ']').prop("checked", false);
    return;
  }

  var idList = idsString.split(',');

  setCheckBoxByIdList(elementId, idList);
}

function getCheckBoxDataList(allFlag, elementId) {
  var dataList = [];

  if (allFlag) {
    $('#' + elementId + ' input[class=group_' + elementId + ']' + ':checked').each(function () {
      var data = {};
      var showValue = $(this).next("label").text();
      var id = $(this).val();
      data.id = id;
      data.showValue = showValue;
      dataList.push(data);
    });
  } else {
    $('#' + elementId + ' input[class=group_' + elementId + ']' + '[checkType=1]:checked').each(function () {
      var data = {};
      var showValue = $(this).next("label").text();
      var id = $(this).val();
      data.id = id;
      data.showValue = showValue;
      dataList.push(data);
    });
  }

  return dataList;
}

function getCheckBoxShowValueList(allFlag, elementId) {
  var dataList = [];

  if (allFlag) {
    $('#' + elementId + ' input[class=group_' + elementId + ']' + ':checked').each(function () {
      var showValue = $(this).next("label").text();
      dataList.push(showValue);
    });
  } else {
    $('#' + elementId + ' input[class=group_' + elementId + ']' + '[checkType=1]:checked').each(function () {
      var showValue = $(this).next("label").text();
      dataList.push(showValue);
    });
  }

  return dataList;
}

function getCheckBoxIdList(allFlag, elementId) {
  var dataList = [];

  if (allFlag) {
    $('#' + elementId + ' input[class=group_'+ elementId + ']' + ':checked').each(function () {
      var id = $(this).val();
      dataList.push(id);
    });
  } else {
    $('#' + elementId + ' input[class=group_' + elementId + ']' + '[checkType=1]:checked').each(function () {
      var id = $(this).val();
      dataList.push(id);
    });
  }

  return dataList;
}

function getCheckBoxShowValueString(allFlag, elementId) {
  var dataList = getCheckBoxShowValueList(allFlag, elementId);
  var dataString = dataList.join(',');
  return dataString;
}

function getCheckBoxIdString(allFlag, elementId) {
  var dataList = getCheckBoxIdList(allFlag, elementId);
  var dataString = dataList.join(',');
  return dataString;
}




///*
// * Modify the page to get the ID of the div where the checkBox list @param elementId is located @param basicCode basic data code @param
// * name checkBox name @param isall ture loads the "all" option @param checkedId selected checkbox id
// * used to determine whether to be selected when modifying the page initialization
// *
// * input custom attribute: checkType 0 (all) 1 (subclass checkbox) @returns
// */
//function getEditCheckBox(isall,elementId,basicCode,name,checkedId){
// getCheckBox(isall,elementId,basicCode,name);
// var data = checkedId. split(',');
// setTimeout(function(){
// for(var i = 0;i<data.length;i++){
// $('#'+elementId+' input[value='+data [i]+']').prop("checked",true);
// };
// },1000)
//
//}
//
///*
// * Get the selected id of the radio checkbox @param elementClass input class return idStr "XX,XX,XX"
// * id spelling up string
// *
// * @returns
// */
//function getCheckedId(elementClass){
// var elementCls=$("input[class='"+elementClass+"']:checked");
// var elementLen = elementCls.length;
// var idStr = "";
// elementCls.each(function(i){
// idStr = idStr+$(this).val();
// if(i!=elementLen-1 ){
// idStr = idStr+",";
// }
// });
// return idStr;
//}
//
///*
// * 获取单选复选框被选择的id @param elementClass input的class return idStr "XX,XX,XX"
// * id拼起来的字符串
// *
// * @returns
// */
//function getCheckedName(elementClass){
// var elementCls=$("input[class='"+elementClass+"']:checked");
// var elementLen = elementCls.length;
// var idStr = "";
// elementCls.each(function(i){
// idStr = idStr+$(this).next("label").text();
// if(i!=elementLen-1){
// idStr = idStr+",";
// }
// });
// return idStr;
//}
//
//
//function setCheckedByIds(elementId,checkedId){
// var data = checkedId.split(',');
//
// $('#'+elementId+' input').prop("checked",false);
//
// for(var i = 0;i<data.length;i++){
// $('#'+elementId+' input[value='+data[i]+']').prop("checked",true);
// }
//
// var len = $('#'+elementId).closest('li').find('input[checktype=1]').length;
// var checkedLen = $('#'+elementId).closest('li').find('input[checktype=1]:checked').length;
// if(len == checkedLen ){
// $('#'+elementId).closest('div').find('input[checktype=0]').prop("checked",true);
// }else{
// $('#'+elementId).closest('div').find('input[checktype=0]').prop("checked",false);
// }
//
//}
//
//
///*
// * Get the class of the Data @param elementClass input where the radio checkbox is selected return idStr "XX,XX,XX"
// * String made up of id
// *
// * @returns
// */
/ /function getCheckedData(elementClass){
// var dataTemp = [];
// var elementCls=$("input[class='"+elementClass+"']");
// // var elementLen = elementCls.length;
// // var idStr = "";
// elementCls.each(function(i){
// dataTemp.push({value:$(this).val(),
// labelText:$(this).next("label ").text()
// })
// });
// return dataTemp;
//}
//
///*
// * Add page: initialize circular radio box
// *
// * param: b 0: Without <b> 1: With <b> b is used to put color blocks
// *
// */
//function getRadio(divId,basicCode,name,b){
// $.ajax({
// type : 'post',
// url : '/basicData/detailListByBasicCode',
// dataType : "json",
// data : {basicCode:basicCode},
//     success: function(result){
//     var html="";
//     if(b){
//     $.each(result,function(i){
//        html=html+"<input  id='"+result[i].detailCode+"' type='radio' name='"+name+"' value='"+result[i].id+"' >";
//                 html=html+"<label for='"+result[i].detailCode+"' ><b></b>"+result[i].detailName+"</label>";
//     });
//     }else{
//     $.each(result,function(i){
//        html=html+"<input  id='"+result[i].detailCode+"' type='radio' name='"+name+"' value='"+result[i].id+"' >";
//                 html=html+"<label for='"+result[i].detailCode+"' >"+result[i].detailName+"</label>";
//     });
//     }
//    
//     $('#'+divId).html(html);
//   }
// });
//};
//
//
///*
// * 修改页面:圆形的单选框初始化
// *
// * param:b 0:不带<b> 1:带<b>b is the param used to put the color block: div id where elementId is located @param checkedId // getRadio(elementId,basicCode,name,b); //function getEditRadio (elementId,basicCode,name,b,checkedId){ // */ // *
// * The id of the selected checkbox is used to determine whether it should be selected or not when modifying the page initialization




// setTimeout(function(){
// $('#'+elementId+' input[value='+checkedId+']').prop("checked",true);
// },1000);
//}
/ //function setRadioByIds
(elementId,checkedId){
// $('#'+elementId+' input[value='+checkedId+']').prop("checked",true);
//}
//
/// *
// * "All" state 1. Select all others, all are selected 2. Cancel all other inputs are canceled 3. All others are selected The state of "all" is selected 4. One of the others is not selected
// * " All" status is unchecked @param id @param name @returns
// */
//function checkstatus(id,name){
// console.log(name)
// if($('#'+id) .is(':checked')) {
// $("input[class='"+name+"']").prop(" checked",true);
// }else{
// console.log("false");
// $("input[class='"+name+"']").prop("checked",false);
// }
//}
//
//
//// $('.uploadImged').on('hover')
//
//// window.onload=function(){
//// $('.initLoading').remove();
//// }
//
//
///**
// * 表格编辑拓张
// */
// $.extend($.fn.datagrid.methods, {
//    editCell: function(jq,param){
//       return jq.each(function(){
//            var opts = $(this).datagrid('options');
//            var fields = $(this).datagrid('getColumnFields',true).concat($(this).datagrid('getColumnFields'));
//           for(var i=0; i<fields.length; i++){
//                var col = $(this).datagrid('getColumnOption', fields[i]);
//                col.editor1 = col.editor;
//                if (fields[i] != param.field){
//                    col.editor = null;
//                }
//            }
//            $(this).datagrid('beginEdit', param.index);
//            for(var i=0; i<fields.length; i++){
//                var col = $(this).datagrid('getColumnOption', fields[i]);
//                col.editor = col.editor1;
//            }
//        });
//    }
//});
//
//
//
//function endEditing(gridid){
//     if (editIndex == undefined){return true}
//     if ($(gridid).datagrid('validateRow', editIndex)){
//         $(gridid).datagrid('endEdit', editIndex);
//         editIndex = undefined;
//         return true;
//     } else {
//         return false;
//     }
//}
//
//
//// 删除上传的图片
//
//$('.imgListUl').on('mouseover','.uploadImged',function(){
// $(this).find('.deleteImgBanner').show();
//}).on('mouseout','.uploadImged',function(){
// $(this).find('.deleteImgBanner').hide();
//}).on('click','.uploadImged .deleteImgBanner',function(){
// $(this).closest('li.uploadImged').remove();
//})

// # sourceURL=common.js

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326208250&siteId=291194637