0

I'm working on CodeIgniter project,. In my web-app I'm filtering my data-tables processing on server using drop-down lists with selecting multiple values from drop-down list box. I'm getting a proper output.

My view code is:

<div class="content-wrapper">
<div class="box-body">
    <form name="listproperty" action="" method="post" role="form" id="form-filter">
        <div class="row">
            <div class="col-md-3">
                <label>Area: </label>
                <select class="form-control", name="area_name" id="area_name">
                    <option value="0">Select Area</option>
                    <?php 
                        $area_id; 
                        foreach ($areaList as $area) { ?>
                            <option value="<?= $area->area_id_primary; ?>" <?php if ($area->area_id_primary == $area_id) { echo "selected='selected'"; } ?>><?= $area->area_name; ?></option>
                        <?php } ?>
                </select>
            </div>
            <div class="col-md-3">
                <label>City: </label>
                <select class="form-control" name="city_name" id="city_name" multiple="">
                    <option value="">Select City</option>
                    <?php $ct = explode(' ', $city); 
                        foreach ($propertyCityList as $propertyCity) { 
                            $selected = false;
                            foreach ($ct as $usercity) { 
                                if ($usercity == $propertyCity->city_id_primary) {
                                    $selected = true;
                                    break;
                                }
                            } ?>
                            <option value="<?= $propertyCity->city_id_primary; ?>" <?php if($selected === true) { echo "selected='selected'"; } ?>><?= $propertyCity->city_name; ?></option>
                    <?php } ?>
                </select>
            </div>
        </div>
        <div class="row">&nbsp;</div>
        <div class="row">
            <div class="pull-right">
                <div class="col-md-12">
                    <button type="button" id="btn-filter" class="btn btn-success" style="margin-right: 25px;">Filter</button>
                    <button type="button" id="btn-reset" class="btn btn-default">Reset</button>
                </div>
            </div>
        </div>  
    </form>
</div><hr>
<div class="box-body">
    <table class="table table-striped table-bordered" id="listpropertyData" cellspacing="0" width="100%">
        <thead>
            <tr>
                <th width="10%">Added Date</th>
                <th width="10%">ASYS No.</th>
                <th width="18%">Address</th>
                <th width="10%">City</th>
                <th width="15%" style="text-align: center;">Action</th>
            </tr>
        </thead>
        <tfoot>
            <tr>
                <th>Added Date</th>
                <th>ASYS No.</th>
                <th>Address</th>
                <th>City</th>
                <th style="text-align: center;">Action</th>
            </tr>
        </tfoot>
    </table>
</div>

<script type="text/javascript">
    var property;
    $(document).ready(function() {

        $("#area_name").select2();
        $("#city_name").select2();

        property = $('#listpropertyData').DataTable({
            "processing": true,
            "serverSide": true,
            "ajax": {
                "url": "<?= base_url('Controller/getPropertyDatatable'); ?>",
                "type": "POST",
                "dataType": "JSON",
                "data": function ( data ) {
                    data.area_name = $('#area_name').val();
                    data.city_name = $('#city_name').val();
                }
            },
            "dom": '<"top"fli>rt<"bottom"p><"clear">',
            "lengthMenu": [[25, 50, 100, 200, -1], [25, 50, 100, 200, "All"]],
            "columnDefs": [
                {
                    "targets":[7],
                    "orderable":false
                },
            ],
        });

        $('.dataTables_filter input[type="search"]').css(
            {'width':'450px','display':'inline-block'}
        );

        $('#btn-filter').click(function(){ //button filter event click
            property.ajax.reload();  //just reload table
        });
        $('#btn-reset').click(function(){ //button reset event click
            $('#form-filter')[0].reset();
            property.ajax.reload(); 
            window.location.reload(); //just reload table
        });
    });
</script>

My controller code:

public function listProperty()
{
    # code...
    $data['areaList'] = $this->property_model->areaList();
    $data['propertyCityList'] = $this->property_model->propertyCityList();
    $this->global['pageTitle'] = 'RoxAI-ePro : Property List';
    $this->loadViews("property/listProperty", $this->global, $data, NULL);
}
public function getPropertyDatatable()
{
    # code...
    $listProperty = $this->property_model->getPropertyDatatable();

    $data = array();
    foreach ($listProperty as $property) {
        # code...
        $added_on=$property->property_added_on;
        $added_on_date = str_replace('/', '-', $added_on);
        $property_added_on_date=date('d/m/Y', strtotime($added_on_date));

        $row = array();
        $row[] = $property_added_on_date;
        $row[] = $property->property_asys_number;
        $row[] = $property->property_address;
        $row[] = $property->city_name;
        $row[] = 'Action';

        $data[] = $row;
    }
    $output = array(
        'draw' => $_POST['draw'],
        'recordsTotal' => $this->property_model->countPropertyAll(),
        'recordsFiltered' => $this->property_model->countPropertyFiltered(),
        'data' => $data,
    );
    echo json_encode($output);
}

My question is how I can pass that drop-down list values in URL so I can get same result directly if I'm trying to copy-paste URL and open it in another browser with same selection of values in drop-down?

Ganesh Aher
  • 1,118
  • 3
  • 21
  • 51
  • you want the actual url to change whenever the dropdown list is changed e.g. somepage?dropdown=stmt – Alex Oct 24 '18 at 05:52
  • @Alex Yes, When I'm select any value from or any value coming from database into drop-down that value will shows in URL. – Ganesh Aher Oct 24 '18 at 05:55
  • @Alex In short I want to append values of drop down in URL on ajax request. – Ganesh Aher Oct 24 '18 at 05:56
  • https://stackoverflow.com/questions/3338642/updating-address-bar-with-new-url-without-hash-or-reloading-the-page – Alex Oct 24 '18 at 06:00
  • https://stackoverflow.com/questions/824349/modify-the-url-without-reloading-the-page – Alex Oct 24 '18 at 06:00
  • Personally I would use the query string, with `pushState()` as @alex mentioned and I would change how I build the array in ajax, to pull the current browser URL, then your pages will be bookmarkable (save searches). when someone loads the page check the url for the query string and make an ajax call, (make a call when you change the url). Right now your URL and field values can become "unstuck" or out of sync. Because you use POST, but want to save search data in the URL (GET). – ArtisticPhoenix Oct 24 '18 at 06:14
  • I built a whole system using `jQgrid` `CI2`, and `AJAX` and if someone searches and you want to link it to a page with a grid (table), you just redirect them to a modified url with the data from what they clicked. For example 2 related tables could be linked by a simple click to pass an ID through the URL. Then the rest of the automated stuff (that pulls data from the URL and submits a search) triggers like normal. It works really nice (but I have about 20 related tables, lol). – ArtisticPhoenix Oct 24 '18 at 06:18
  • @ArtisticPhoenix don't recommend me haha I totally almost steered OP down the wrong path. didn't know until a few minutes ago that changing the URL after page load was even possible. still think it is a bit odd giving that much power to nefarious people to change the browser. if I understand correctly after page load `somebadsite.com` can change url to `yourbank.com`? or can you only change hash? – Alex Oct 24 '18 at 06:19
  • 1
    @Alex - you can change the whole url, but if you push reload it goes to that location (hence the suggestion to modify only the query string). It doesn't change your location (unless you push reload in the browser), basically you can use it to store search data. So they hit back, it preforms the previous search etc... I can't say that I ever really tried changing the domain though, but I imagine it would work tell they interact with the browser buttons. ... lol – ArtisticPhoenix Oct 24 '18 at 06:20
  • `pushState` adds to the history, `replaceState` replaces the item in the history (cant go back to it after its replaced). `popState` triggers on page load (generally, but I had some issues with that one, I try to avoid it). If I remember its for navigating the history where a page load is not done, too. It's been minute sense I used it though. – ArtisticPhoenix Oct 24 '18 at 06:25

0 Answers0