關於html裏th:with的介紹使用

thymeleaf th:with 的使用及傳參

估計有不少新手進公司後看大佬們的代碼,看不明白什麼意思,(說的是我自己)就包括了一個前端頁面通過th:with屬性來發送請求一段代碼
例:

/**父類風險分類*/
<select id="riskType" name="riskstyleId"   
         type="text"     	    onChange="changeQualifications()"
		th:with="type=${@type.getparentRiskType()}">
		<option value="">請選擇風險標準分類</option>
		<option th:each="EhsRiskDangerType : ${type}"
		th:text="${EhsRiskDangerType.riskDangerTypeName}"
		th:value="${EhsRiskDangerType.riskDangerTypeId}"></option>
</select>
/**子類風險分類*/
 <select class="layui-select riskSonTypeSelect"
			id="riskSonTypeSelect"  name="riskBranchFactoryId">
		<option value="">請先選擇風險子分類</option>
</select>

這段代碼可以清楚的看到下拉選中分別具有Id,name,type,th:with,onChange()幾個屬性,前面幾個不用多說,這個th:with屬性是用來幹嘛呢,它可以在頁面加載的時候發送請求,發送請求的路徑就是在你的後臺代碼裏找這樣一個類。例:

@Service("type")
public class TypeService {
	@Autowired
	private SysTypeService iSysTypeService;	
	/**
	 * 父類隱患類型
	 * @return
	 */
	public List<EhsRiskDangerType> getparentRiskType()
	{
		return iSysTypeService.selectparentByRiskType();
	}
}

從而給select下拉選循環賦值,而其中的onChange()事件,就是爲了關聯另外一個下拉選準備的,一旦下拉選的值發生改變,觸發事件,來根據Id發送ajax請求,獲取數據,說的再多不如來段代碼:

 /*給子風險分類賦值*/
   
  function changeQualifications(){     
   var url;
	var riskId=$("#riskType").val();
   url = prefix + "/getDeptSelectData?parentId="+ $("#riskType").val();
	$.ajax({
		cache: true,
		type: "GET",
		url: url,
		async: false,
		success: function (data) {	
		利用each	遍歷
			/* $.each(data, function(key, value) { 
				$('#riskSonTypeSelect').empty();
				var riskDangerTypeId=value.riskDangerTypeId;
				var riskDangerTypeName=value.riskDangerTypeName;
				$('#riskSonTypeSelect').append('<option id="'+riskDangerTypeId+'">'+riskDangerTypeName+'</option>');
			}); */
			利用for循環遍歷
			$('#riskSonTypeSelect').empty();
			 for(var i=0;i<data.length;i++){
				    var riskDangerTypeId = data[i].riskDangerTypeId;
				    var riskDangerTypeName=data[i].riskDangerTypeName;		   
				    $('#riskSonTypeSelect').append
				    ('<optionvalue="'+riskDangerTypeId+'">'+riskDangerTypeName+'</option>');
			 };
			 $('#riskSonTypeSelect').selectpicker('refresh');  
 			$('#riskSonTypeSelect').selectpicker('render'); 
		}
	});          
    } 

這樣既完成了th:with發送請求,又關聯了下拉選,美哉美哉。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章