【问题标题】:Adding a Row-Total Column in DataTables editor在 DataTables 编辑器中添加行总计列
【发布时间】:2018-05-07 19:23:00
【问题描述】:

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

需要为 Sum 3 Column(No.1, No.2, No.3) 制作 Column(total)

喜欢这张表

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

需要知道如何将这 3 列汇总到列中,如果我可以在 SQL 中获取它会很好或在 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
        }

【问题讨论】:

  • 您需要“subjects.Exam1”+“subjects.Exam2”+“subjects.Exam3”之和还是什么?
  • 是的,我需要在列总计中进行 Exam1 和 Exam2 和 Exam3
  • 您正在从服务器获取您的对象data 对吧?你能发一个console.log(data) 吗?
  • 是从服务器,但我需要使服务器端 fetch 总计 3 列
  • 我想知道您在客户端拥有的服务器的数据是什么。你还没有任何东西吗?

标签: javascript php mysql ajax datatables


【解决方案1】:

使用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

结果

【讨论】:

    猜你喜欢
    • 2019-09-18
    • 1970-01-01
    • 2020-03-04
    • 2018-09-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-30
    相关资源
    最近更新 更多