Jtable中的列不显示

时间:2013-12-14 13:34:53

标签: 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") }}',

当用户想要更改或创建新记录时显示一个Combobox,这里是代码:

<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      | 
____________________________

请一个解决方案,谢谢。

1 个答案:

答案 0 :(得分:1)

您必须更改功能 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

我希望这个解决方案可以帮到你。