project-work-point-select-form.blade.php 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <?php
  2. ?>
  3. <style type="text/css">
  4. </style>
  5. <div class="layui-form-item {{ $errors->has('project_id') ? 'has-error' : '' }}">
  6. <label class="layui-form-label" style="padding: 0px;">在用项目</label>
  7. <div class="layui-input-block">
  8. <select name="project_id" lay-filter="project_id">
  9. @foreach($options as $option)
  10. <option value="{{$option->value}}" {{$option->value == $project_id ? 'selected' : ''}}>{{$option->text}}</option>
  11. @endforeach
  12. </select>
  13. @if($errors->has('project_id'))
  14. <span class="help-block">{{ $errors->first('project_id') }}</span>
  15. @endif
  16. </div>
  17. @if($errors->has('project_id'))
  18. <span class="help-block">{{ $errors->first('project_id') }}</span>
  19. @endif
  20. </div>
  21. @if(!isset($hide_spec))
  22. <div class="layui-form-item {{ $errors->has('work_point_id') ? 'has-error' : '' }}">
  23. <label class="layui-form-label" style="padding: 0px;">目前工点</label>
  24. <div class="layui-input-block">
  25. <select name="work_point_id" lay-filter="work_point_id">
  26. </select>
  27. @if($errors->has('work_point_id'))
  28. <span class="help-block">{{ $errors->first('work_point_id') }}</span>
  29. @endif
  30. </div>
  31. </div>
  32. @endif
  33. <script>
  34. $(function() {
  35. layui.use(['form'], function() {
  36. let form = layui.form;
  37. let projects = JSON.parse('{!! $options !!}');
  38. let work_points = [];
  39. let project_id = '{{ $project_id }}';
  40. let work_point_id = '{{ $work_point_id }}';
  41. let hide_spec = '{{ isset($hide_spec) ? "yes" : "no" }}';
  42. // console.log(hide_spec)
  43. form.on('select(project_id)', function(data) {
  44. project_id = data.value;
  45. work_point_id = '';
  46. updateWorkPoint()
  47. });
  48. updateWorkPoint();
  49. function updateWorkPoint() {
  50. let name_index = getIndex(projects, project_id);
  51. work_points = projects[name_index].work_points;
  52. let options = '';
  53. for(let i = 0; i < work_points.length; ++i) {
  54. options += '<option value=' + work_points[i].value + (parseInt(work_points[i].value) === parseInt(work_point_id) ? ' selected' : '') + '>' + work_points[i].text + '</option>'
  55. }
  56. $("select[name='work_point_id']").html(options);
  57. form.render('select');
  58. }
  59. function getIndex(list, id) {
  60. id = parseInt(id);
  61. for(var i = 0; i < list.length; ++i) {
  62. if(list[i].value === id) return i;
  63. }
  64. return 0;
  65. }
  66. })
  67. })
  68. </script>