I am using datatables ajax request and i cannot fi...
# cfml-general
c
I am using datatables ajax request and i cannot figure out how to parse the form variables coming back like this 1. order[0][column]: 1 2. order[0][dir]: desc 3. order[0][name]: 4. start: 0 5. length: 10 6. search[value]: Fishe 7. search[regex]: false These arent real arrays so not sure how to parse them. Any suggestions?
a
c
OK i have that setup but now the cfc seems to have a bug for the 2023 release The value NAME cannot be converted to a number. The error occurred in C/CFNextAlpha/cfusion/wwwroot/fastrack hipco/cfc/formutils.cfc line 101
Copy code
99 :                         }
100 :                         else {
101 :                             local.currentElement[local.tempElement][local.tempIndex] = trim(canonicalize(arguments.formScope[local.thisField], true, true));
102 :                         }
103 :                     }
just using a simple cfdump triggers it <cfset util = new "fastrack-hipco.cfc.formutils"().init() /> <cfset util.buildFormCollections(form) /> <cfdump var="#form#">
this one works
👍 1
Thanks so much this was perfect!
👍 1
OK, so i thought lol. It only reads the first collection, no errors just not reading the other two collections. I cant beleive datatables send this stuff as default
m
ignore what you normally think of form fields from coldfusion when receiving datatables requests. this is your form field name 'order[0][column]' use array notation to refer to it, then switch the 0 to a variable. technically, they will send one of those per 0 based column in the table, thats what you would loop from 0 to the column count-1. to reference your list item 1, form['order[#yourLoopIndex#][column]'] . You don't have to use all the stuff it sends, it's just that datatables is trying to give your endpoint the info it has configured. If it is stuff only in your own app, like you are frontend, and the ajax. then you can likely ignore any that start with columns, as you wouldn't use the datatables configuration to drive how you configure stuff. for order, if you select multiple columns, you will get numbers above 0, but never to exceed the count of columns.
c
Ah, well i got everything working except the pagination on the second page doesnt return any results when using the search the first page is fine, what a headache 🙂
a
If the names are consistent then you should be able to just grab them, but I don't use datatables so assume they are dynamic?
So
foo = form[ "order[0][column]" ]
should work
c
no i know them and i am now using that so that part is working
👍 1
ajax_Contacts_SearchResults.cfm
a
This doesn't look right
Copy code
<cfset util = new "fastrack-hipco.cfc.formutils"().init() />
<cfset util.buildFormCollections(form) />
Shouldn't that be :
Copy code
<cfset util = new "fastrack-hipco.cfc.formutils"().init() />
<cfset transformedData = util.buildFormCollections(form) />
c
the util writes back to the form scope, but that isnt the part that is failing, and i will be removing that dependancy soon
a
Oh, it mutates the form scope 😕
c
yeah
m
are you echoing back out the draw
it increments that on each ajax call, and matches it to know which content to render.
c
yes
2nd page returns this { "draw": 11, "recordsTotal": 718047, "recordsFiltered": 6378, "orderColumn": "ContactCombinedName", "data": [ ] }
but not the filtered records
<cfoutput> { "draw": #form.draw#, "recordsTotal": #qryTotalRecords.totalRecords#, "recordsFiltered": #filteredRecords#, "orderColumn": "#orderColumn#", "data": [ <cfloop query="qryContactSearch"> { "Type": "#ContactType#", "Contact": "#ContactCombinedName#", "Company": "#CompanyName#", "Email": "#ContactWorkPhone#", "Territory": "#HTerritory#", "Salesperson": "#HSalesperson#", "Action": "#action#" }<cfif qryContactSearch.CurrentRow neq qryContactSearch.RecordCount>#chr(44)#</cfif> <!--- Add a comma after each entry except the last one ---> </cfloop> ] } </cfoutput>
m
it doesn't seem like qryContactSearch has any records, otherwise your data would have objects in it
c
the query being executed
select DISTINCT vwAllAccounts_Contacts.ContactID, vwAllAccounts_Contacts.ContactCombinedName, vwAllAccounts_Contacts.ContactWorkPhone, vwAllAccounts_Contacts.ContactCellPhone, vwAllAccounts_Contacts.ContactEmail, vwAllAccounts_Contacts.CompanyName, vwAllAccounts_Contacts.AccountID, vwAllAccounts_Contacts.MailingCity, vwAllAccounts_Contacts.MailingState, vwAllAccounts_Contacts.ContactTitleDescr, 'A' as ContactType, vwAllAccounts_Contacts.TerritoryID as HTerritory, vwAllAccounts_Contacts.AssignedName AS HSalesperson, ' ' as Action from vwAllAccounts_Contacts WHERE vwAllAccounts_Contacts.Active=1 AND vwAllAccounts_Contacts.ContactActive=1 AND vwAllAccounts_Contacts.ContactCombinedName LIKE ? AND (vwAllAccounts_Contacts.UserID = ? OR vwAllAccounts_Contacts.InUserID = ? OR vwAllAccounts_Contacts.BranchMgrID = ? OR vwAllAccounts_Contacts.RegionMgrID = ? OR vwAllAccounts_Contacts.FiltrationUserID = ? OR vwAllAccounts_Contacts.FiltrationINUserID = ? OR vwAllAccounts_Contacts.FiltrationBranchMgrID = ? OR vwAllAccounts_Contacts.FiltrationOfficeMgrID = ? ) --ORDER BY CompanyName, ContactCombinedName ORDER BY HSalesperson asc OFFSET 0 ROWS FETCH NEXT 10 ROWS ONLY
well i think i hosed my dev server instance lol i was trying to use cflog and i specified a complete path for file by mistake. Now things dont seem to run right, so I will be reinstalling and then back to this thing 😛
m
Hi cfengineers - we are using DataTables in our app. In our app we convert all URL/form variables into a private structure called request.publicVars, and then reference all of the dataTables variables like: request.publicVars["search[value]"]
Notice the lack of pound signs since you are looking for those literal names
And then for getting sorting, in a loop we would call request.publicVars["order[#request.myTmpPageVars.loopIndx#][column]"] and request.publicVars["order[#request.myTmpPageVars.loopIndx#][dir]"] , etc, etc