You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

243 line
7.9 KiB

  1. Client Side Cookbook
  2. ====================
  3. Standard Patterns for Client Side Scripts
  4. Fetch customer cell no and email id based on name
  5. --------------------------------------------------
  6. This can be implemented using the standard fetch pattern::
  7. cur_frm.add_fetch('customer', 'email_id', 'customer_email');
  8. cur_frm.add_fetch('customer', 'cell_no', 'customer_cell_no');
  9. Form Events (Triggers)
  10. ----------------------
  11. Standard Form-level Triggers are
  12. * refresh - This is called whenever a new record is loaded, or a record is opened, or when a record is saved
  13. * onload - This is called the first time a record is loaded
  14. * setup - This is called the first time a Form is loaded
  15. Some Examples::
  16. cur_frm.cscript.refresh = function(doc, dt, dn) {
  17. // set contextual help
  18. cur_frm.clear_tip();
  19. if(doc.city && !doc.location) cur_frm.set_tip("Its always a good idea to add location to the city");
  20. }
  21. Accessing / Updating Field Objects
  22. ----------------------------------
  23. Every input / field in the Form is an instance of the :class:`_f.Field`. The reference to the field object
  24. can be got using `cur_frm.fields_dict` property. This is a dictionary that contains reference to all field
  25. objects by name or label (in case there is no name).
  26. Properties of the field can be set by setting the `df` dictionary (which represents the `DocField`). Example::
  27. var f = cur_frm.fields_dict['first_name']
  28. f.df.hidden = 1;
  29. f.refresh();
  30. Field Events (Triggers)
  31. -----------------------
  32. Field `onchange` triggers can be set by declaring a function in the `cur_frm.cscript` object (namespace). The
  33. function will be called when the onchange event will be triggered. The function will be passed 3 parameters
  34. * doc - reference to the current main record
  35. * dt - reference to the DocType (this will be different to `doc.doctype` in case of a child (table) trigger)
  36. * dn - reference to the DocType (this will be different to `doc.name` in case of a child (table) trigger)
  37. Example::
  38. cur_frm.cscript.first_name(doc, dt, dn) {
  39. if(doc.first_name.length < 3) {
  40. msgprint("First Name should atleast be 3 characters long.")
  41. }
  42. }
  43. Overloading Link Field queries
  44. ------------------------------
  45. If a filter is to be added to validate values that can be set by `Link` fields, it is necessary to
  46. overload the exiting query method. This can be done by setting the `get_query` method on
  47. the `Field` object. Example::
  48. // standard field
  49. cur_frm.fields_dict['test_link'].get_query = function(doc,dt,dn) {
  50. return "SELECT tabDocType.name FROM tabDocType WHERE IFNULL(tabDocType.issingle,0)=0 AND tabDocType.name LIKE '%s'"
  51. }
  52. // field in a grid
  53. cur_frm.fields_dict['test_grid'].grid.get_field('my_link').get_query = function(doc,dt,dn) {
  54. return "SELECT tabDocType.name FROM tabDocType WHERE IFNULL(tabDocType.issingle,0)=0 AND tabDocType.name LIKE '%s'"
  55. }
  56. Setting contextutal help (Tips)
  57. -------------------------------
  58. Contextual help can be set using the :meth:`_f.Frm.set_tip`, :meth:`_f.Frm.append_tip`, :meth:`_f.Frm.clear_tip`
  59. methods. See Examples::
  60. cur_frm.cscript.refresh = function(doc, doctype, docname) {
  61. cur_frm.clear_tip("")
  62. if(doc.status="Draft") {
  63. cur_frm.set_tip("This is a Draft, to publish, please check on 'Published' before saving")
  64. }
  65. if(doc.is_popular="Yes") {
  66. cur_frm.append_tip("This post is popular!")
  67. }
  68. }
  69. Custom UI using the HTML Field
  70. ------------------------------
  71. Custom UI Objects can be added to forms by using the HTML field. The object can be added in the form wrapper
  72. and reset with latest values on the `refresh` event. Example::
  73. cur_frm.cscript.refresh = function(doc, dt, dn) {
  74. var cs = cur_frm.cscript;
  75. if(!cs.my_object) {
  76. // lets add a listing
  77. cs.my_object = new Listing();
  78. ..
  79. ..
  80. }
  81. cs.my_object.refresh();
  82. }
  83. Useful API Methods
  84. ------------------
  85. .. function:: get_children(child_dt, parent, parentfield, parenttype)
  86. Get list of child records for the given parent record where:
  87. * child_dt is the DocType of the child type
  88. * parent is ths name of the parent record
  89. * parentfield is the fieldname of the child table in the parent DocType
  90. * parenttype is the type of the Parent `DocType`
  91. .. function:: get_field(doctype, fieldname, docname)
  92. Get the field metadata (`DocField` format) for the given field and given record.
  93. **Note:** Separate metadata is maintained for each field of each record. This is because metadata
  94. can be changed by a script only for one record. For example, a field may be hidden in record A but
  95. visible in record B. Hence same metadata cannot be referenced for the two records. Example::
  96. f = get_field(doc.doctype, 'first_name', doc.name);
  97. f.hidden = 1;
  98. refresh_field('first_name');
  99. .. function:: get_server_fields(method, arg, table_field, doc, dt, dn, allow_edit, call_back)
  100. Update the values in the current record by calling a remote method. Example Client Side::
  101. cur_frm.cscript.contact_person = function(doc, cdt, cdn) {
  102. if(doc.contact_person) {
  103. var arg = {'customer':doc.customer_name,'contact_person':doc.contact_person};
  104. get_server_fields('get_contact_details',docstring(arg),'',doc, cdt, cdn, 1);
  105. }
  106. }
  107. Server side version::
  108. def get_contact_details(self, arg):
  109. arg = eval(arg)
  110. contact = sql("select contact_no, email_id from `tabContact` where contact_name = '%s' and customer_name = '%s'" %(arg['contact_person'],arg['customer']), as_dict = 1)
  111. ret = {
  112. 'contact_no' : contact and contact[0]['contact_no'] or '',
  113. 'email_id' : contact and contact[0]['email_id'] or ''
  114. }
  115. return str(ret)
  116. .. function:: $c_get_values(args, doc, dt, dn, user_callback)
  117. Similar to get_server_fields, but no serverside required::
  118. cur_frm.cscript.item_code = function(doc, dt, dn) {
  119. var d = locals[dt][dn];
  120. $c_get_values({
  121. fields:'description,uom' // fields to be updated
  122. ,table_field:'sales_bom_items' // [optional] if the fields are in a table
  123. ,select:'description,stock_uom' // values to be returned
  124. ,from:'tabItem'
  125. ,where:'name="'+d.item_code+'"'
  126. }, doc, dt, dn);
  127. }
  128. .. function:: set_multiple(dt, dn, dict, table_field)
  129. Set mutliple values from a dictionary to a record. In case of Table, pass `tablefield`
  130. .. function:: refresh_many(flist, dn, table_field)
  131. Refresh multiple fields. In case of Table, pass `tablefield`
  132. .. function:: refresh_field(n, docname, table_field)
  133. Refresh a field widget. In case of a table record, mention the `table_field` and row ID `docname`
  134. .. function:: set_field_tip(fieldname, txt)
  135. Set `txt` comment on a field
  136. .. function:: set_field_options(n, options)
  137. Set `options` of a field and `refresh`
  138. .. function:: set_field_permlevel(n, permlevel)
  139. Set `permlevel` of a field and `refresh`
  140. .. function:: hide_field(n)
  141. Hide a field of fieldname `n` or a list of fields `n`
  142. .. function:: unhide_field(n)
  143. Unhide a field of fieldname `n` or a list of fields `n`
  144. Using Templates
  145. ---------------
  146. The standard Form UI Engine can be overridden using the templates. The `template` is HTML code and can be
  147. set in the `template` field of the DocType. To render fields in the template, Element IDs must be set in a
  148. specific pattern. The pattern is
  149. * frm_[DocType]_[fieldname]
  150. See Example::
  151. <h1>Contact Form</h1>
  152. <table>
  153. <tr>
  154. <td>First Name</td>
  155. <td id="frm_Contact_first_name"></td>
  156. </tr>
  157. <tr>
  158. <td>Last Name</td>
  159. <td id="frm_Contact_last_name"></td>
  160. </tr>
  161. <tr>
  162. <td>Email ID</td>
  163. <td id="frm_Contact_email"></td>
  164. </tr>
  165. <tr>
  166. <td></td>
  167. <td><button onclick="cur_frm.save('Save', function() { loadpage('Thank You'); })">Save</button></td>
  168. </tr>
  169. </table>