Reklama
Pokazuje wyniki od 1 do 3 z 3

Temat: Pomoc z JS

  1. #1
    Avatar Rollercoster
    Data rejestracji
    2011
    Wiek
    29
    Posty
    1,247
    Siła reputacji
    14

    Domyślny Pomoc z JS

    Mam taki malutki problemik którego nie potrafię rozwiązać, mam kod:

    Kod PHP:
    /*
     *
     * Script by http://www.yvoschaap.com
     * 
     */
    
    /* request channel */
    function xhr() {};
    xhr.prototype.init = function() {
    	try {
    		this._xh = new XMLHttpRequest();
    	} catch (e) {
    		var _ie = new Array(
    		'MSXML2.XMLHTTP.5.0',
    		'MSXML2.XMLHTTP.4.0',
    		'MSXML2.XMLHTTP.3.0',
    		'MSXML2.XMLHTTP',
    		'Microsoft.XMLHTTP'
    		);
    		var success = false;
    		for (var i=0;i < _ie.length && !success; i++) {
    			try {
    				this._xh = new ActiveXObject(_ie[i]);
    				success = true;
    			} catch (e) {
    				
    			}
    		}
    		if ( !success ) {
    			return false;
    		}
    		return true;
    	}
    }
    
    xhr.prototype.wait = function() {
    	state = this._xh.readyState;
    	return (state && (state < 4));
    }
    
    xhr.prototype.process = function() {
    	if (this._xh.readyState == 4 && this._xh.status == 200) {
    		this.processed = true;
    	}
    }
    
    xhr.prototype.send = function(urlget,data) {
    	if (!this._xh) {
    		this.init();
    	}
    	if (!this.wait()) {
    		this._xh.open("GET",urlget,false);
    		this._xh.send(data);
    		if (this._xh.readyState == 4 && this._xh.status == 200) {
    			return this._xh.responseText;
    		}
    	}
    	return false;
    }
    
    var urlBase = "edit";
    var formVars = "";
    var changing = false;
    
    
    function fieldEnter(campo,evt,idfld) {
    	evt = (evt) ? evt : window.event;
    	if (evt.keyCode == 13 && campo.value!="") {
    		elem = document.getElementById( idfld );
    		remote = new xhr;
    		nt = remote.send(urlBase + "/" +encodeURIComponent(elem.id)+ "/"+encodeURIComponent(campo.value)+"/"+formVars,"");
    		//remove glow
    		noLight(elem);
    		elem.innerHTML = nt;
    		changing = false;
    		return false;
    	} else {
    		return true;
    	}
    
    
    }
    
    function fieldBlur(campo,idfld) {
    	if (campo.value!="") {
    		elem = document.getElementById( idfld );
    		remote = new xhr;
    		nt = remote.send(urlBase + "/" +encodeURIComponent(elem.id)+ "/"+encodeURIComponent(campo.value)+"/"+formVars,"");
    		elem.innerHTML = nt;
    		changing = false;
    		return false;
    	}
    }
    
    //edit field created
    function editBox(actual) {
    	//alert(actual.nodeName+' '+changing);
    	if(!changing){
    		width = widthEl(actual.id) + 20;
    		height =heightEl(actual.id) + 2;
    
    		if(height < 40){
    			if(width < 100)	width = 150;
    			actual.innerHTML = "<input id=\""+ actual.id +"_field\"  maxlength=\"254\" type=\"text\" value=\"" + actual.innerHTML + "\" onkeypress=\"return fieldEnter(this,event,'" + actual.id + "')\" onfocus=\"highLight(this);\" onblur=\"noLight(this); return fieldBlur(this,'" + actual.id + "');\" />";
    		}else{
    			if(width < 70) width = 90;
    			if(height < 50) height = 50;
    			actual.innerHTML = "<textarea name=\"textarea\" id=\""+ actual.id +"_field\"  onfocus=\"highLight(this);\" onblur=\"noLight(this); return fieldBlur(this,'" + actual.id + "');\">" + actual.innerHTML + "</textarea>";
    		}
    		changing = true;
    	}
    
    		actual.firstChild.focus();
    }
    
    
    
    //find all span tags with class editText and id as fieldname parsed to update script. add onclick function
    function editbox_init(){
    	if (!document.getElementsByTagName){ return; }
    	var spans = document.getElementsByTagName("span");
    
    	// loop through all span tags
    	for (var i=0; i<spans.length; i++){
    		var spn = spans[i];
    
            	if (((' '+spn.className+' ').indexOf("editText") != -1) && (spn.id)) {
    			spn.onclick = function () { editBox(this); }
    			spn.style.cursor = "pointer";
    			spn.title = "Click to edit!";	
           		}
    
    	}
    
    
    }
    
    //crossbrowser load function
    function addEvent(elm, evType, fn, useCapture)
    {
    	if (elm.addEventListener){
    		elm.addEventListener(evType, fn, useCapture);
    		return true;
    	} else if (elm.attachEvent){
    		var r = elm.attachEvent("on"+evType, fn);
    		return r;
    	} else {
    		//alert("Please upgrade your browser to use full functionality on this page");
    	}
    }
    
    //get width of text element
    function widthEl(span){
    	if(document.layers){
    		w=document.layers[span].clip.width;
    	} else if (document.all && !document.getElementById){
    		w=document.all[span].offsetWidth;
    	} else if(document.getElementById){
    		w=document.getElementById(span).offsetWidth;
    	}
    	return w;
    }
    
    //get height of text element
    function heightEl(span){
    
    	if(document.layers){
    		h=document.layers[span].clip.height;
    	} else if (document.all && !document.getElementById){
    		h=document.all[span].offsetHeight;
    	} else if(document.getElementById){
    		h=document.getElementById(span).offsetHeight;
    	}
    	return h;
    }
    
    function highLight(span){
    	//span.parentNode.style.border = "2px solid #D1FDCD";
    	//span.parentNode.style.padding = "0";
    	span.style.border = "1px solid #54CE43";          
    }
    
    function noLight(span){
    	//span.parentNode.style.border = "0px";
    	//span.parentNode.style.padding = "2px";
    	span.style.border = "0px";   
    }
    
    //sets post/get vars for update
    function setVarsForm(vars){
    	formVars  = vars;
    }
    
    addEvent(window, "load", editbox_init);
    Już tłumacze jak to działa.

    Na poczatku mamy:
    Kod PHP:
    <span id="namename" class="editText">Tekst</span>
    Po kliknięciu w tekst pojawia się ramko z możliwością edycji tekstu, możemy edytować tekst i wcisnąć enter.
    Wtedy ten tekst leci do kontrolera który sobie go tam obrabia itd.
    Problem w tym, że po zatwierdzeniu tego już nie widać zmian, tzn. wszystko znika i nie zostaje nic (w źródle strony nadal jest <span id="namename" class="editText">Tekst</span>)

    Mam nadzieję że mi pomożecie :) Zaraz postaram się udostępnić demko skryptu.
    Ostatnio zmieniony przez Rollercoster : 13-02-2014, 18:30

  2. #2
    Avatar joozioom
    Data rejestracji
    2007
    Posty
    1,460
    Siła reputacji
    17

    Domyślny

    Pokaż jeszcze ten skrypt do którego wysyłane jest żądanie ;d

  3. Reklama
  4. #3
    Avatar Rollercoster
    Data rejestracji
    2011
    Wiek
    29
    Posty
    1,247
    Siła reputacji
    14

    Domyślny

    Kod PHP:
          public function edit($type, $value)
        {
            if(is_numeric($type))
            {
                if($this->admin->update_stocks('ID', array('stock' => $value), $type) == TRUE)
                    return true;
                else
                    return false;
    
    
            }
            else
            {
                if($this->admin->update_stocks('WORLD_NAME', array('character_name' => $value), $type) == TRUE)
                    return true;
                else
                    return false;
    
    
            }
        }

    Kod jest mocno poglądowy, proszę się nim nie sugoerować.

    Funkcja edit jako argumenty przyjmuje kolejno span id oraz wpisany tekst.


    Chicałbym dodać że kod kontrolera i modelu działa znakomicie, tzn. fajnie się aktualizuje itd. Jedyny problem to żeby po wciśnięciu klawisza enter pojawiło się
    <span id="namename" class="editText">Zmieniony tekst</span>
    Ostatnio zmieniony przez Rollercoster : 13-02-2014, 19:23

Reklama

Informacje o temacie

Użytkownicy przeglądający temat

Aktualnie 1 użytkowników przegląda ten temat. (0 użytkowników i 1 gości)

Podobne tematy

  1. Odpowiedzi: 7
    Ostatni post: 26-02-2012, 23:05

Zakładki

Zakładki

Zasady postowania

  • Nie możesz pisać nowych tematów
  • Nie możesz pisać postów
  • Nie możesz używać załączników
  • Nie możesz edytować swoich postów
  •