【问题标题】:Laravel/Ajax/SQL: trying to show the current value on when null or notLaravel/Ajax/SQL:尝试在是否为空时显示当前值
【发布时间】:2021-09-28 11:01:26
【问题描述】:

下面是我的表格的截图。我的目标是在effective_start_datetime,如果其中有值,我希望它显示为状态active,如果它是null,则在编辑时显示inactive。 (在编辑点击时显示当前状态)

模型:(获取 SQL 数据)

$group_edit = HmsBbrGroup::find($group_id);

表格:

<select id="edit_effective_start_datetime" class="form-control w-100 edit_effective_start_datetime">
<option value="active">Active</option>
<option value="inactive">Inactive</option>
</select>

表格:(只有状态不显示)

Ajax:(点击编辑按钮时输出表单内容)

        $(document).on('click','.edit_group',function (e) {
            e.preventDefault();
            var g_id = $(this).val();
            console.log(g_id);
            $('#editGroupModal').modal('show');
            $.ajax({
                type: "GET",
                url: "/clinical/bbr-group-configuration-edit/"+g_id,
                success: function (response) {
                     console.log(response);
                    if(response.status == 404) {
                        $('#success_message').html("");
                        $('#success_message').addClass('alert alert-danger');
                        $('#success_message').text('response.message');
                    } else {
                        $('#edit_group_name').val(response.group_edit.group_name);
                        $('#edit_group_description').val(response.group_edit.group_description);
                        $('#edit_group_id').val(response.group_edit.group_id);

                        $('#edit_effective_start_datetime').val(response.group_edit.effective_start_datetime).change();
                        $('#edit_group_type_id').val(response.group_edit.group_type_id).change();
                    }
                    
                }
            });
        });

从表单中可以看出,我的 ajax 输出除了$('#edit_effective_start_datetime').val(response.group_edit.effective_start_datetime).change(); 中的状态之外的内容@是null还是不是

我尝试添加一个函数,但是这不起作用:

                    $('#edit_effective_start_datetime').val(getgroupstatus(response.group_edit.edit_effective_start_datetime)).change();

function getgroupstatus(status) {
  var g_status = '';
  if (status === null) {
    g_status = 'inactive'
  } else {
    g_status = 'active'
  } 
  return g_status;
}

任何关于如何显示状态的帮助/建议都会有很大帮助,谢谢。

【问题讨论】:

  • 选项的值是activeinactive 所以在assign try .val(response.group_edit.effective_start_datetime ? 'active' : 'inactive')

标签: sql ajax laravel function edit


【解决方案1】:

使用布尔值而不是字符串 - 你的代码会更简单

<select id="edit_effective_start_datetime">
    <option value=0>Inactive</option>
    <option value=1>Active</option>
<select>

$("#edit_effective_start_datetime").val(response.group_edit.effective_start_datetime === null ? 0 : 1);

【讨论】:

    【解决方案2】:

    你可以使用道具

        var effective_start_datetime=response.group_edit.edit_effective_start_datetime?
                                    'active':
                                    'inactive';
    
        $('#edit_effective_start_datetime option[value='+effective_start_datetime+']').prop("selected",true);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-11-27
      • 1970-01-01
      • 2018-11-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-07
      • 2021-09-07
      相关资源
      最近更新 更多