vue+element的一些心得

994 阅读1分钟

一、实现功能:点击el-table里的操作按钮才时点击行高亮,点击其他地方不显示

一开始的做法是设置给table加上 highlight-current-row css

// el-table当前行的背景色
.el-table__body tr.current-row>td{
  background: rgba(39, 120, 241, 0.1) !important;
  border-top: 1px solid #2778F1 !important;
  border-bottom: 1px solid #2778F1 !important;
}
.el-table__body tr.current-row>td:first-child{
  border-left: 1px solid #2778F1 !important;
}
.el-table__body tr.current-row>td:last-child{
  border-right: 1px solid #2778F1 !important;
}
 	<el-table
          ref="table"
          :data="historyConversations"
          v-loading="tableLoading"
          tooltip-effect="dark"
          height="calc(100vh - 270px)"
          stripe
          class="tableBox"
          style="width: 100%;"
          @sort-change="changeSort"
          highlight-current-row
        >
          <el-table-column
            label="访客"
            prop="customer.nickname"
          />
          <el-table-column
            width="100"
            label="操作"
          >
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                @click="showMessages(scope.row)"
              >
                查看详情
              </el-button>
            </template>
          </el-table-column>
        </el-table>

这样操作可以实现点击某行高亮,但是点击改行的其他列也会高亮,与需求不太吻合,于是想到点击某行的按钮位某行加上class[current-row]名,但是,这样有的列的border不显示,只有按钮所在列显示border,(不知道什么原因,如有知道可以留言 不胜感激) 最后发现了 可以为行添加class,于是就修改代码

	<el-table
          ref="table"
          :data="historyConversations"
          v-loading="tableLoading"
          tooltip-effect="dark"
          height="calc(100vh - 270px)"
          stripe
          class="tableBox"
          style="width: 100%;"
          @sort-change="changeSort"
          :row-class-name="tableRowClassName"
        >
          <el-table-column
            label="访客"
            prop="customer.nickname"
          />
          <el-table-column
            width="100"
            label="操作"
          >
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                @click="showMessages(scope.row)"
              >
                查看详情
              </el-button>
            </template>
          </el-table-column>
        </el-table>
methods:{
    tableRowClassName(row) {
    if (row.row.id === this.id) {
      return 'current-row';
    }
    return '';
  },
  }
   showMessages(row) {
    this.id = row.id;
    this.$refs.HistoryConversationDetail.visible = true;
  },

这样就实现了想要的功能

二、实现功能:el-form的el-form-item是数组的验证 当有数组循环使用同一个验证时,可以在el-form-item里添加rules,

 <el-form
        ref="form"
        :model="form"
        label-width="80px"
        v-loading="loading"
        :rules="rules"
      >
        <el-form-item label="标准问" prop="standard_question">
          <el-input v-model="form.standard_question" placeholder="请输入问题"
                    style="width: calc(100% - 120px);"
          />
          <el-button type="primary" style="width: 110px; margin-left: 10px;" @click="onRecognize2">
            拓展问推荐
          </el-button>
        </el-form-item>
        <div style="max-height: 350px;overflow-y: auto;">
          <el-form-item :label="i === 0? '拓展问':''" v-for="(t, i) in form.extended_questions"
                        :key="i" :prop="'extended_questions.'+i" :rules="rules.exo"
          >
            <div style="width: calc(100% - 120px);
                display: inline-block;position: relative;"
            >
              <el-input v-model="form.extended_questions[i]" placeholder="请输入问题" />
              <el-button type="text" @click="form.extended_questions.splice(i, 1)"
                         style="width: 40px; margin-left: 10px;
                          position: absolute;right: 10px;"
                         v-if="form.extended_questions.length > 1"
              >
                <i class="el-icon-error" style="width: 12px;height: 12px;" />
              </el-button>
            </div>
          </el-form-item>
        </div>
         </el-form>
data(){
 const validatePass = (rule, value, callback) => {
      if (value) {
        sameQuestion(value, this.faqId)
          .then((res) => {
            if (res.data.tip) {
              callback(new Error(res.data.tip));
            }
          });
      }
    };
    return{
    	 rules: {
        standard_question: [
          { required: true, message: '请输入标准问题', trigger: 'blur' },
        ],
        exo: [
          { validator: validatePass, trigger: 'blur' },
        ],
      },
    }
    }

这种情况当删除拓展问时,报错的信息不随拓展问的内容一起移动位置,做一下修改就可以。

 <el-form
        ref="form"
        :model="form"
        label-width="80px"
        v-loading="loading"
        :rules="rules"
      >
        <el-form-item label="标准问" prop="standard_question">
          <el-input v-model="form.standard_question" placeholder="请输入问题"
                    style="width: calc(100% - 120px);"
          />
          <el-button type="primary" style="width: 110px; margin-left: 10px;" @click="onRecognize2">
            拓展问推荐
          </el-button>
        </el-form-item>
       <div style="max-height: 300px;overflow-y: auto;">
          <el-form-item :label="i === 0? '拓展问':''" v-for="(t, i) in form.extended_questions"
                        :key="t.id" :prop="'extended_questions.'+i+'.text'" :rules="rules.exo"
          >
            <div style="width: calc(100% - 120px);
                display: inline-block;position: relative;"
            >
              <el-input v-model="form.extended_questions[i].text" placeholder="请输入问题" />
              <el-button type="text" @click="form.extended_questions.splice(i, 1)"
                         style="width: 40px; margin-left: 10px;
                          position: absolute;right: 10px;"
                         v-if="form.extended_questions.length > 1"
              >
                <i class="el-icon-error" style="width: 12px;height: 12px;" />
              </el-button>
            </div>
          </el-form-item>
        </div>
         </el-form>