Sunday, September 23, 2012

Easy Sort Html Table / Grid View using JQuery example

1st u need the gloruis sortElements by James Padolsey (well after getting JQ) 
http://james.padolsey.com/javascript/sorting-elements-with-jquery/

now grid views gives us a row with TH and the rest r TD so we need:
1 - take all the TRs
2 - build the compare callback func
3 - make it skip the TH
4 - be able to sort desc

so here, i put this just after James's code:

function MakeTableSortable(tableID) {
  $("#" + tableID + " th").click(function () {
    var index = this.cellIndex;
    if (this.abc == null) {
      this.abc = true;
    }             else {
      this.abc = !this.abc;
    }

    $(
"#" + tableID + " tr").sortElements(function (a, b) {
      if (a.cells[0].tagName == "TH" || b.cells[0].tagName == "TH") {
        return 0;
      }       var _a = a.cells[index].innerText.toLowerCase();
      var _b = b.cells[index].innerText.toLowerCase();
      if (!a.parentNode.rows[0].cells[index].abc) {
        if (_a > _b) return -1;
        if (_a < _b) return 1;
        return 0;
      }       if (_a > _b) return 1;
      if (_a < _b) return -1;
      return 0;
    });
  });
}


1 - take all the TRs - by puttign the id and after that an element tag name u get all the childs with that tag name, it can work with name just dont put '#'
2 - build the compare callback func - that is this part (comaring the string values):
      var _a = a.cells[index].innerText.toLowerCase();...
      if (_a > _b) return 1;...
3 - make it skip the TH - if tagName == 'TH' - case sensitive
4 - be able to sort desc - all the abc part, its just a flag in the TH

p.s. - if u add this on James's .js file u now only need to add a ref for it in any html doc and u have ur talbes auto sortable :)

window.onload = function () {
  var tables = document.getElementsByTagName("table");
  for (var i = 0; i < tables.length; i++) {
    MakeTableSortable(tables[i].id);
  }
}


see also http://bresleveloper.blogspot.co.il/2012/10/advanced-htmltable-gridview-auto-sort.html
example with the advanced sort http://jsbin.com/agijer/2/edit

Create Class in Javascript example

taken right from here (a hebrew blog, no. 2 in list)
http://www.softwarearchiblog.com/2012/07/jquery-c-java.html

var myNS = myNS || {}; // ns is optional o.c.
myNS.Calculator = function () {
  // private members
  var value = 0;

  var addBy = function (x) {
    value += x;
    console.log(
'value = ' + value);
  };
  var multiplyBy = function (x) {
    value *= x;
    console.log(
'value = ' + value);
  };

  
return { // public parts (aka interface)
    addBy: addBy,
    multiplyBy: multiplyBy
  };
};
var calc = new myNS.Calculator();
calc.addBy(4);
calc.multiplyBy(6);
// 24

Monday, September 3, 2012

pass Info from web.app to Dll

in the web.app:
Configuration config = WebConfigurationManager.OpenWebConfiguration("~/");
#if QA
config.AppSettings.Settings.Add("EnviormentVar", "QA");
#endif
config.Save();

in the dll:
System.Configuration.Configuration config = WebConfigurationManager.OpenWebConfiguration("~/");
KeyValueConfigurationElement Appsetting = config.AppSettings.Settings["EnviormentVar"];
return (Appsetting.Key + " <br/>" + "Value:" + Appsetting.Value);

u'll get: 
Appsetting.Value = QA