vue实现自定义checkbox组件

效果展示

在这里插入图片描述

父组件中使用

<checkbox-group v-model="checkList">
        <checkbox
          v-for="(item,index) in data"
          v-bind:key="index"
          :label="item.name"
          :value="item.id"
        ></checkbox>
</checkbox-group>

Checkbox.vue

<template>
  <div class="checkbox-wrap" @click="handleClick">
    <div class="left" :class="isChecked?'box-click':''" ref="box">
      <transition name="fade">
        <div class="hook" v-show="isChecked"></div>
      </transition>
    </div>
    <div class="right">{{label}}</div>
  </div>
</template>
<script lang='ts'>
import { Component, Vue, Watch, Prop } from "vue-property-decorator";
@Component({
  components: {}
})
export default class extends Vue {
  private isChecked: boolean = false;
  @Prop()
  private label!: string;
  @Prop()
  private value!: string;
  private handleClick() {
  	this.isChecked = !this.isChecked;
    if (this.isChecked) {
      this.$parent.$emit("pushItem", this.value);
    } else {
      this.$parent.$emit("removeItem", this.value);
    }
    this.$emit("input", this.isChecked);
  }
}
</script>
<style scoped lang="scss">
.fade-enter-active,
.fade-leave-active {
  transition: opacity 0.3s;
}
.fade-enter, .fade-leave-to /* .fade-leave-active below version 2.1.8 */ {
  opacity: 0;
}
.checkbox-wrap {
  height: 24px;
  display: inline-block;
  vertical-align: center;
  margin-bottom: 5px;
  .left {
    height: 24px;
    width: 24px;
    border-radius: 7px;
    background-color: #ffffff;
    display: inline-block;
    border: 1.2px solid #cccccc;
    box-sizing: border-box;
  }
  .right {
    margin-left: 5px; 
    padding-right: 10px;
    display: inline;
    vertical-align: top; // 内联元素默认的vertical-align为baseline。对象的内容与基线对齐。如果我们在right插入文字。则为出现left right不水平对齐的情况
    line-height: 24px;
    color: #66757f;  // 在mounted
    font-size: 15px;
  }
  .box-click {
    background-color: #1da1f2;
    border: 0.5px solid #cccccc;
  }
  .hook {
    margin-left: 8px;
    margin-top: 2px;
    width: 5px;
    height: 12px;
    border-right: 1.3px solid #ffffff;
    border-bottom: 1.3px solid #ffffff;
    transform: rotate(40deg);
  }
}
.checkbox-wrap:hover {
  cursor: pointer;
}
</style>

CheckboxGroup.vue

<template>
  <div class="checkbox-group-wrap">
    <slot></slot>
  </div>
</template>
<script lang='ts'>
import { Component, Vue, Watch, Prop } from "vue-property-decorator";
@Component({
  components: {}
})
export default class extends Vue {
  private checkBoxGroup: string[] = [];
  private pushItem(value) {
    this.checkBoxGroup.push(value);
    this.$emit("input", this.checkBoxGroup);
  }
  private removeItem(value) {
    const index = this.checkBoxGroup.indexOf(value);
    this.checkBoxGroup.splice(index, 1);
    this.$emit("input", this.checkBoxGroup);
  }
  private mounted() {
    this.$on("pushItem", this.pushItem);
    this.$on("removeItem", this.removeItem);
  }
}
</script>
<style scoped lang="scss">
</style>

这里script部分是用ts写的,你可以改成js的语法。template和style部分不用改。注意这里子组件checkbox作为slot触发父组件checkboxGroup事件pushItem的用法。slot不能绑定事件

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要使用C#和Vue生成PDF,可以使用以下步骤: 1. 在Vue中使用多个自定义组件来构建表单,使用Vue的模板语法和指令来定义表单元素和验证规则。 2. 使用Vue的axios库将表单数据发送到后端C#应用程序。 3. 在C#中使用iTextSharp或其他PDF生成库将表单数据转换为PDF文件。 下面是一个简单的示例,演示了如何使用Vue和C#生成PDF: 在Vue中定义表单: ``` <template> <form> <custom-input v-model="name" label="Name"></custom-input> <custom-input v-model="email" label="Email" type="email"></custom-input> <custom-select v-model="gender" label="Gender" :options="['Male', 'Female']"></custom-select> <custom-checkbox v-model="terms" label="I agree to the terms and conditions"></custom-checkbox> <button @click.prevent="submitForm">Submit</button> </form> </template> <script> import axios from 'axios'; import CustomInput from './CustomInput.vue'; import CustomSelect from './CustomSelect.vue'; import CustomCheckbox from './CustomCheckbox.vue'; export default { data() { return { name: '', email: '', gender: '', terms: false } }, components: { CustomInput, CustomSelect, CustomCheckbox }, methods: { submitForm() { axios.post('/api/pdf', { name: this.name, email: this.email, gender: this.gender, terms: this.terms }) .then(response => { // handle PDF response }) .catch(error => { console.error(error); }); } } } </script> ``` 在C#中处理表单数据并生成PDF: ``` using iTextSharp.text; using iTextSharp.text.pdf; using System.IO; using System.Web.Http; public class PdfController : ApiController { [HttpPost] [Route("api/pdf")] public HttpResponseMessage GeneratePdf([FromBody] FormData formData) { var document = new Document(); var output = new MemoryStream(); var writer = PdfWriter.GetInstance(document, output); document.Open(); var font = FontFactory.GetFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED); document.Add(new Paragraph($"Name: {formData.Name}", font)); document.Add(new Paragraph($"Email: {formData.Email}", font)); document.Add(new Paragraph($"Gender: {formData.Gender}", font)); document.Add(new Paragraph($"Terms: {(formData.Terms ? "Yes" : "No")}", font)); document.Close(); var response = new HttpResponseMessage(); response.Content = new ByteArrayContent(output.ToArray()); response.Content.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment"); response.Content.Headers.ContentDisposition.FileName = "form.pdf"; response.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/pdf"); return response; } public class FormData { public string Name { get; set; } public string Email { get; set; } public string Gender { get; set; } public bool Terms { get; set; } } } ``` 在这个示例中,使用了iTextSharp库来生成PDF文件。在C#的控制器中,定义了一个名为`FormData`的类来表示表单数据。在`GeneratePdf`方法中,将表单数据添加到PDF文件中,并将PDF文件作为HTTP响应返回。在Vue中,使用axios库将表单数据发送到后端C#应用程序。当接收到PDF响应时,可以将其保存到本地或直接在浏览器中打开。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值