在DataTables编辑器中添加行总列

时间:2017-11-23 19:09:40

标签: javascript php mysql ajax datatables

https://datatables.net/forums/discussion/45999

需要为Sum 3 Column(No.1,No.2,No.3)制作列(总计)

喜欢此表

-------------------------
No1 | No2 | No3 | Total |
-------------------------
4  |  2  |  4  |  10
-------------------------
5  |  9  |  6  |  20
-------------------------

如果我可以在SQL中获取它,那么我需要知道如何在列中对这3列进行求和它是好的还是在Javascript中使用

这是我的代码

<script type="text/javascript" language="javascript" class="init"> 
var editor; // use a global for the submit and return data rendering in the examples

$(document).ready(function() {

    editor = new $.fn.dataTable.Editor( {
        ajax:
        {
            url: "examples/php/subjects.php?exam=<?php echo $examid ?>",
            type: "POST",
        },
        table: "#example",
        fields: [ {
                label: "Name Subject:",
                name: "subjects.Name"
            }, {

                label: "Name Class:",
                name: "subjects.C_ID",
                type: "select",
                placeholder: "Select a Class"

            }, {

                label: "Name Exam:",
                name: "subjects.ExamID",
                type: "select",
                placeholder: "Select a exam",
                "default": <?php echo $_GET['exam'] ?>

            }, {
                label: "No1:",
                name: "subjects.Exam1"
            },
            {
                label: "No2:",
                name: "subjects.Exam2"
            },
            {
                label: "اNo3:",
                name: "subjects.Exam3"
            }
        ]
    } );

    var table = $('#example').DataTable( {
        responsive: true,
        dom: "Bfrtip",
        "pageLength": 100,
        ajax:
        {
            url: "examples/php/subjects.php",
            type: "GET",
            data: {
                "exam": <?php echo $examid ?>               },
        },
        ajaxUrl: "examples/php/subjects.php",
        columns: [
            {
                data: null,
                defaultContent: '',
                className: 'select-checkbox',
                orderable: false
            },
            { data: "subjects.Name" },
            { data: "classes.Name" },
            { data: "subjects.Exam1" },
            { data: "subjects.Exam2" },
            { data: "subjects.Exam3" },
            { data: "subjects.Exam3" },
            { data: Need sum here [Exam1, Exam2, Exam3] },

        ],
        autoFill: {
            columns: [1],
            editor:  editor
        },
        keys: {
            columns: [1],
            editor:  editor,
        },
        select: {
            style:    'os',
            selector: 'td:first-child',
            blurable: true
        },
        buttons: [
            { extend: "create", editor: editor },
            { extend: "edit",   editor: editor },
            { extend: "remove", editor: editor }
        ]
    } );
} );

需要在这里制作总{ data: [Exam1, Exam2, Exam3] },可以帮助我如何制作它,请

{ data: "subjects.Name" },
{ data: "classes.Name" },
{ data: "subjects.Exam1" },
{ data: "subjects.Exam2" },
{ data: "subjects.Exam3" },
{ data: "subjects.Exam3" },
{ data: Need sum here [Exam1, Exam2, Exam3] },

服务器端

<?php
use
DataTables\Editor,
DataTables\Editor\Field,
DataTables\Editor\Format,
DataTables\Editor\Mjoin,
DataTables\Editor\Options,
DataTables\Editor\Upload,
DataTables\Editor\Validate;
// Build our Editor instance and process the data coming from _POST
Editor::inst( $db, 'subjects', 'SubID' )
->fields(
    Field::inst( 'subjects.Name' )->validator( 'Validate::notEmpty' ),
    Field::inst( 'subjects.Exam1' )
        ->validator( 'Validate::numeric' )
        ->setFormatter( 'Format::ifEmpty', null ),
    Field::inst( 'subjects.Exam2' )
        ->validator( 'Validate::numeric' )
        ->setFormatter( 'Format::ifEmpty', null ),
    Field::inst( 'subjects.Exam3' )
        ->validator( 'Validate::numeric' )
        ->setFormatter( 'Format::ifEmpty', null ),
    Field::inst( 'subjects.C_ID' )
        ->options( Options::inst()
        ->table( 'classes' )
        ->value( 'CID' )
        ->label( 'Name' )
        )
        ->validator( 'Validate::dbValues' ),
    Field::inst( 'classes.Name' ),
    Field::inst( 'subjects.ExamID' )
        ->options( Options::inst()
        ->table( 'exams' )
        ->value( 'ID' )
        ->label( 'Name' )
        )
        ->validator( 'Validate::dbValues' ),
    Field::inst( 'exams.Name' )
)
->leftJoin( 'classes', 'classes.CID', '=', 'subjects.C_ID' ) 
->leftJoin( 'exams', 'exams.ID', '=', 'subjects.ExamID' ) 
->where( 'subjects.ExamID', $_GET['exam'] )
->process( $_POST )
->json();

我在这里遇到错误

服务器端

Field::value( '(subjects.Exam1 + subjects.Exam2 + subjects.Exam3) as Total')

JS

{ data: "subject.Total" },

解决

"columnDefs": [
        {
            //
            "render": function ( data, type, row ) {
                return +row.subjects.Exam1 + +row.subjects.Exam2 + 
                +row.subjects.Exam3;
            },
                "targets": 5
        }

1 个答案:

答案 0 :(得分:0)

SQLno1 + no2 + no3

等列进行求和
CREATE TABLE sometable (
  no1 int,
  no2 int,
  no3 int
);

INSERT into sometable (no1, no2, no3) values(42, 6, 10);
INSERT into sometable (no1, no2, no3) values(48, 6, 10)

选择陈述

SELECT no1, no2, no3, (no1 + no2 + no3) AS Total FROM sometable

结果 select results