HasNames.php 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. namespace Dcat\Admin\Grid\Concerns;
  3. use Dcat\Admin\Grid;
  4. /**
  5. * @method Grid\Model model()
  6. */
  7. trait HasNames
  8. {
  9. /**
  10. * Grid name.
  11. *
  12. * @var string
  13. */
  14. protected $_name;
  15. /**
  16. * Set name to grid.
  17. *
  18. * @param string $name
  19. *
  20. * @return $this
  21. */
  22. public function setName($name)
  23. {
  24. $this->_name = $name;
  25. $this->tableId = $this->makeName($this->tableId);
  26. return $this;
  27. }
  28. /**
  29. * Get name of grid.
  30. *
  31. * @return string
  32. */
  33. public function getName()
  34. {
  35. return $this->_name;
  36. }
  37. /**
  38. * Retrieve an input item from the request.
  39. *
  40. * @param string $key
  41. *
  42. * @return mixed
  43. */
  44. public function getRequestInput($key)
  45. {
  46. return $this->request->get($this->makeName($key));
  47. }
  48. /**
  49. * @param string $key
  50. *
  51. * @return string
  52. */
  53. public function makeName($key)
  54. {
  55. return $this->getNamePrefix().$key;
  56. }
  57. /**
  58. * @return string
  59. */
  60. public function getNamePrefix()
  61. {
  62. if (! $name = $this->getName()) {
  63. return;
  64. }
  65. return $name.'_';
  66. }
  67. /**
  68. * @return string
  69. */
  70. public function getRowName()
  71. {
  72. return $this->makeName('grid-row');
  73. }
  74. /**
  75. * @return string
  76. */
  77. public function getSelectAllName()
  78. {
  79. return $this->makeName('grid-select-all');
  80. }
  81. /**
  82. * @return string
  83. */
  84. public function getPerPageName()
  85. {
  86. return $this->makeName('grid-per-page');
  87. }
  88. /**
  89. * @return string
  90. */
  91. public function getExportSelectedName()
  92. {
  93. return $this->makeName('export-selected');
  94. }
  95. }