{"action":"created","issue":{"url":"https://api.github.com/repos/hikalkan/jtable/issues/1984","repository_url":"https://api.github.com/repos/hikalkan/jtable","labels_url":"https://api.github.com/repos/hikalkan/jtable/issues/1984/labels{/name}","comments_url":"https://api.github.com/repos/hikalkan/jtable/issues/1984/comments","events_url":"https://api.github.com/repos/hikalkan/jtable/issues/1984/events","html_url":"https://github.com/hikalkan/jtable/issues/1984","id":177666425,"number":1984,"title":"update a record by just clicking on the cell in the table","user":{"login":"ccloquet","id":6105955,"avatar_url":"https://avatars.githubusercontent.com/u/6105955?v=3","gravatar_id":"","url":"https://api.github.com/users/ccloquet","html_url":"https://github.com/ccloquet","followers_url":"https://api.github.com/users/ccloquet/followers","following_url":"https://api.github.com/users/ccloquet/following{/other_user}","gists_url":"https://api.github.com/users/ccloquet/gists{/gist_id}","starred_url":"https://api.github.com/users/ccloquet/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ccloquet/subscriptions","organizations_url":"https://api.github.com/users/ccloquet/orgs","repos_url":"https://api.github.com/users/ccloquet/repos","events_url":"https://api.github.com/users/ccloquet/events{/privacy}","received_events_url":"https://api.github.com/users/ccloquet/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":0,"created_at":"2016-09-18T18:01:54Z","updated_at":"2016-10-09T08:22:10Z","closed_at":null,"body":"I had to toggle boolean values by just clicking on the table. Here is how I proceeded :\r\n\r\n`table.fields` contains the list of the fields for the table.\r\n\r\nIn the fields definition, I specified a function using \r\n\r\n`display: function(d) { return format_yesno(d.record.fieldname) }`\r\n\r\nwhere `format_yesno` refers to \r\n\r\n```\r\nfunction() format_yesno(d)\r\n{\r\n    <div class=\"yesno\"> .... </div>\r\n}\r\n```\r\nThen the magic comes from\r\n\r\n```\r\n$('body').delegate('.yesno', 'click', \r\n\t\tfunction(e) \r\n\t\t{ \r\n\t\t\tvar id \t\t= e.target.parentElement.parentElement.parentElement.attributes['data-record-key'].value // recordId\r\n\t\r\n\t\t\tvar $sel_row \t= $('#table').jtable('getRowByKey', id)\r\n\r\n\t\t\tvar index \t= $(this).parent().parent().children().index($(this).parent()) // column index\r\n\r\n\t\t\tvar i = 0, field_name = null;\r\n\t\t\tfor (var f in table.fields) // look for the field name corresponding to the column index\r\n\t\t\t{\r\n\t\t\t\tif (table.fields.hasOwnProperty(f)) \r\n\t\t\t\t{\r\n\t\t\t\t\tif ( table.fields[f].list == true )\r\n\t\t\t\t\t{\r\n\t\t\t\t\t\t++i\r\n\t\t\t\t\t\tif (i == index)\r\n\t\t\t\t\t\t{\r\n\t\t\t\t\t\t\tfield_name = f\r\n\t\t\t\t\t\t\tbreak;\r\n\t\t\t\t\t\t}\r\n\t\t\t\t\t}\r\n\t\t\t\t}\r\n\t\t\t}\r\n\r\n\t\t\tif (field_name != null)\r\n\t\t\t{\r\n\t            \t\t$sel_row.each(function () {\r\n                \t\t\tvar record = $(this).data('record');\r\n                \t\t \r\n\t\t\t\t\trecord[field_name] = 1 - record[field_name] // toggle\r\n\t\t\t\t\t$('#table').jtable('updateRecord', \r\n\t\t\t\t\t{\r\n\t\t\t\t\t    record: \trecord,\r\n\t\t\t\t\t    error: \tfunction(e){},\r\n\t\t\t\t\t    success:\tfunction(r) {},\r\n\t\t\t\t\t});\r\n\r\n            \t\t\t});\r\n\t\t\t}\r\n\t\t})\r\n```\r\n\r\nAnything wrong with this method ?"},"comment":{"url":"https://api.github.com/repos/hikalkan/jtable/issues/comments/252472405","html_url":"https://github.com/hikalkan/jtable/issues/1984#issuecomment-252472405","issue_url":"https://api.github.com/repos/hikalkan/jtable/issues/1984","id":252472405,"user":{"login":"sheryever","id":41900,"avatar_url":"https://avatars.githubusercontent.com/u/41900?v=3","gravatar_id":"","url":"https://api.github.com/users/sheryever","html_url":"https://github.com/sheryever","followers_url":"https://api.github.com/users/sheryever/followers","following_url":"https://api.github.com/users/sheryever/following{/other_user}","gists_url":"https://api.github.com/users/sheryever/gists{/gist_id}","starred_url":"https://api.github.com/users/sheryever/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/sheryever/subscriptions","organizations_url":"https://api.github.com/users/sheryever/orgs","repos_url":"https://api.github.com/users/sheryever/repos","events_url":"https://api.github.com/users/sheryever/events{/privacy}","received_events_url":"https://api.github.com/users/sheryever/received_events","type":"User","site_admin":false},"created_at":"2016-10-09T08:22:10Z","updated_at":"2016-10-09T08:22:10Z","body":"Try this.\r\n\r\n\r\n```\r\ndisplay: function(d) { return format_yesno(d) }\r\n```\r\n\r\n```javascript\r\nfunction format_yesno(d)\r\n{\r\n    return '<div class=\"yesno\" onclick=\"updateThisRecord(\" + d.record.id +\",\" + d.record.fieldname+\" > .... </div>'\r\n}\r\n\r\nfunction updateThisRecord(recordkey, field_name) {\r\n    var record =  $('#table').jtable('getRowByKey', recordkey)\r\n    record[field_name] = 1 - record[field_name] // toggle\r\n    $('#table').jtable('updateRecord',  {\r\n        record:     record,\r\n        error:  function(e){},\r\n        success: function(r) {},\r\n     });\r\n}\r\n```"}}
{"actor":{"display_login":"sheryever"}}