search data to return result by button click value

search data to return result by button click value

tylrwbtylrwb Posts: 1Questions: 1Answers: 0

What i am looking for is a way to select a button that has a value and that be what search looks for. Right now I can put the search value to what I need it to be and it will work, but I have to manually put it in the search box. I do not need it to show in the search box just present the result.

Here is my controller that works if I put it manually in the search

        public ActionResult  GetGrid()
        {
            // Server Side Parameters
            int start = Convert.ToInt32(Request["start"]);
            int length = Convert.ToInt32(Request["length"]);
            string searchValue = Request["search[value]"];
            //string search = id;
            string sortColumnName = Request["column[" + Request["order[0][column]"] + "][name]"];
            string sortDirection = Request["order[0][dir]"];

            List<PartSearchView> prodList = new List<PartSearchView>();
            using (GeneralEntities db = new GeneralEntities())
            {
                prodList = db.PartSearchView.ToList();
                int totalRows = prodList.Count;
                if (!string.IsNullOrEmpty(searchValue))
                {
                    if (searchValue == "ELEC")
                    {
                        prodList = prodList.Where(x => x.Status == true).Where(x => x.VendorId != null).Where(x => x.PartNumber.StartsWith("E")).ToList();
                    }
                    else
                    {
                        prodList = prodList.Where(x => x.PartNumber != null && x.PartNumber.ToLower().Contains(searchValue.ToLower())).ToList();
                    }
                                 
                }

                //if (!string.IsNullOrEmpty(search))
                //{
                //    if (search == "ELEC")
                //    {
                //        prodList = prodList.Where(x => x.Status == true).Where(x => x.VendorId != null).Where(x => x.PartNumber.StartsWith("E")).ToList();
                //    }
                //}


                int totalRowsAfterFilteing = prodList.Count;
                // Sorting
                prodList = prodList.OrderBy(sortColumnName + " " + sortDirection).ToList<PartSearchView>();

                prodList = prodList.Skip(start).Take(length).ToList();

                return Json(new { data = prodList, draw = Request["draw"], recordsTotal = totalRows, recordsFiltered = totalRowsAfterFilteing }, JsonRequestBehavior.AllowGet);
            }

        }

Now I need to select a button on the View and have it put the value of that as a search. What I have throws an error

    var ControlerNameParts = "@Url.Content("~/Parts")";
    $(document).ready(function () {
        //debugger;       
        var table = 
            $('#btnElec').click(function () {
                table.search("search value").draw();
            });
        table = $("#PartSearchTable").dataTable({
            "serverSide": true,
            "ajax": {
                "url": "/Parts/GetGrid/",
                "type": "GET",
                "datatype": "JSON",
            },

            @*"sAjaxSource":  "@Url.Content("~/Parts/GetGrid")",*@
            "responsive": true,
            "bRetrieve": true,
            "bProcessing": true,
            "deferRender": true,
            "dom": 'lBfrtip',
            "order": [0, "PartNumber"],
            "language": {
                "processing": "processing...Please wait"
            },
            "buttons": [
                { extend: 'copyHtml5', exportOptions: { columns: ':visible' } }
                , { extend: 'excelHtml5', exportOptions: { columns: ':visible' } }
                , { extend: 'csvHtml5', exportOptions: { columns: ':visible' } }
                , { extend: 'pdfHtml5', exportOptions: { columns: ':visible' } }
                , { extend: 'print', exportOptions: { columns: ':visible' } }
                , 'colvis'
            ],
            columnDefs: [{ orderable: false, targets: [1, 2, 3, 4, 5] }],

            "aoColumns": [
                { "data": "PartNumber", "name": "PartNumber" },
                {
                    "data": "PartImage", "aTargets": [0],
                    "render": function (data) {
                        if (data === null) return '<img src=Content/Images/Parts/NoImage.png style="width:50px;" />';
                        return '<img src=Content/Images/Parts/' + data + ' class="zoom" style="width:50px;" />';
                    }
                },
                { "data": "Description", "name": "Description" },
                {
                    "data": "HasUpgrade", "aTargets": [0],
                    "render": function (data) {
                        if (data === true) {
                            return '<b style="color:green;">Yes</b>';
                        } else {
                            return '<b style="color:red;">No</b>';
                        }
                    }
                },
                {
                    "data": "IsUpgrade", "aTargets": [0],
                    "render": function (data) {
                        if (data === true) {
                            return '<b style="color:green;">Yes</b>';
                        } else {
                            return '<b style="color:red;">No</b>';
                        }
                    }
                },
                {
                    "mRender": function (oObj, type, full) {
                        var button = '<div>'
                        button += '<div class="btn-group">';
                        button += '<a class="btn btn-warning btn-sm" href="' + ControlerNameParts + "/Details/" + full.Material + '" data-ajax-update="#SkEdit" data-ajax-success="openModalDialog(\'SkEdit\', \'Edit\')" data-ajax-mode="replace" data-ajax-method="GET" data-ajax-failure="clearModalDialog(\'SkEdit\');alert(\'Ajax call failed\')" data-ajax-begin="prepareModalDialog(\'SkEdit\')" data-ajax="true">Details</a>&nbsp;';
                        button += '</div></div>';
                        return button;
                    }
                },

            ],


        });
       
    });

I tried to add it at the end of the .dataTable, but it gives the same error.

this is the button i am using

<button class="btn btn-info btn-sm" type="button" id="btnElec" value="ELEC">Electrical</button>

Answers

  • colincolin Posts: 15,112Questions: 1Answers: 2,583

    You can call search() rather than manually putting the string into the search element - that method also puts the string there too!

    If that doesn't help, we're happy to take a look, but as per the forum rules, please link to a test case - a test case that replicates the issue will ensure you'll get a quick and accurate response. Information on how to create a test case (if you aren't able to link to the page you are working on) is available here.

    Cheers,

    Colin

Sign In or Register to comment.