Click here to Skip to main content
15,891,316 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi
sorry to bother you again .

Well initially i binded the data to jquery datatable from DB where i get it .

Now i will demonstrate my required thing with example .

Initially i have a dropdownlist with 3 list items Countries , states,cities

Initially on load countries will be selected and all data will be displayed TILL THIS POINT I HAVE DONE IT .

Now when user changes his selection to STATES OR CITIES the only respective Data should BIND TO Datattable not all data ?

I have no clue how to achieve it but my lazy brain is thinking to create a new Datatable which is awful even to imagine .

My piece of code :

JavaScript
$(document).ready(function () {

    $("#btnGuru").click(function () {
        tableToExcel('myDataTable', 'W3C Example Table');
    });

    $.datepicker.regional[""].dateFormat = 'dd/mm/yy';
    $.datepicker.setDefaults($.datepicker.regional['']);
    debugger;
    var dt = $('#myDataTable').dataTable({

        "bProcessing": true,
        "bServerSide": true,
        "sAjaxSource": "Home/AjaxHandler",  //Here this gets the complte list . Should i apply some filter on it ? if so how ? AJAXHandler is a action method which gives me complete list
        "bJQueryUI": true,
        "aoColumns": [



My controller action method code :

JavaScript
public ActionResult AjaxHandler(JQueryDataTableParamModel param)
      {
          var Lead_FullDetails = ser_obj1.Lead_List();
          IEnumerable<LeadsInfoDummy> filteredLeads = Lead_FullDetails;

          //Check whether the companies should be filtered by keyword
          if (!string.IsNullOrEmpty(param.sSearch))
          {
              //Used if particulare columns are filtered
              var ContactNameFilter = Convert.ToString(Request["sSearch_1"]);
              var ContactAddressFilter = Convert.ToString(Request["sSearch_2"]);
              var SourceFilter = Convert.ToString(Request["sSearch_3"]);
              var DomainFilter = Convert.ToString(Request["sSearch_4"]);


              //Optionally check whether the columns are searchable at all

              var isContactNameSearchable = Convert.ToBoolean(Request["bSearchable_1"]);
              var isContactAddressSearchable = Convert.ToBoolean(Request["bSearchable_2"]);
              var isSourceSearchable = Convert.ToBoolean(Request["bSearchable_3"]);
              var isDomainSearchable = Convert.ToBoolean(Request["bSearchable_4"]);


              //filtering rows based on user input

              filteredLeads = Lead_FullDetails.Where(c => isContactNameSearchable && c.Contact_Name.ToLower().Contains(param.sSearch.ToLower())
                           ||
                           isContactAddressSearchable && c.Contact_Address.ToLower().Contains(param.sSearch.ToLower())
                           ||
                           isSourceSearchable && c.Lead_Source.ToLower().Contains(param.sSearch.ToLower())
                           ||
                           isDomainSearchable && c.Domain.ToLower().Contains(param.sSearch.ToLower()));

          }
          else
          {
              filteredLeads = Lead_FullDetails;
          }

          var isContactNameSortable = Convert.ToBoolean(Request["bSortable_1"]);
          var isContactAddressSortable = Convert.ToBoolean(Request["bSortable_2"]);
          var isSourceSortable = Convert.ToBoolean(Request["bSortable_3"]);
          var isDomainSortable = Convert.ToBoolean(Request["bSortable_4"]);
          var sortColumnIndex = Convert.ToInt32(Request["iSortCol_0"]);

          Func<LeadSortModel, string> orderingFunction = (c => sortColumnIndex == 1 && isContactNameSortable ? c.Contact_Name :
                                                        sortColumnIndex == 2 && isContactAddressSortable ? c.Contact_Address :
                                                        sortColumnIndex == 3 && isSourceSortable ? c.Lead_Source :
                                                        sortColumnIndex == 4 && isDomainSortable ? c.Domain :
                                                        "");

          //var sortDirection = Request["sSortDir_0"]; // asc or desc
          //if (sortDirection == "asc")
          //{
          //    filteredLeads = Lead_FullDetails.OrderBy(orderingFunction); // I am unable to do this also due to some error .

          //}
          //else
          //{

          //}

          var DisplayedLeads = filteredLeads.Skip(param.iDisplayStart).Take(param.iDisplayLength);
          var result = from c in DisplayedLeads select new[] { Convert.ToString(c.Lead_Id), c.Contact_Name, c.Contact_Address, c.Lead_Source, c.Domain };
          return Json(new
          {
              sEcho = param.sEcho,
              iTotalRecords = Lead_FullDetails.Count(),
              iTotalDisplayRecords = filteredLeads.Count(),
              aaData = result
          },
                      JsonRequestBehavior.AllowGet);
      }

So how can we achieve this

My code is already posted in these links :
Data table Filter query ? Datapicker

fnRender giving issue in datatable ? mvc4



Regards
Posted
Updated 15-Apr-14 19:30pm
v3

When you select a Country, you need to get all the related States by querying the Database and bind one DataTable. Then Bind the DataTable to the State DropDownList.

The same procedure is followed when you select a State to populate the Cities.
 
Share this answer
 
Comments
sunil gutta 16-Apr-14 1:27am    
yes kudos . i too thinking the same . once check the code above i edited my post ..
Good. Please accept the answer. :)
sunil gutta 16-Apr-14 14:23pm    
yes i will for sure but the above posted code works one ways ONLOAD . i dont know how to link dropdown & datatable .. once see the code i commented some lines
I don't understand the exact problem in your code. But to bind the DropDownList with a DataTable, you should do like...

ddlYourDripDown.DataSource = datatableForDropDown;
ddlYourDripDown.DataBind();
sunil gutta 16-Apr-14 16:02pm    
ok but i am making an ajax call to controller for fetching data and loading it right . what i need is when something changes in dropdown how can i make a ajax call to controller for filtered data ?
Through "sAjaxSource": "Home/AjaxHandler" i need to process my drop-down selection through it just becoz if i make a separate call i need to create separate action method and separate data-table binding again .. so i am confused . sorry for trouble .
JavaScript
"fnServerData": function (sSource, aoData, fnCallback) {

           //On change in Drop-Down selection
           $('#DropDown_Select').change(function () {
               status = $(this).val()
               $.ajax({
                   "type": "GET",
                   "dataType": 'json',
                   "contentType": "application/json; charset=utf-8",
                   "url": sSource + "/" + status, //sending server side status and filtering table
                   "data": aoData,
                   "success": function (data) {

                       fnCallback(data);
                   }
               });

           });


Regards
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900