之前一直在使用easyui中,忽視了官網上的小細節,類似於datagrid、combobox 等組件在使用的時候,它的數據加載方式分為兩種:
官網中:
①在html中,比如:
<table id="dg" title="貨主會員管理" class="easyui-datagrid" fitColumns="true" pagination="true" rownumbers="true" nowrap="true" url="/shipper/shipper/list" fit="true" toolbar="#tb" data-options="pageSize:25,pageList:[10,15,25,50,100],singleSelect:true"> <thead>
這種是直接在html中聲明了datagrid組件,並請求了一次url;
②在js中,比如:
jQuery('#dg').datagrid({ //綁定雙擊行事件 url: '/shipper/shipper/list', onDblClickCell: function (index, field, value) { editDetail(index); } });
之前自己在使用的時候,一直存在一個誤區:在js中使用datagrid的所有事件的時候,必須在html中用class聲明該組件,才可以使用該組件的所有事件,現在才恍然大悟:兩種只要任選其一就OK了。這樣在使用過程中,就不會出現加載兩次url問題了。
總結:html代碼中利用class聲明了datagrid,導致easyUI解析class代碼的時候先解析class聲明中的datagrid,這樣組件就請求了一次url;然后又調用js初始化代碼請求一次url。這樣導致了重復加載,解決的方法就是只用一種初始化方法來聲明easyUI組件以避免重復的提交請求,即刪除html中的class聲明(class="easyui-datagrid"),其他組件應該也是有這樣的問題,再用的時候記得觸類旁通!當然,如果不使用js綁定控件的事件,就不會出現重復提交的問題,可以根據實際情況使用html設置url.