GLForm.wxs 1.82 KB
var isShowFn = function (formData, targetItem) {
  var flag = false;
  if (targetItem.isShowObj && formData) {
    for (var i = 0; i <= targetItem.isShowObj.list.length; i++) {
      var item = targetItem.isShowObj.list[i];
      if (item == formData[targetItem.isShowObj.attr]) {
        flag = true;
        break;
      }
    }
  } else {
    flag = true;
  }
  return flag;
};

// 根据 formData 的值和 optionList 计算 picker 的选中索引
var getPickerIndex = function (formData, item) {
  if (!formData || !item) {
    return 0;
  }
  var optionList = item.optionList;
  var optionId = item.optionId;
  var fieldName = item.name;

  if (!optionList || optionList.length === 0 || !optionId || !fieldName) {
    return 0;
  }

  var currentValue = formData[fieldName];
  if (
    currentValue === null ||
    currentValue === undefined ||
    currentValue === ""
  ) {
    return 0;
  }

  // WXS 不支持 String(),使用 "" + value 的方式转换为字符串
  var currentValueStr = "" + currentValue;

  for (var i = 0; i < optionList.length; i++) {
    var option = optionList[i];
    if (!option) continue;

    // 在 WXS 中,需要使用字符串键名访问对象属性
    var optionValue = null;
    if (optionId === "value") {
      optionValue = option.value;
    } else if (optionId === "name") {
      optionValue = option.name;
    } else {
      optionValue = option[optionId];
    }

    if (optionValue === null || optionValue === undefined) continue;

    // WXS 不支持 String(),使用 "" + value 的方式转换为字符串
    var optionValueStr = "" + optionValue;

    // 比较字符串值(已确保数据类型一致,直接字符串比较即可)
    if (optionValueStr === currentValueStr) {
      return i;
    }
  }
  return 0;
};

module.exports = {
  isShowFn: isShowFn,
  getPickerIndex: getPickerIndex,
};