_script.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. <?php
  2. use yii\helpers\Url;
  3. ?>
  4. <script type="text/javascript">
  5. <?php $this->beginBlock('JS_END') ?>
  6. yii.process = (function ($) {
  7. var _onSearch = false;
  8. var pub = {
  9. roleSearch: function () {
  10. if (!_onSearch) {
  11. _onSearch = true;
  12. var $th = $(this);
  13. setTimeout(function () {
  14. _onSearch = false;
  15. var data = {
  16. id:<?= json_encode($name)?>,
  17. target:$th.data('target'),
  18. term: $th.val(),
  19. };
  20. var target = '#' + $th.data('target');
  21. $.get('<?= Url::toRoute(['role-search']) ?>', data,
  22. function (html) {
  23. $(target).html(html);
  24. });
  25. }, 500);
  26. }
  27. },
  28. action: function () {
  29. var action = $(this).data('action');
  30. var params = $((action == 'assign' ? '#avaliable' : '#assigned')+', .role-search').serialize();
  31. var urlAssign = '<?= Url::toRoute(['assign', 'id' => $name, 'action' => 'assign']) ?>';
  32. var urlDelete = '<?= Url::toRoute(['assign', 'id' => $name, 'action' => 'delete']) ?>';
  33. $.post(action=='assign'?urlAssign : urlDelete,
  34. params,function (r) {
  35. $('#avaliable').html(r[0]);
  36. $('#assigned').html(r[1]);
  37. },'json');
  38. return false;
  39. }
  40. }
  41. return pub;
  42. })(window.jQuery);
  43. <?php $this->endBlock(); ?>
  44. <?php $this->beginBlock('JS_READY') ?>
  45. $('.role-search').keydown(yii.process.roleSearch);
  46. $('a[data-action]').click(yii.process.action);
  47. <?php $this->endBlock(); ?>
  48. </script>
  49. <?php
  50. yii\web\YiiAsset::register($this);
  51. $this->registerJs($this->blocks['JS_END'], yii\web\View::POS_END);
  52. $this->registerJs($this->blocks['JS_READY'], yii\web\View::POS_READY);