如何将数据从数据库传递到JavaScript

时间:2019-09-25 09:02:54

标签: javascript php laravel

我想将数据从数据库传递到javascript。在laravel中,我通常使用控制器将数据传递到视图刀片:

public function profileView($id)
    {
        $person = $this->personRepository->getByIdWith($id, ['person_title', 'country', 'salutation']);
        $practitioner_id = $this->practitionerRepository->getIdByPersonId($id);
        $practitioner = $this->practitionerRepository->getById($practitioner_id);
        $specialty_id = PractitionerSpecialty::where('practitioner_id', $practitioner_id)->value('specialty_id');

        return view('person.profile.profile-view')
            ->with('practitioner_activity_type', PractitionerActivityType::where('practitioner_id', $practitioner_id)->first())
    }

然后,我总是在数据表中显示所有内容,因此我使用数据表函数传递数据:

initialization.table(url, idName, columns, columnDefs);
        $('#data-table').DataTable().ajax.url("/" + url + "/all" + "/" + getPersonId()).load();

        $('#data-table').on('draw.dt', function () {
            displayCurrentActivityType(findCurrentActivityType(dataTable.getTable().data().toArray()));

现在,我正在使用的路由上有多个数据表,并且在一个路由上不能有多个数据表。任何人都可以建议我将数据传递给我的js的方法。例如,需要我的活动数据的功能之一如下所示:

var displayCurrentActivityType = function (activity) {
        if (activity != null && typeof (activity) !== 'undefined') {
            $('#main_activity_h').text(activity.activity_type.name);
            $('.activity_color').css('background-color', activity.activity_type.color);
            $('#assigned_activity_h').text("Assigned: " + activity.effective_date);
            if (activity.expiry_date != null) {
                $('#expires_activity_h').text("Expires: " + activity.expiry_date);
            }
        }
    }

我只是缺少activity数据

@edit我试图这样做,但是我认为我做错了事,Blade仍然无法显示数据库中的数据。但是数据库中有关于此从业者的数据。

function getActivities() {
        $.ajax({
            type: "GET",
            url: "/activity-types/get/" + getPersonId(),
            data: {},
            dataType: "json",
            success: function (data) {
                forms.prefillForm(fieldValues, data, '_update', 'rotation_');
                $('#rotation_description').html(data['description']);
                if (data != null && typeof (data) !== 'undefined') {
                    $('#main_activity_h').text(data['name']);
                    $('.activity_color').css(data['color']);
                    $('#assigned_activity_h').text("Assigned: " + data['effective_date']);
                    if (activity.expiry_date != null) {
                        $('#expires_activity_h').text("Expires: " + data['expiry_date']);
                    }
                }
            }
        });
    }

0 个答案:

没有答案