yii2 with dhtmlx scheduler and select populated from the server

∥☆過路亽.° 提交于 2019-12-11 17:17:01

问题


I am trying to populate the project select dropdown with data from the server. I am using yii2.

My controller data action:

public function actionData()
{
    $list = new OptionsConnector(null, "PHPYii");
    $list->render_table("project", "id", "id(value),name(label)");
    $connector = new JSONSchedulerConnector(null, "PHPYii");
    $connector->set_options("project", $list);
    $connector->configure(
        new Booking(), "id", "start, end, activity, user, subsubproject, status, comment"
    );
    $connector->render();
}

I get an error message:

Exception 'Error' with message 'Call to a member function find() on string'

And I think this line is the cause: $connector->set_options("project", $list);

What should I change?

UPDATE: So I am here now:

public function actionData() {
    $list = new JSONOptionsConnector(null, "PHPYii");
    $list->enable_log("text1.log");
    $list->configure(
        new Subsubproject(),
            "-","id, name"
    );
    $list->render();
    $connector = new JSONSchedulerConnector(null, "PHPYii");
    $connector->enable_log("text2.log");
    $connector->set_options("subsubprojects", $list);
    $connector->configure(
        new Booking(),
            "id", "start, end, activity, user, subsubproject, status,comment"
    );
    $connector->render();
}

and I get this:

0: Object { key: undefined, id: 1, name: "Thing1", … }
​1: Object { key: undefined, id: 2, name: "Thing2", … }
​2: Object { key: undefined, id: 3, name: "Thing3", … }

I don't have keys... How can I get some? :)


回答1:


1) You don't need to call the render method of JSONOptionsConnector directly. Calling it ends processing of the request if I'm not mistaken, so the SchedulerConnector takes no effect Try commenting out $list->render(); line.

2) The response format seems a bit off. This may be a bug of PHPYii wrapper of dhtmlx connector, I'm not sure

According to source codes the client-side needs value and label properties from options, and while handler returns id and name.

You can try something following:

public function actionData() {
    $list = new JSONOptionsConnector(null, "PHPYii");
    $list->enable_log("text1.log");
    $list->configure(
        new Subsubproject(),
            "id","id(value), name(label)"
            // or 
            // "id(value)","id(value), name(label)"
    );

    $connector->enable_log("text2.log");
    $connector->set_options("subsubprojects", $list);
    $connector->configure(
        new Booking(),
            "id", "start, end, activity, user, subsubproject, status,comment"
    );
    $connector->render();
}

This should produce a json response containing a list of booking and subprojects. However, I can't test this code so something may still be wrong. You can try it and see whether the result JSON looks right.

If it doesn't get you any closer, I honestly would produce json manually rather than using a connector with PHPYii wrapper. That way you'll have direct control over what is returned from your controller and won't have another black box there.

You'll need to return a json of the following structure from your action: https://docs.dhtmlx.com/scheduler/data_formats.html#jsonwithcollections

so you'll have something like this in your action:

return $this->asJson([
  "data"=> $preparedEventsArray
  "collections" => [
      "subprojects"=> $preparedSubprojects
   ]
]);

where $preparedEventsArray is an array of event objects as shown in docs, and $subprojects - your value/label objects

Note that names of properties in the data collection - "id","start_date","end_date","text" - are mandatory, you'll have to map your data model to this structure, e.g.

start -> start_date
end -> end_date
activity -> text

all other properties can have their names unchanged.

The official docs don't have a sample code for Yii2, unfortunately. There are common docs for server formats https://docs.dhtmlx.com/scheduler/server_integration.html And tutorials for PHP Slim framework and Laravel, which is not exactly what you need, but the closest thing the current documentation has.



来源:https://stackoverflow.com/questions/54150808/yii2-with-dhtmlx-scheduler-and-select-populated-from-the-server

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!