【问题标题】:Serverside JQuery Datatable Results Error服务器端 JQuery 数据表结果错误
【发布时间】:2018-02-20 16:09:22
【问题描述】:

全部,

我正在使用我在以下网站上找到的服务器端冷融合 jquery 数据表的一个很好的示例。 Site Here

在示例代码中,表格和选择语句在 JS 文件中公开,我需要在搜索中包含多个表格。原始代码如下所示。

原始工作代码

"fnServerData": function ( sSource, aoData, fnCallback ) {
    aoData.push(
        { "name": "table", "value": "ukLocationCodes" },
        { "name": "sql", "value": "SELECT [id], [varCode], [varLocation]" }
        );

        $.ajax( {"dataType": 'json',
             "type": "POST",
             "url": sSource,
             "data": aoData,
             "success": fnCallback} );

原始代码在 ColdFusion 页面上用于查询搜索。

 <cfquery name="rResult" datasource="A8002CMS">
    #preservesinglequotes(form.sql)#
    FROM #form.table#

    WHERE 1 = 1
<cfif len(form.sSearch)>
        AND (
<cfloop from="1" to="#listLen(variables.fieldlist)#" index="variables.index">
#listGetAt(variables.fieldlist, variables.index,',')# LIKE '%#form.sSearch#%' <cfif variables.index LT listLen(variables.fieldlist)> OR </cfif>
</cfloop>
    )
</cfif>
<cfif isdefined('form.iSortCol_0')>
    ORDER BY
<cfloop from="0" to="#form.iSortingCols-1#" index="variables.i">
    #listGetAt(variables.fieldlist,form["iSortCol_#variables.i#"]+1)# #form["sSortDir_#variables.i#"]# 
    <cfif variables.i is not form.iSortingCols-1>, 
    </cfif>
</cfloop>
<!--- create the JSON response --->
<cfsavecontent variable="variables.sOutput"><cfoutput>{
    "sEcho": #form.sEcho#,
    "iTotalRecords": #qGetCount.fullCount#,
    "iTotalDisplayRecords": #rResult.recordcount#,
    "aaData": [ 
    <cfloop query="rResult" startrow="#form.iDisplayStart+1#" endrow="#form.iDisplayStart+form.iDisplayLength#"><cfset variables.count=variables.count+1>
[<cfloop list="#variables.fieldlist#" index="variables.i">
<!--- custom translations --->
"#rResult[variables.i][rResult.currentRow]#"
<cfif variables.i is not listLast(variables.fieldlist)>, </cfif>
</cfloop>]

更改后的代码,在您输入搜索之前可以正常使用

为了查找多个表格并隐藏表格并选择,我将JS脚本更改为以下。

$(document).ready(function() {
    var table = $('#displayData').dataTable( {
    "processing": true,
    "stateSave": true,
    "serverSide": true,
    "ajax": {
    "url": "MyVolunteers.cfm",
    "type": "POST"
        },

    "columns": [ 
                    {"name": "EMPLOYEE_ID" , "className": "hidden"},
                    {"name": "EVER_NUM" , "className": "hidden" , "orderable": "true"},
                    {"name": "LAST_NAME", "title": "LAST NAME", "orderable": "true"},
                    {"name": "FIRST_NAME", "title": "FIRST NAME", "orderable": "true"},
                    {"name": "SortOrderDate", "title": "APP DATE", "orderable": "true"},
                    {"name": "DOCS_VER", "className": "hidden"},
                    {"name": "DOCS_WAIT", "title": "APP STATUS", "orderable": "true"},
                    {"name": "APP_STATUS", "title": "PROGRESS", "orderable": "true"},
                    {"name": "LOCATION_NAME", "title": "LOCATION", "orderable": "true"},
                    {"title": "OPTIONS", "orderable": "false"}
                ],

 "columnDefs": [

                {
                    <cfset E = "+row[0]+"/>
                    "render": function ( data, type, row )
                    {return "<a class='btn btn-primary btn-xs' href='candidate/?select="#E#"'>view</a> <a class='btn btn-success btn-xs' href='candidate/?select=#e_id#"+"'>edit</a>";},
                    "targets": -1
                },

                {

                    "render": function ( data, type, row )
                    {
                          var color = 'black';
                           if (row[5] == 1) {
                            color = 'green';
                            ColorCheck = 'VALIDATED';
                            IconChoice = ' fa fa-check-square-o';
                          } 
                          else if (row[6] == 1) {
                            color = 'orange';
                            ColorCheck = 'WAITING';
                            IconChoice = 'fa fa-spin fa-spinner';
                          } 
                          else  {
                            color = 'red';
                            ColorCheck = 'NON-VALID';
                            IconChoice = 'fa fa-exclamation-triangle';
                          }

                          return '<span style="color:' + color + '"><i class="' + IconChoice + '"></i> ' + ColorCheck + '</span>';
                        },
                        "targets": -4


                },
                {

                         "render": function ( data, type, row )
                         {
                          var appstat = 'black';
                           if (row[5] == 1) {
                            appstat = 'green';
                            TextStatus = 'FINISHED';
                            IconChoice = 'fa fa-check';
                          } 
                           else  {
                            appstat = 'black';
                            var TextStatus = row[7];
                            IconChoice = 'fa fa-chevron-right';
                          }
                          return '<span class="text-uppercase" style="color:' + appstat + '"><i class="' + IconChoice + '"></i> ' + TextStatus + '</span>';
                        },
                        "targets": -3
                },
                { "visible": false,  "targets": [ 0 ] }
        ],
"pagingType": "full_numbers",
"order": [[2,'asc']],
"language": {
        "lengthMenu": "Page length: _MENU_",
        "search": "Filter:",
        "zeroRecords": "No matching records found"
                                },
                                "data": function ( sSource, aoData, fnCallback ) {
        aoData.push(
            );

            $.ajax( {"dataType": 'json',
                 "type": "POST",
                 "url": sSource,
                 "data": aoData,
                 "success": fnCallback} );
                        }
                } );
            } );
setInterval( function () {
$('#displayData').DataTable().ajax.reload();
}, 30000 );

将查询更改为如下所示。

<cfsilent>
<cfparam name="form.table" default="">
<cfparam name="form.columns" default="">
<cfparam name="form.editButtonText" default="">
<cfparam name="form.editButtonTarget" default="">
<cfparam name="form.search" default="">
<cfparam name="variables.fieldlist" default="">

<cfsetting showDebugOutput="true">
<cfsetting enablecfoutputonly="true">
<cfprocessingdirective suppresswhitespace="true">

<!--- this comes from the AJAX script in the template --->
<cfset variables.fieldlist=form.columns>
<cfset variables.count=0>

<!--- strip off the comma if it is the last element --->
<cfif right(variables.fieldlist,'1') EQ ",">
    <!--- last char is a comma --->
    <cfset variables.listLength = len(variables.fieldlist)>
    <cfset variables.fieldlist = left(variables.fieldlist, variables.listLength-1)>
</cfif>

<!--- get count of records --->
<cfquery name="qGetCount" datasource="MySQLDATABASE">
    SELECT COUNT(*) AS fullCount
    FROM VOLTABLE1
    WHERE ARCHIVE IS NULL
</cfquery>

<cfquery name="rResult" datasource="MySQLDATABASE">
    SELECT VOLTABLE1.EMPLOYEE_ID, VOLTABLE1.EVER_NUM, VOLTABLE1.LAST_NAME, VOLTABLE1.FIRST_NAME, VOLTABLE1.DOCS_VER, VOLTABLE1.DOCS_WAIT, VOLTABLE1.APP_STATUS, VOLTABLE1.LOCATION_ID, DATE_FORMAT(VOLTABLE1.EMPLOYEE_DATE,'%Y-%m-%d %r') AS SortOrderDate, VOLTABLE2.LOCATION_NAME
    FROM VOLTABLE1
    RIGHT JOIN
    VOLTABLE2
    ON VOLTABLE1.LOCATION_ID = VOLTABLE2.LOCATION_ID
    WHERE 1 = 1 AND VOLTABLE1.ARCHIVE IS NULL
<cfif len(form.search)>
        AND (
<cfloop from="1" to="#listLen(variables.fieldlist)#" index="variables.index">
#listGetAt(variables.fieldlist, variables.index,',')# LIKE '%#form.search#%' <cfif variables.index LT listLen(variables.fieldlist)> OR </cfif>
</cfloop>
    )
</cfif>
<cfif isdefined('form.iSortCol_0')>
    ORDER BY
<cfloop from="0" to="#form.iSortingCols-1#" index="variables.i">
    #listGetAt(variables.fieldlist,form["iSortCol_#variables.i#"]+1)# #form["sSortDir_#variables.i#"]# 
    <cfif variables.i is not form.iSortingCols-1>, 
    </cfif>
</cfloop>

</cfif>
</cfquery>

<!--- strip off the table name from the values, otherwise it will break making the json --->
<cfset variables.fieldlist = ReplaceNoCase(variables.fieldlist,'VOLTABLE1.','','all')>
<cfset variables.fieldlist = ReplaceNoCase(variables.fieldlist,'VOLTABLE2.','','all')>

<!--- This is where I think the error is happening, I've read the JQuery Datatable documentation and I still am unable to resolve the issue.--->
<cfsavecontent variable="variables.sOutput"><cfoutput>{
    "draw": #form.draw#,
    "recordsTotal": #qGetCount.fullCount#,
    "recordsFiltered": #rResult.recordcount#,
    "data": [ 
    <cfloop query="rResult" startrow="#form.iDisplayStart+1#" endrow="#form.iDisplayStart+form.iDisplayLength#"><cfset variables.count=variables.count+1>
[
<cfloop list="#variables.fieldlist#" index="variables.i">
<!--- custom translations --->
<cfset outputResults = ReplaceNoCase(rResult[variables.i][rResult.currentRow],'"','', 'ALL' ) />
"#outputResults#"
<cfif variables.i is not listLast(variables.fieldlist)>, </cfif>
</cfloop>
]

<cfif rResult.recordcount LT form.iDisplayStart+form.iDisplayLength>
    <cfif variables.count is not rResult.recordcount AND rResult.recordcount NEQ rResult.currentRow>,</cfif>
<cfelse>
    <cfif variables.count LT form.iDisplayLength>,</cfif>
</cfif>

</cfloop>
            ]
}</cfoutput></cfsavecontent>
</cfprocessingdirective>
</cfsilent>
<cfoutput>#variables.sOutput#</cfoutput>

数据在页面上显示正常,但是当我尝试使用数据表搜索功能时,调试时出现以下错误。 在 FORM 中未定义 SECHO 元素

我认为可能有问题。我认为这与我的新查询包含两个表这一事实有关,但我不太确定如何解决它。

【问题讨论】:

  • 我在您的代码中没有看到对“secho”的引用。您能否搜索术语“secho”并发布该部分代码。当 Coldfusion 在范围内没有适当的元素时,此错误似乎很常见。
  • 抱歉,我忘记添加部分代码了。
  • 首先,使用旧的逗号语法查询 JOIN 只是自找麻烦。请改用实际的 ANSI JOINs。其次,这个查询看起来仍然非常容易受到注入。现在你的错误:我的猜测是它与使用两个表无关,而是SECHO 没有通过你的表单。要进行故障排除,请转储生成并设置为 cfquery 的查询。手动运行它。这可能会给你一个更完整的错误,或者至少让你看到循环构建了什么样的查询。
  • 如果您在WHERE (A.ARCHIVE IS NULL) 的动态部分之前添加条件,则可以摆脱1=11=1 只是确保您将有一个有效的查询>> ....WHERE something &lt;dynamicWhere&gt;.
  • 这看起来有点像 XY 问题http://xyproblem.info。您到底想使用数据表做什么?为什么要动态构建带有表单变量的WHERE 语句?该查询会改变吗?

标签: javascript jquery coldfusion datatables


【解决方案1】:

我知道是什么导致了您的错误。再次阅读您的帖子时,它跳了出来。

原始工作代码

"fnServerData": function ( sSource, aoData, fnCallback ) {
    aoData.push(
        { "name": "table", "value": "ukLocationCodes" },
        { "name": "sql", "value": "SELECT [id], [varCode], [varLocation]" }
        );

        $.ajax( {"dataType": 'json',
             "type": "POST",
             "url": sSource,
             "data": aoData,
             "success": fnCallback} );

 

更改后的代码,在您输入搜索之前可以正常使用

为了查找多个表格并隐藏表格并选择,我将JS脚本更改为以下。

$.ajax( {"dataType": 'json',
             "type": "POST",
             "url": sSource,
             "data": aoData,
             "success": fnCallback} );

当您从旧数据表功能更改代码时,问题首先出现。发布您的 ajax 请求时,旧版数据表 ajax 会自动生成并传递 sEcho 参数。它还期望服务器响应在成功回调中回显该值。

那么当您将 ajax 更改为使用自定义 ajax 调用时发生了什么,这绕过了任何数据表内置功能(即不发送 sEcho)。

要纠正即时错误,您有多种选择。要正确更正此问题,您可能不应该使用选项 3。

  1. 将您的代码恢复为使用旧代码和legacy parameters。这意味着您的 ajax 必须像您最初使用的那样使用 bProcessingbServerSidesAjaxSourcefnServerData

  2. 使用 upgrade guide 并将旧数据表转换为 v1.10。

  3. 这是最简单的,但不推荐。由于您的自定义 ajax 不会发布 sEcho,因此请在您的 cfm 文件中注释掉对 form.sEcho 的引用。这将消除直接错误,但我不知道它会产生什么副作用。

【讨论】:

  • 所以看起来我正在运行 1.9+ 我能够使用处理”:true,“serverSide”:true,“bStateSave”:true,没有错误,但搜索仍然失败“ sEcho": #form.sEcho#。我不确定用什么替换它?
  • 您的评论 “看起来我正在运行 1.9”processing": true"serverSide": true 不一致,因为这些参数在 1.9 中不存在。我 99.9% 确定您正在运行 v1.10+ 并将 1.9 与 1.10 参数混合。所以你需要选择 ALL 1.9 或 ALL 1.10 否则你会继续出错。
  • 如果您决定要恢复/保留旧版,这里有一篇文章可以帮助您将更新的 ajax 调用替换为旧版。 stackoverflow.com/questions/22105762/how-to-use-fnserverdata
  • 请原谅我的无知,我只是想弄清楚为什么它工作得很好,直到我尝试在查询中添加多个表。如果“sEcho”是错误,那似乎是错误发生的地方,并且仅在查询中使用两个表后才发生。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-07-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-03-13
  • 2014-09-06
  • 2014-04-01
相关资源
最近更新 更多