DIV CSS 佈局教程網

為 EasyUI datagrid editor 加上 combogrid 搜尋框
編輯:關於JavaScript     

datagrid 的 editor 本身不支援 combogrid,得先自行擴充。

一、擴充 editor

 $.extend($.fn.datagrid.defaults.editors, {
  combogrid: {
    init: function (container, options) {
      var input = $('<input type="text" class="datagrid-editable-input">').appendTo(container);
      input.combogrid(options);
      return input;
    },
    destroy: function (target) { $(target).combogrid('destroy'); },
    getValue: function (target) { return $(target).combogrid('getValue'); },
    setValue: function (target, value) { $(target).combogrid('setValue', value); },
    resize: function (target, width) { $(target).combogrid('resize', width); }
  }
});

二、把搜尋框放進 toolbar

 <div id="Marketer_ID_Member_bar">
<table cellpadding="0" cellspacing="0" style="width: 100%;">
  <tr>
    <td style="text-align: right; padding-right: 15px">
      <input id="userName_Member" placeholder="姓名/用戶名" style="width: 100px; border: 0;" />
      <input type="button" value="查詢" onclick="sy.memberInfo.userSearch_Member();" />
    </td>
  </tr>
</table>
</div>

再用 toolbar: "#Marketer_ID_Member_bar" 綁定到表格上。

三、折騰的過程

照一般做法,搜尋函式會寫成這樣:

 $('#Marketer_ID_Member').combogrid('grid').datagrid('options').queryParams.Name = $("#userName_Member").val();
$('#Marketer_ID_Member').combogrid('grid').datagrid('reload');

但 editor 裡的元素並沒有明確的 id 可供選取。我先嘗試直接幫它加上 id 屬性,無效;接著嘗試在 combogrid 載入完成時透過 Marketer_ID_Member_bar 反查父層並補上 id,雖然賦值成功,但那個父層其實並不是 combogrid,也失敗。

最後回頭觀察一般情境下的 combogrid:把一個 input 轉成 combogrid 後,它的 class 會變成 easyui-validatebox combogrid-f combo-f validatebox-text;而 editor 中的輸入框 class 是 datagrid-editable-input combogrid-f combo-f。差異就在 combogrid-f 這個共同標記上。

四、最終解法

既然無法設 id,就用 class 選擇器:

 $('.combogrid-f').combogrid('grid').datagrid('options').queryParams.Name = $("#userName_Member").val();
$('.combogrid-f').combogrid('grid').datagrid('reload');

繞了一圈才找到可行方案。若不想折騰,另一條路是放棄 editor,改用彈出視窗編輯,實務上也是常見取捨。

補充:combogrid 用法要點

1. 必須套用 class="easyui-combogrid",且只能用在 input 或 select 元素上。
2. 參數需寫在 data-options 中。
3. 常用屬性:loadMsg(載入提示)、idField(提交值)、textField(顯示值)、mode('remote' 時以參數 q 向伺服器請求新資料)、filter(mode 為 'local' 時篩選本地資料)。
4. 常用方法:options()、grid()、setValues(values)、setValue(value)、clear()。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved