1. 程式人生 > >layui table列中引數轉換(1=汽車 2=火車 3=飛機) & layui table列中的值替換 (已解決)

layui table列中引數轉換(1=汽車 2=火車 3=飛機) & layui table列中的值替換 (已解決)

問題: 類別屬性為sort 後臺資料庫存的是1 2  3 但是顯示的需要轉換成 人才 政策  專案

程式碼:方法一

頁面:

    <script type="text/html" id="tranSort">
    	{{# if (d.sort==1) { }}
 		  <span>人才</span>
	    {{# } else if(d.sort==2) { }}  
		  <span>政策</span>
	    {{# } else{ }}  
		  <span>專案</span>
	    {{# }}}  
    </script>

小插曲:

本想這種花括號是成對出現,伸展開更容易理解程式碼,萬萬沒想到這種寫法是不認識的,報錯,這是因為這個模板語言不支援

js:


		cols : [ [ // 表頭
		{
			field : 'check',
			title : ' ',
			fixed : 'left',
			type : 'checkbox'
		}, {
			field : 'investId',
			title : '序號',
			sort : true,
			width : 80
		}, {
			field : 'title',
			title : '標題'
		}, {
			field : 'sort',
			title : '類別',
			templet:'#tranSort'
		}
		, {
			field : 'pubdate',
			title : '釋出時間'
		},{
			fixed : 'right',
			title : '操作',
			align : 'center',
			toolbar : '#barDemo',
			width : 180
		} ] ]
		,

 

 

方法二

js:

		done:function(res, curr, count){
			  console.log(res);
			  $("[data-field = 'sort']").children().each(function(){
				  
				    if($(this).text() == '1'){
				 
				      $(this).text("人才");
				 
				    }else if($(this).text() == '2'){
				 
				       $(this).text("政策");
				    }else if($(this).text() == '3'){
				 
				       $(this).text("專案");
				    }
				  })
			    //得到當前頁碼
			    console.log(curr); 
			    //得到資料總量
			    console.log(count);
		}

效果: