Editable.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <?php
  2. namespace Dcat\Admin\Grid\Displayers;
  3. use Dcat\Admin\Admin;
  4. class Editable extends AbstractDisplayer
  5. {
  6. protected $class = "grid-editable";
  7. public function display()
  8. {
  9. $id = $this->getKey();
  10. $name = $this->column->getName();
  11. $resource = $this->resource() . "/" . $id;
  12. $this->setupScript();
  13. return "<span class=\"{$this->class}\" style=\"border-bottom:dashed 1px #0088cc\" data-value=\"{$this->value}\" data-name=\"{$name}\" data-id=\"{$id}\" data-url=\"{$resource}\" >{$this->value}</span>";
  14. }
  15. protected function setupScript()
  16. {
  17. $script = <<<JS
  18. $(".{$this->class}").on("click",function() {
  19. $(this).attr('contenteditable', true);
  20. })
  21. $(".{$this->class}").on("blur",function() {
  22. var obj = $(this);
  23. var url = obj.attr('data-url').trim();
  24. var name = obj.attr('data-name').trim();
  25. var old_value = obj.attr('data-value').trim();
  26. var value = obj.html().trim();
  27. if (value != old_value) {
  28. var data = {
  29. _token: Dcat.token,
  30. _method: 'PUT'
  31. };
  32. data[name] = value;
  33. Dcat.NP.start();
  34. $.ajax({
  35. url: url,
  36. type: "POST",
  37. data: data,
  38. success: function (data) {
  39. Dcat.NP.done();
  40. if (data.status) {
  41. obj.attr('data-value',value)
  42. Dcat.success(data.message);
  43. } else {
  44. obj.html(old_value)
  45. Dcat.error(data.message);
  46. }
  47. },
  48. error:function(a,b,c) {
  49. Dcat.NP.done();
  50. obj.html(old_value)
  51. Dcat.handleAjaxError(a, b, c);
  52. }
  53. });
  54. }
  55. })
  56. JS;
  57. Admin::script($script);
  58. }
  59. }