javascript - Jtable 中的列不显示

标签 javascript symfony datagrid jtable

我使用 JTable 创建了一个 Datagrid,这是我的 JavaScript 代码:

<script type="text/javascript">
    $(document).ready(function () {
        $('#PersonTableContainer').jtable({
            title: 'Table of people',
            actions: {
                listAction: '{{ path("person_list") }}',
                createAction: '',
                updateAction: '',
                deleteAction: ''
            },
            fields: {
                PersonId: {
                    key: true,
                    list: false
                },
                Name: {
                    title: 'Name',
                    width: '40%'
                },
                Age: {
                    title: 'Age',
                    width: '20%'
                },
                PaysId: {
                    title: 'Country',
                    width: '30%'
                }
            }
        });
    });
</script>

代码工作正常,它显示了所有信息。

____________________________
Name      |  Age    | Country
----------------------------
Mohssine  | 22      | France
Saad      | 10      | USA
____________________________

添加此代码后:

options: '{{ path("get_countries") }}',

当用户想要更改或创建新记录时,为用户显示一个组合框,代码如下:

<script type="text/javascript">
    $(document).ready(function () {
        $('#PersonTableContainer').jtable({
            title: 'Table of people',
            actions: {
                listAction: '{{ path("person_list") }}',
                createAction: '',
                updateAction: '',
                deleteAction: ''
            },
            fields: {
                PersonId: {
                    key: true,
                    list: false
                },
                Name: {
                    title: 'Name',
                    width: '40%'
                },
                Age: {
                    title: 'Age',
                    width: '20%'
                },
                PaysId: {
                    title: 'Country',
                    options: '{{ path("get_countries") }}',
                    width: '30%'
                }
            }
        });
    });
</script>

PHP 代码:返回国家/地区列表的函数

public function getCountriesAction()
    {
        $stmt = $this->getDoctrine()->getEntityManager()
            ->getConnection()
            ->prepare('select country as DisplayText,id as value from countries');
        $stmt->execute();
        $countries = $stmt->fetchAll();

        $jTableResult = array();
        $jTableResult['Result'] = "OK";
        $jTableResult['Options'] = $countries;
        $serializer = new Serializer(array(new GetSetMethodNormalizer()), array('json' =>  new JsonEncoder()));
        $content = $serializer->encode($jTableResult, 'json');
        return new Response($content);
    }

当我显示数据网格时,所有信息都在那里,除了我在代码中添加的列之外,它显示时没有信息。

____________________________
Name      |  Age    | Country
----------------------------
Mohssine  | 22      | 
Saad      | 10      | 
____________________________

请提供解决方案,谢谢。

最佳答案

您必须更改您的函数getCountriesAction:

public function getregionsAction()
    {
        $stmt = $this->getDoctrine()->getEntityManager()
            ->getConnection()
            ->prepare('select country,id from countries');
        $stmt->execute();
        $countries= $stmt->fetchAll();



        foreach ($countries=  as $key => $value) {

            $eil = array();
            $eil["DisplayText"] = $countries[$key]['country'];
            $eil["Value"] = $countries[$key]['id'];
            $rows[] = $eil;  
        }

        $jTableResult = array();
        $jTableResult['Result'] = "OK";
        $jTableResult['Options'] = $rows;

        $serializer = new Serializer(array(new GetSetMethodNormalizer()), array('json' => new JsonEncoder()));
        $content = $serializer->encode($jTableResult, 'json');
        return new Response($content);
    }

请检查:https://gist.github.com/cristic84/5883136

希望这个解决方案能够帮助您。

关于javascript - Jtable 中的列不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20583953/

相关文章:

javascript - 如何在不延迟的情况下正确获取 setInterval 内的某些元素宽度?

javascript - Node.js 中的 Promise Bluebird 出现问题

symfony - 覆盖路由器并将参数添加到特定路由(在使用路径/url 之前)

javascript - Dojo datagrid - 尚未绘制的行的 getSelected()

wpf - 将 WPFToolkit DataGrid 的事件绑定(bind)到 ModelView 的命令 (MVVM)

javascript - 遍历嵌套的 json 数组

javascript - window.showmodaldialog 在 chrome 中不起作用

Symfony2 错误 500 而不是 404 在生产中

database - 无法使用 Doctrine 从表文章中获取用户

c# - 绑定(bind)到父 DataContext 中的依赖属性