RowAction.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. namespace Dcat\Admin\Grid;
  3. use Illuminate\Support\Fluent;
  4. use Illuminate\Support\Str;
  5. abstract class RowAction extends GridAction
  6. {
  7. /**
  8. * @var Fluent
  9. */
  10. protected $row;
  11. /**
  12. * @var Column
  13. */
  14. protected $column;
  15. /**
  16. * @var string
  17. */
  18. public $selectorPrefix = '.grid-row-action-';
  19. /**
  20. * Get primary key value of current row.
  21. *
  22. * @return mixed
  23. */
  24. public function getKey()
  25. {
  26. if ($this->row) {
  27. return $this->row->get($this->parent->getKeyName());
  28. }
  29. return parent::getKey();
  30. }
  31. /**
  32. * Set row model.
  33. *
  34. * @param mixed $key
  35. *
  36. * @return \Illuminate\Database\Eloquent\Model|mixed
  37. */
  38. public function row($key = null)
  39. {
  40. if (func_num_args() == 0) {
  41. return $this->row;
  42. }
  43. return $this->row->{$key};
  44. }
  45. /**
  46. * Set row model.
  47. *
  48. * @param Fluent $row
  49. *
  50. * @return $this
  51. */
  52. public function setRow($row)
  53. {
  54. $this->row = $row;
  55. return $this;
  56. }
  57. public function getRow()
  58. {
  59. return $this->row;
  60. }
  61. /**
  62. * @param Column $column
  63. *
  64. * @return $this
  65. */
  66. public function setColumn(Column $column)
  67. {
  68. $this->column = $column;
  69. return $this;
  70. }
  71. /**
  72. * 生成选择器.
  73. * 需要保证每个行操作的选择器都不同.
  74. *
  75. * @param string $prefix
  76. * @param string $class
  77. *
  78. * @return string
  79. */
  80. public function makeSelector($prefix, $class = null)
  81. {
  82. $class = $class ?: static::class;
  83. $key = $prefix.'-'.$class.'-'.$this->getKey();
  84. if (! isset(static::$selectors[$key])) {
  85. static::$selectors[$key] = $prefix.Str::random(8);
  86. }
  87. return static::$selectors[$key];
  88. }
  89. }