开发者

datatables checkbox to toggle WHERE clause

开发者 https://www.devze.com 2023-04-12 06:28 出处:网络
Ok so I am currently populating my datatable just fine with coldfusion and everything sorts perfectly. One of the columns in my SQL is the status. Currently I am returning all results WHERE status !=

Ok so I am currently populating my datatable just fine with coldfusion and everything sorts perfectly. One of the columns in my SQL is the status. Currently I am returning all results WHERE status != 'Completed'

This has worked fine for now, but what I would really like to do is place a checkbox on the page just above the datatable and when checked it would show all records including those that are completed.

Question is, can I do some kind of callback on the check box to dynamically omit the where clause, or do I deliver ALL records to datatables and filter 开发者_开发技巧it there? My only worry with that is when the database gets large, returning all records may take a while and 90% of the time they will be filtered out.

Either way, code snippets would be very helpful!

Thanks



RESOLVED

iKnowKungFoo was right. Since I am using pagination in datatables, the query is limited to those rows anyway, eliminating the worry for the large return result. I implemented the following code to put a jqueryui toggle button.

js

$('#completed_button').bind('change', function(){
   if($(this).is(':checked')){
      $(this).button('option', 'label', 'Hide Completed');
      oTable.fnFilter('Completed',6,false);
   } else {
      $(this).button('option', 'label', 'Show Completed');
      oTable.fnFilter('',6,false);
   }
});

html

<input class="toggle-button" id="completed_button" type="checkbox"  /><label for="completed_button">Show Completed</label>

SQL

WHERE 1 =1 
<cfif trim(url.sSearch_6) NEQ "Completed">
   AND TS.tickets_status_id != <cfqueryparam cfsqltype="cf_sql_integer" value="13" />
</cfif>


I'd say the approach depends on the amount of data you're anticipating. If the checkbox will only filter or include a small number of records, include them in the data return and filter them on the client side. If that checkbox will have a large impact on the result set, re-firing the data call when the checkbox is changed and filtering on the server is probably a better approach.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号