DataTable列是显示对象而不显示实际值?

时间:2018-06-18 19:53:14

标签: javascript jquery datatables phalcon

我正在尝试使用我的数据填充DataTable,但DataTable在列中显示对象而不打印实际值!

enter image description here

控制器中的功能:

public function getEvaluationSymptomsAction() {

        //Get Evaluation Symptoms from DataBase
        $evaluation_symptoms = CxEbEvaluationSymptom::getAllEvaluationSymptomsWithNameForDataTable();

        $inferredRemedies = array();

        $additonalRemedies = array();

        $data = array();

        foreach ($evaluation_symptoms as $key => $symptom) {
            $data[ $key ][ 'id' ] = $symptom[ 'id' ];
            $data[ $key ][ 'title' ] = $symptom[ 'title' ];

            // Get Inferred Remedies By Symptom ID
            $inferredRemedies = CxEbEvaluationSymptom::getInferredRemediesBySymptomId($symptom[ 'id' ]);

            $additonalRemedies = CxEbEvaluationSymptom::getAdditionalSymptomRemediesBySymptomId($symptom[ 'id' ]);


            $data[ $key ][ 'remedy' ] = $inferredRemedies;


            $data[ $key ][ 'additional-remedy' ] = $additonalRemedies;


            $data[ $key ][ 'date_created' ] = $symptom[ 'date_created' ];

        }
//print_r($data);exit;
        // Return data array
        return array('data' => $data);
        // Return data
    }

上面的函数返回以下数组:(注意下面的数组是 var_dump 版本实际上数据是 JSON 格式)

Array code is here

查询

public static function getInferredRemediesBySymptomId($symptomId){
        $queryBuilder = new Builder();

        return  $queryBuilder
            ->from(array('es' =>  'Cx\EbFront\Models\Evaluation\CxEbEvaluationSymptom'))
            ->leftJoin('Cx\EbFront\Models\Evaluation\CxEbEvaluationSymptomCause',  'es.id = esc.evaluation_symptom_id','esc')
            ->leftJoin('Cx\EbFront\Models\Evaluation\CxEbEvaluationCause','esc.evaluation_cause_id = ec.id','ec')
            ->leftJoin('Cx\EbFront\Models\Evaluation\CxEbEvaluationCauseRemedy','ec.id = ecr.evaluation_cause_id','ecr')
            ->leftJoin('Cx\EbFront\Models\Evaluation\CxEbEvaluationRemedy','ecr.evaluation_remedy_id = er.id', 'er')
            //->leftJoin('Cx\EbFront\Models\Evaluation\CxEbEvaluationSymptom', 'es.id = :ID:', 'es')
            ->columns('er.id, er.title')
            ->where('es.is_active = 1')
            ->andWhere('es.id = :ID:')
            ->getQuery()
            ->execute(array('ID' => $symptomId))
            //->execute()->setHydrateMode(Resultset::HYDRATE_ARRAYS)
            ->toArray();
    }

    /**
     * A
     * Data for column Additonal Remedies in DataTable
     * @return array
     */
    public static function getAdditionalSymptomRemediesBySymptomId($symptomId){
        $queryBuilder = new Builder();

        return  $queryBuilder
            ->from(array('er' =>  'Cx\EbFront\Models\Evaluation\CxEbEvaluationRemedy'))
            ->innerJoin('Cx\EbFront\Models\Evaluation\CxEbEvaluationSymptomRemedy',  'er.id = esr.evaluation_remedy_id','esr')
            ->columns('er.id, er.title')
            ->where('er.is_active = 1')
            ->andWhere('esr.evaluation_symptom_id = :ID:')
            ->getQuery()
            ->execute(array('ID' => $symptomId))
            ->toArray();
    }

以下是用于初始化DataTable的JS代码:

function EbEvaluationSymptom(){
    var $body = $('body');
    var $CxRecordsTable = $('#cx-records-table');

    // Init the DatTable using the Cx Admin DataTable plugin
    cx.common.data.cxAdminDataTables.EbEvaluationSymptom = $CxRecordsTable.cxAdminDataTable({
        ajaxUrl: '<?php echo $this->CxHelper->Route('eb-admin-get-evaluation-symptoms')?>',
        columns: [
            cx.common.admin.tableEditColumn('id'),
            { data: 'title' },
            { data: 'remedy' },
            { data: 'additional-remedy' },
            { data: 'date_created' }

        ],

    });

};

JSON响应:

https://pastebin.com/g2mhhS4D

1 个答案:

答案 0 :(得分:0)

DataTables不知道如何格式化为补救和其他补救措施提供的对象数组。 假设每个数组中始终有一个对象,您要访问每个数组中的第一个对象并使用其title属性:

    Class.forName(driverName)
    val connection = DriverManager.getConnection(url, 
    Properties().apply {
        setProperty("user", username)
        setProperty("password", password)
    })
    val stmt = connection.createStatement()
    val rs = stmt.executeQuery(query)

    while (rs.next()) {
       println(rs.getString("name"))
    }

    rs.close()
    stmt.close()
    connection.close()

如果您计划在每个数组中包含多个元素,则需要采用不同的方法