Vue 利用文本域做一个批量添加选项功能

需要用到的 “知识点” :

1、Element UI 的 Dialog 对话框

<el-button type="text" @click="dialogVisible = true">点击打开 Dialog</el-button>

<el-dialog
  title="提示"
  :visible.sync="dialogVisible"
  width="30%"
  :before-close="handleClose">
  <span>这是一段信息</span>
  <span slot="footer" class="dialog-footer">
    <el-button @click="dialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="dialogVisible = false">确 定</el-button>
  </span>
</el-dialog>

<script>
  export default {
    data() {
      return {
        dialogVisible: false
      };
    },
    methods: {
      handleClose(done) {
        this.$confirm('确认关闭?')
          .then(_ => {
            done();
          })
          .catch(_ => {});
      }
    }
  };
</script>

2、Element UI 的 Input 输入框

文本域

用于输入多行文本信息,通过将 type 属性的值指定为 textarea。

<el-input
  type="textarea"
  :rows="2"
  placeholder="请输入内容"
  v-model="textarea">
</el-input>

<script>
export default {
  data() {
    return {
      textarea: ''
    }
  }
}
</script>

3、Vue 文本域 多行输入时,逐行获取,渲染内容

我们在进行文本域输入时,很多情况会涉及到输入操作步骤,我们希望可以逐行获取,以数组形式提交到后端。当然从后端获取到数组形式的操作步骤时,可以按照逐行显示的方法显示在文本域中,那么具体怎么进行呢?

Step1: 搞清楚我们在文本域中敲击回车时,发生了什么
回车 => "\n"

以图片为例,文本域中的内容就可以表示为:“1、aaa\n2、bbb\n3、ccc” 这样一来,我们只需替换 \n 为一个常见的分隔符就好,这里用 “,” 举例,替换会用到下列自定义函数

    // 多行文本域内容逐行获取
    preText(pretext) {
      return pretext.replace(/\r\n/g, ",").replace(/\n/g, ",");
    },

Step2:  \n 替换完毕,接下来转换为数组

这时,我们的 “1、aaa\n2、bbb\n3、ccc” 就会转变为 “1、aaa,2、bbb,3、ccc” 到这里,JavaScript 学得不错的同学就应该都知道该怎么变为数组了。
没错,通过 JavaScript   split()  方法

("1、aaa,2、bbb,3、ccc").split(",");

Step3: 字符串转数组成功,然后该逆过程
我们从后端获取到数组形式的操作步骤时,可以按照逐行显示的方法显示在文本域中,其实也很简单,既然转数组的时候是字符串分割,那么数组转字符串,数组拼接不就完了嘛!我想这时候 JavaScript 学得不错的同学又应该知道该怎么办了。
JavaScript   join()  方法

“怎么样才能做到换行呢?”
“\n”
“所以,用 \n 拼接就完了嘛!”

that.TaskInfo.taskcontent = res.data.content.join("\n");

真实项目使用 :

src  /  views  /  edit  /  components  /  parameter.vue 

<template>
  <!-- 批量添加对话框开关 -->
  <div>
    <el-col>
      <el-form-item>
        <el-tooltip content="添加选项">
          <img
            src=""
            alt="添加选项"
            @click="addElement()"
            style="margin: 0 10px 0 30px"
          />
        </el-tooltip>

        <el-tooltip content="批量添加">
          <img src="" alt="批量添加" @click="batchAddVisible = true" />
        </el-tooltip>
      </el-form-item>
    </el-col>
    
    <!-- Dialog 对话框 -->
    <el-dialog
      class="batchAdd"
      title="标题文本"
      :visible.sync="batchAddVisible"
      width="30%"
    >
      <span>每行一个选项</span>
      <!-- Input 输入框 文本域 -->
      <el-input
        type="textarea"
        placeholder="请输入选项内容"
        v-model="textarea"
        :autosize="{ minRows: 5, maxRows: 20 }"
        @change="preText"
      >
      </el-input>
      <span slot="footer" class="dialog-footer">
        <el-button size="mini" @click="batchAddVisible = false"
          >取 消</el-button
        >
        <el-button
          size="mini"
          type="primary"
          @click="addElements(), (batchAddVisible = false)"
          >保 存</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      batchAddVisible: false, // 控制批量添加对话框的显示
      textarea: "", // 批量添加文本域内容的绑定值
      preTextArray: [], // 批量添加文本域内容存储的数据
    };
  },

  methods: {
    addElement() {
      // 添加选项功能
      this.data.options.push({
        name: `选项${this.data.options.length + 1}`,
        value: this.data.options.length + 1,
        addInput: false,
      });
    },

    deleteElement() {
      // 删除元素
      this.data.options.splice(index, 1);
    },

    // 批量添加文本域 change 发生改变时触发的事件
    preText(pretext) {
      // 此方式会保留所有空格 / 换行
      this.preTextArray = pretext
        .replace(/\r\n/g, ",")
        .replace(/\n/g, ",")
        .split(",");

      // 此方法仅会保留每一行文本内的空格(除首尾空格)
      this.preTextArray = pretext
        .trim()
        .replace(/\r\n/g, ",")
        .replace(/\n/g, ",")
        .split(",");

      for (let i = 0; i < this.preTextArray.length; i++) {
        // 去除每一项内的首尾空格
        this.preTextArray[i].trim();
      }
      for (let i = 0; i < this.preTextArray.length; i++) {
        if (this.preTextArray[i] == "") {
          // 去除多余的换行项
          this.preTextArray.splice(i, 1);
          // 这里会产生索引塌陷问题 , 所以一定要记得 i--
          i--;
        }
      }

      // 此方法会去除所有的空格及换行
      this.preTextArray = pretext
        .trim()
        .replace(/\r\n/g, ",")
        .replace(/\n/g, ",")
        .replace(/\s/g, "")
        .replace(/\t/g, "")
        .split(",");

      for (let i = 0; i < this.preTextArray.length; i++) {
        if (this.preTextArray[i] == "") {
          // 去除多余的换行项
          this.preTextArray.splice(i, 1);
          // 这里会产生索引塌陷问题 , 所以一定要记得 i--
          i--;
        }
      }
    },

    addElements() {
      // 批量添加选项功能
      for (let i = 0; i < this.preTextArray.length; i++) {
        this.data.options.push({
          name: `${this.preTextArray[i]}`,
          value: this.data.options.length + 1,
          addInput: false,
        });
      }
      // 点击完保存按钮后需要清空文本域里的内容
      this.textarea = "";
    },
  },
};
</script>

<style lang="scss" scoped>
</style>

代码优化 : 

大佬说你咋还用 for 循环来写呢 ?不怕内存泄漏啊 ?

改成 ES6 的写法 , emmm , So =>

<script>
export default {
  data() {
    return {
      batchAddVisible: false, // 控制批量添加对话框的显示
      textarea: "", // 批量添加文本域内容的绑定值
      preTextArray: [], // 批量添加文本域内容存储的数据
    };
  },

  methods: {
    addElement() {
      // 添加选项功能
      this.data.options.push({
        name: `选项${this.data.options.length + 1}`,
        value: this.data.options.length + 1,
        addInput: false,
      });
    },

    deleteElement() {
      // 删除元素
      this.data.options.splice(index, 1);
    },

    // 批量添加文本域 change 发生改变时触发的事件
    preText(pretext) {
      // 此方式会保留所有空格 / 换行
      this.preTextArray = pretext
        .replace(/\r\n/g, ",")
        .replace(/\n/g, ",")
        .split(",");

      // 此方法仅会保留每一行文本内的一个空格(除首尾空格)
      this.preTextArray = pretext
        .trim()
        .replace(/\r\n/g, ",")
        .replace(/\n/g, ",")
        .split(",");
        .filter((item) => item !== '')
        .map((item) => item.trim())

      console.log(this.preTextArray)
    },

    addElements() {
      // 批量添加选项功能
      this.preTextArray.forEach((item) => {
        this.data.options.push({
          name: `${item}`,
          value: this.data.options.length + 1,
          addInput: false
        })
      })
      // 点击完保存按钮后需要清空文本域里的内容
      this.textarea = "";
    },
  },
};
</script>
  • 2
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值