Hi, I have the datatables Successfully Implemented with my Coldfusion code. But there is a Problem, I am unable to use the sql server fetch offset with it,
right now my query returns around 5000000 records without using offset which is usually taking a lot off time , i am trying to show 10 at a time so query should not time out and it should on click of next page start with the offset and show 10 rows only
This is my code
Jquery Code
$(document).ready(function() {
$('#ButtonSubmit').on('click', function (e) {
e.preventDefault();
$.ajax({
url: "getheaders.cfm",
cache: false,
data : $('#form').serialize(),
method: "post",
success: function(response){
$('.mytable').html(response);
}
}).done(function(data) {
$('.table').show();
$(".table").DataTable({
"bFilter": true,
"serverSide": true,
"columns": [
{ "data": "name", "title": "Name", "autoWidth": true },
{ "data": "fname", "title":"First Name", "autoWidth": true }
],
"ajax": {
"url" : "datatables.cfm",
"type" : 'post'
},
"language": {
"processing": "Loading..."
}
});
})
});
});
now the coldfusion code
<cffunction name="records" access="remote" returnformat="json" returntype="any" output="false">
<cfparam name="draw" default="1" type="integer" />
<cfparam name="start" default="0" type="integer" />
<cfparam name="length" default="10" type="integer" />
<cfparam name="search" default="" type="string" />
<cfset var aData = arrayNew(1) />
<cfset var sGridData = StructNew() />
<cfif len(form["search[value]"]) gt 0>
<cfset search=form["search[value]"]>
</cfif>
<cfset var rsQuery = ''>
<cfset var listColumns = 'name,firstname'>
<cfquery name="rsQueryData" datasource="#Application.dsLocal#">
;WITH cte AS(
SELECT ROW_NUMBER() OVER (PARTITION BY id ORDER BY DateCreated DESC) AS rn,
name,firstname
FROM table1
WHERE 1=1
<cfif len(trim(search))>
AND
(
<cfloop list="#listColumns#" index="thisColumn">
<cfif thisColumn neq listFirst(listColumns)>
OR
</cfif>
#thisColumn# LIKE <cfqueryparam cfsqltype="CF_SQL_VARCHAR" value="%#trim(search)#%" />
</cfloop>
)
</cfif>
order by 2 desc
OFFSET val(start+1) ROWS FETCH NEXT #length# ROWS ONLY;
UNION
SELECT ROW_NUMBER() OVER(PARTITION BY id ORDER BY datecreated desc ) AS rn,
name,firstname
FROM table2
WHERE 1=1
<cfif len(trim(search))>
AND
(
<cfloop list="#listColumns#" index="thisColumn">
<cfif thisColumn neq listFirst(listColumns)>
OR
</cfif>
#thisColumn# LIKE <cfqueryparam cfsqltype="CF_SQL_VARCHAR" value="%#trim(search)#%" />
</cfloop>
)
</cfif>
order by 2 desc
OFFSET val(start+1) ROWS FETCH NEXT #length# ROWS ONLY;
)
SELECT *
FROM cte WHERE rn = 1
</cfquery>
<!--- Loop our results, implementing our filtering and pagination and putting into an array to be returned --->
<cfoutput query="rsQueryData" startrow="#val(start+1)#" maxrows="#val(length)#">
<cfset sGridData = structNew() />
<cfset sGridData['name'] = rsQueryData['name'][currentRow] />
<cfset sGridData['firstname'] = rsQueryData['FirstName'][currentRow]/>
<cfset ArrayAppend(aData, sGridData) />
</cfoutput>
<cfset sGridReturn["draw"] = val(draw) />
<cfset sGridReturn["recordsTotal"] = 10000 />
<cfset sGridReturn["recordsFiltered"] = rsQueryData.RecordCount />
<cfset sGridReturn["data"] = aData />
<cfreturn sGridReturn />
</cffunction>
as i am using the order by clause and the offset it gives me just 17 records while i have so many records, what i am missing here
i want the query should give calculate all the records and give me the results paginated way like 10 at a time so my query should not timeout and smoothe it should go
because after this next task is to get full data in excel