效果如下:
當type值選擇完后,amount值會自動相應填入。
1. 從后台獲取數據,為一個數組,里面包含多個對象。
<select id="scholarshipTypeSelect" resultType="com.entity.scholarshipTypeUser"> select first.id, first.type, second.amount from scholarshipType first,scholarshipType second where first.type = second.type </select>
采用了自身連接,將type與amount對應起來,形成對象,一個數組,發送到前端。
返回值:[{"id":1,"type":"學院級","amount":2000},{"id":2,"type":"學校級","amount":3000},{"id":3,"type":"國家級","amount":5000},{"id":4,"type":"國家勵志級","amount":8000}]
2.在獲取到數據之后,先給type賦值
在type中,item in types,由於獲取到了types數據,所以在type的select選擇框中是有數據的。
3.此時實現在點擊select選擇框中的選項時,amount中的數據會相應變化。
由於返回的是types數據,所以type中可以獲取到值,但是amount中獲取不到,沒有明確賦值。
循環types數組,當select中選中時,判斷每一項中是否有相等的,若是相等,將該項的amount值賦給amount的input輸入框中。
此時,即實現了后台獲取數據,select聯動效果。
注意:
1.在select的循環選擇框中,@on-change返回的是value,是選中項的值(:value="item.type")。與this.formValidate.type值一致。
若是使用elelment-ui框架的話,則為@change="selectType",使用方法與iview的一致。
2.不能在this中使用this,不然會獲取不到,可以先定義一下:const _this = this;
3.v-model表單綁定:綁定的是輸入框中的值。聯動效果,也可以是兩個select選擇框。