UiSwitch.vue 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <template>
  2. <el-row class="form-group">
  3. <el-col style="min-height:50px;padding-top:5px;" :span="3">
  4. <label style="margin-left:10px;vertical-align: middle;" class="control-label">{{label ? label : 'Switch'}}</label>
  5. </el-col>
  6. <el-col :span="14" style="min-height:50px;padding-top:5px;">
  7. <div id='ui-switch-input'>
  8. <el-switch v-model="value1"
  9. :disabled="disabled ? true : false"
  10. :width="width == 40 ? 40 : 58"
  11. :active-text="activeText ? activeText : 'ON'"
  12. :inactive-text="inactiveText ? inactiveText : 'OFF'"
  13. :active-value="1"
  14. :inactive-value="0"
  15. :active-color="activeColor ? activeColor : '#20A0FF'"
  16. :inactive-color="inactiveColor ? inactiveColor : '#C0CCDA'"
  17. :name="name ? name : 'switch'"
  18. :id="name"
  19. @change="change">
  20. </el-switch>
  21. </div>
  22. </el-col>
  23. <el-col :span="7">
  24. <div class="classJs">
  25. <div v-if="tips">
  26. <span class="red">*</span>&ensp;{{tips}}
  27. </div>
  28. </div>
  29. </el-col>
  30. </el-row>
  31. </template>
  32. <script>
  33. export default {
  34. props: ['label','disabled','width','activeText','inactiveText','activeColor','inactiveColor','name','checked','tips'],
  35. data() {
  36. return {
  37. value1: this.checked ? 1 : 0
  38. }
  39. },
  40. mounted() {
  41. var checked = this._props.checked ? true : false;
  42. var th = $(document).find('#ui-switch-input').find('#'+this._props.name);
  43. if ( checked ) {
  44. // 开启
  45. var val = th.attr('true-value');
  46. th.val(val)
  47. }else{
  48. // 关闭
  49. var val = th.attr('false-value');
  50. th.val(val);
  51. }
  52. },
  53. methods: {
  54. change(value) {
  55. var th = $(document).find('#ui-switch-input').find('#'+this._props.name);
  56. if( value ){
  57. var val = th.attr('true-value');
  58. th.val(val)
  59. }else{
  60. var val = th.attr('false-value');
  61. th.val(val);
  62. }
  63. }
  64. }
  65. }
  66. </script>
  67. <style scoped>
  68. .el-switch__input {
  69. display: none;
  70. }
  71. .classJs {
  72. margin-left: 10px;
  73. vertical-align: middle;
  74. height:42px;
  75. line-height:42px;
  76. }
  77. .red{
  78. color: red;
  79. }
  80. </style>