Search code examples
javascriptphpjqueryyiiyii-components

Change the url and reload on change of dropdown value which is in common header


I am new to yii and I want to change url and reload the current view page on change of a dropdown value which is in the admin's common header field. Dropdown is

echo $form->dropDownList($siteid, 'selectedsiteid', $data,$htmlOptions, array('class' => "form-control"));

The current urls may be like the following

1. http://localhost/zyntegra_commoditylifev2/admin/sitetag/viewTags/sid/6
2. http://localhost/zyntegra_commoditylifev2/admin/category/viewCategory/sid/ 3.http://localhost/zyntegra_commoditylifev2/admin/site/index


Suppose the current changed dropdown value is 10 and If the current url is like (1) one then I need to change the url like this

http://localhost/zyntegra_commoditylifev2/admin/sitetag/viewTags/sid/10

If the current url is like (2) one then

http://localhost/zyntegra_commoditylifev2/admin/category/viewCategory/sid/10

and if it's like the (3) one then there is no need of reload.

Plz somebody help me to solve this problem.

Thanks in Advance


Solution

  • Try it like,

    var tagsUrl='http://localhost/zyntegra_commoditylifev2/admin/sitetag/viewTags/sid/';
    var catUrl='http://localhost/zyntegra_commoditylifev2/admin/category/viewCategory/sid/';
    $(function(){
        $('#your-select-id').on('change',function(){
           href=window.location.href;
           if(href.indexOf('viewTags') !== -1){
               window.location.href=tagsUrl+this.value;//append value of select element
           } else if(href.indexOf('viewCategory') !== -1){
               window.location.href=catUrl+this.value;//append value of select element
           }
        });
    });
    

    Above code may help you to complete your task.

    Updated, to get only sid then try it like,

    // use location.href in url to get current url
    var url='http://localhost/zyntegra_commoditylifev2/admin/sitetag/viewTags/sid/110';
    alert(url.split('sid/')[1]);//110 
    

    Updating @salini answer like,

    function reloadpage() {             
        var sid = $( '#Site_selectedsiteid' ).val( );
        var currentURL = window.location.pathname;
        var n=currentURL.indexOf("/sid");// use '/sid' only in indexOf to remove else part
        if(n!=-1) {
            var newurl = currentURL.substring(0,n); 
            window.location.href = newurl + '/sid/' + sid;
        }
    }