HasNames.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. * HTML element names.
  17. *
  18. * @var array
  19. */
  20. protected $elementNames = [
  21. 'grid_row' => 'grid-row',
  22. 'grid_select_all' => 'grid-select-all',
  23. 'grid_per_page' => 'grid-per-pager',
  24. 'export_selected' => 'export-selected',
  25. ];
  26. /**
  27. * Set name to grid.
  28. *
  29. * @param string $name
  30. *
  31. * @return $this
  32. */
  33. public function setName($name)
  34. {
  35. $this->_name = $name;
  36. $this->tableId = $this->makeName($this->tableId);
  37. return $this;
  38. }
  39. /**
  40. * Get name of grid.
  41. *
  42. * @return string
  43. */
  44. public function getName()
  45. {
  46. return $this->_name;
  47. }
  48. /**
  49. * Retrieve an input item from the request.
  50. *
  51. * @param string $key
  52. *
  53. * @return mixed
  54. */
  55. public function getRequestInput($key)
  56. {
  57. return $this->request->get($this->makeName($key));
  58. }
  59. /**
  60. * @param string $key
  61. *
  62. * @return string
  63. */
  64. public function makeName($key)
  65. {
  66. return $this->getNamePrefix().$key;
  67. }
  68. /**
  69. * @return string
  70. */
  71. public function getNamePrefix()
  72. {
  73. if (! $name = $this->getName()) {
  74. return;
  75. }
  76. return $name.'-';
  77. }
  78. /**
  79. * @return string
  80. */
  81. public function getRowName()
  82. {
  83. return $this->getElementNameWithPrefix('grid_row');
  84. }
  85. /**
  86. * @return string
  87. */
  88. public function getSelectAllName()
  89. {
  90. return $this->getElementNameWithPrefix('grid_select_all');
  91. }
  92. /**
  93. * @return string
  94. */
  95. public function getPerPageName()
  96. {
  97. return $this->getElementNameWithPrefix('grid_per_page');
  98. }
  99. /**
  100. * @return string
  101. */
  102. public function getExportSelectedName()
  103. {
  104. return $this->getElementNameWithPrefix('export_selected');
  105. }
  106. /**
  107. * @return string
  108. */
  109. protected function getElementNameWithPrefix($name)
  110. {
  111. $elementName = $this->elementNames[$name];
  112. if ($this->_name) {
  113. return sprintf('%s-%s', $this->_name, $elementName);
  114. }
  115. return $elementName;
  116. }
  117. }