Symfony2 coloring table rows depending on database value and config parameter

拈花ヽ惹草 提交于 2020-01-16 14:47:59

问题


Good day every one: I am new Symfony and this might seem simple to someone but to me is complex. I have a list of objects an entity content called worker from where I create a CollectionArray. That worker has a parameter that is a DateInterval let's call it timeRamaining. Then I have 2 config parameter that come from app/config.yml, those parameter I can call them Dangerrous and veryDangerous (Orange and RED)... to say if the time remaining is close to some event or not, in this case Retirement. Now In the view I have an HTML table that shows the workers List depending on search parameters, I have been struggling the whole afternoon to make this list to show red or orange rows in case this remaining time is Dangerous (orange) or veryDangerous (Red). I do not know how is supposed to be responsible of every task... for instance, determining the color of the row? Is a task that I should handle to the worker or the controller or the view? the worker is the place where it seems easier because the controller has to go up and down the whole array, but the worker is too far from the view, it's programming logic and then in the entity I do not have access to the parameters that I need from the app/config.yml. This question might seem silly, but is really giving me a hard time... any similar example that you can show me will be well received and appreciated. Thank you


回答1:


In your controller you need to fetch your configuration options and pass it to your view as well as your data.

public function tableAction() {
    $dangerousThreshold     = $this->container->getParameter('dangerous_threshold');
    $veryDangerousThreshold = $this->container->getParameter('very_dangerous_threshold');

   // If required transform your threshold in a comparable value, eg. a date.

    $data = $this->container->get('your_repository')->findAll();

    return $this->render(
        'YourBundle:Dashboard:table.html.twig',
        array(
            'data'                     => $data,
            'dangerous_threshold'      => $dangerousThreshold,
            'very_dangerous_threshold' => $veryDangerousThreshold,
        )
    );
}

And in the view, compare the $data date to both dangerous & very dangerous threshold and assign a css class which will allow you to set any style you want.



来源:https://stackoverflow.com/questions/19374066/symfony2-coloring-table-rows-depending-on-database-value-and-config-parameter

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