Javascript/Jquery返回cfquery值为“否"为false和“是"为true [英] Javascript / Jquery returns cfquery value of 'no' as false and 'yes' as true

查看:146
本文介绍了Javascript/Jquery返回cfquery值为“否"为false和“是"为true的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个有趣的问题. 我有一个jquery ajax命令从MySQL表中获取数据(以下代码) 然后,当我建立表的值时,应该为yes的值现在为true,而valse的值为非false.

I have an interesting problem. I Have a jquery ajax command to get data from a MySQL table (code to follow) When i then build my table values that should be yes are now true and values of valse are not false.

这是我的Coldfusion页面上的功能. 没什么,我正在调用cfc方法

Here is the function on my coldfusion page. Nothing fancy, im calling a cfc method

function getitems()
{
  var cusno = document.getElementById("cusno").value;
  console.log("Cus No Is :" + cusno);
  $.ajax({
          type: "POST",
          url: "<cfoutput>#dir1st#</cfoutput>functions/ajax/invoices.cfc?method=getRegularInvoice",
          data: { 'cusno': cusno  },
          success: function(data){
            console.log("Data Is :" + data);
            $('#thetable tr').not(':first').not(':last').remove();
            var html = '';
            //console.log(data);
            var opts = $.parseJSON(data);
            $.each(opts, function(i, d) {
              //console.log("onceoff=" + d.onceoff);
              html += '<tr>'
                + '<td><a onclick="edititem('+ d.refid +')"><i class="text-primary fas fa-edit"></i></a>' + d.code + '</td>'
                + '<td>' + d.description + '</td>'
                + '<td>' + d.qty + '</td>'
                + '<td>' + d.price + '</td>'
                + '<td>' + d.total + '</td>'
                + '<td>' + d.dep + '</td>'
                + '<td>' + d.onceoff + '</td>'
                + '<td>' + d.regday + '/' + d.regmonth + '</td>'
                + '<td>' + d.payment_type + '</td>'
                + '<td>' + '<a onclick="if (!confirm(\'Are you sure?\')) return false;"  href="invoice_regular_delitem.cfm?cusno=<cfoutput>#URL.cusno#</cfoutput>&refid='+ d.refid +'"><i class="text-danger fas fa-trash"></i></a>' + '</td>'
                + '</tr>';

            })
            //console.log("html="+html);

            $('#thetable tr').first().after(html);

          }
        })
};

显示该表,并且我的所有列均正确,但是,onesoff列应返回是"或否"值,但返回True或false. 表格中是varchar列,因此不能如此,请参见cfdump下面的内容,以便查看结果

The table displays and all my columns are correct, however, onceoff column should return a Yes or No value, however, it returns True or false. In the table it is a varchar column so can't be that, see below cfdump so you can see the result

这是我的cfc代码,结果是上面的

Here is my cfc code that results in the above

  <cffunction name="getRegularInvoice" access="remote" output="false" returnformat="json">
  <cfargument name="cusno" type="any" required="true">

    <cfquery name="getitems" datasource="#datasrc#">
        SELECT *
        FROM reg_invoice_items
        WHERE cusno = '#arguments.cusno#'
        ORDER BY code asc
    </cfquery>

    <cfset returnArray = arrayNew(1)>

      <cfloop query="getitems">

          <cfset data = structNew() />
          <cfset data['refid'] = #getitems.refid# />
          <cfset data['code'] = #getitems.code# />
          <cfset data['description'] = #getitems.description# />
          <cfset data['qty'] = #getitems.qty# />
          <cfset data['price'] = #getitems.price# />
          <cfset data['total'] = #getitems.total# />
          <cfset data['originalprice'] = #getitems.originalprice# />
          <cfset data['disc'] = #getitems.disc# />
          <cfset data['exvat'] = #getitems.exvat# />
          <cfset data['location'] = #getitems.location# />
          <cfset data['costnon'] = #getitems.costnon# />
          <cfset data['ajustnon'] = #getitems.ajustnon# />
          <cfset data['regmonth'] = #getitems.regmonth# />
          <cfset data['regday'] = #getitems.regday# />
          <cfset data['onceoff'] = "#getitems.once_off#" />
          <cfset data['dep'] = #getitems.dep# />
          <cfset data['payment_type'] = #getitems.payment_type# />
          <cfset data['currency'] = #getitems.currency# />


          <cfset arrayAppend(returnArray,data) />
      </cfloop>
      <cfreturn returnArray />

最后这是表的输出,ondoff应该说否"

And finally here is the output of the table, onceoff should say "No"

推荐答案

您没有提到要使用哪个CF版本,但是... ColdFusion 2016+中还有一个查询选项.使用新的查询序列化设置您的Application.cfc中的yes/no值将保留为字符串,并将查询自动转换为jQuery兼容的结构数组.显然,它们是应用程序级别的设置,它们将应用于应用程序内的所有所有查询.

You didn't mention which version of CF you're using, but ... there's one other option for queries in ColdFusion 2016+. Using the new query serialization settings in your Application.cfc will preserve the yes/no values as strings AND convert the query into a jQuery compatible array of structures - automatically. Obviously being application level settings, they'll apply to all queries within the application.

  • serialization.serializeQueryAs ="struct" -自动将应用程序中的 all 查询序列化为结构数组(而不是CF默认使用的怪异格式) ).

  • serialization.serializeQueryAs = "struct" - Automatically serialize all queries in the application as an array of structures (instead of the weird format CF uses by default).

serialization.preserveCaseForQueryColumn = true -保留查询列名称的大小写(而不是将所有内容都转换为大写).

serialization.preserveCaseForQueryColumn = true - Preserve the case of query column names (instead of converting everything to upper case).

Application.cfc

component {
    this.name = "Your_Application_Name_Here";
    this.serialization.serializeQueryAs = "struct";
    this.serialization.preserveCaseForQueryColumn = true;
}

DemoComponent.cfc

<cfcomponent>
    <cffunction name="getData" access="remote" returntype="query">

    <!--- DEMO query. Note, manual queries MUST include column data types --->
    <cfset local.getItems = QueryNew("onceoff,exvat"
                              , "varchar,varchar"
                              , [{exvat : "yes",onceoff : "no"}]
                           )>

        <cfreturn local.getItems>
    </cffunction>   
</cfcomponent>

结果

[{"onceoff":"no","exvat":"yes"}]

这篇关于Javascript/Jquery返回cfquery值为“否"为false和“是"为true的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆