1

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

Hasib Tarafder
  • 5,773
  • 3
  • 30
  • 44
Salini L
  • 829
  • 5
  • 15
  • 43

2 Answers2

1

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;
    }
}
Rohan Kumar
  • 40,431
  • 11
  • 76
  • 106
  • but there is more urls like this i just mentioned only some of these – Salini L Feb 06 '14 at 07:18
  • @Salini did you try above solution? what is the problem you are facing? – Kumar V Feb 06 '14 at 07:36
  • @kumar there are more like urls http://localhost/zyntegra_commoditylifev2/admin/sitetag/tags/sid ect ect – Salini L Feb 06 '14 at 07:41
  • @Salini but, urls are same but parameter only different. Right? – Kumar V Feb 06 '14 at 07:43
  • 1
    I need if the last part of the url is like /sid/ or /sid/2(some value) then need to change it to /sid/10(current dropdown value) – Salini L Feb 06 '14 at 07:44
  • @Salini ok then did you try above solution? Rohan solution will work for two urls. Do you have more than 2 urls? – Kumar V Feb 06 '14 at 07:47
  • ya Rohan's answer is current but i need to it with more urls – Salini L Feb 06 '14 at 07:49
  • @Salini you can create an array of all urls and use it – Rohan Kumar Feb 06 '14 at 07:54
  • @Rohan Now only i tried ur code but its not working no response and other links are also not working – Salini L Feb 06 '14 at 08:47
  • @Rohan I want to check whether there any substring matching to /sid/ in the current url and if any substring matches then i want to get the proceeding substring (means if url is ..../sid/110 then I want to pick that 110 to a variable X then I want to check whether its null or any value. If it has a value then remove that number from url and replace it with current value. please help me to solve this – Salini L Feb 06 '14 at 08:57
  • @Salini you can use `split()`, see my updated answer – Rohan Kumar Feb 06 '14 at 09:10
  • @RohanKumar Thank u Rohan thats nice answer. Why I didnt thought /sid instide of /sid/ :-( – Salini L Feb 06 '14 at 10:35
  • WC, don't know about your thinking. :) – Rohan Kumar Feb 06 '14 at 10:42
1

Ya I have Solved it. Here is my iavascript code

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


It works properly. Thanks @Roshan and @kumar for ur kind help

Rohan Kumar
  • 40,431
  • 11
  • 76
  • 106
Salini L
  • 829
  • 5
  • 15
  • 43