Show results in an ordered way in html page - javascript

I am new to html and i want to consult you about an issue i have.
I have a json object with this structure (only example):
{
[{
ServerName: "Server1",
TestResults: [{
Name: "Test1",
ErrorMessage: "",
Success: true
},
Name: "Test1", ErrorMessage: "", Success: true
}]
}, {
ServerName: "Server2",
TestResults: [{
Name: "Test1",
ErrorMessage: "",
Success: true
},
Name: "Test1", ErrorMessage: "", Success: true
}]
}]
}
My javascript code gets this object and suppose to create from it something like this:
<ul>
<lh>ServerName</lh>
<li>TestName</li>
<li>TestName</li>
<lh>ServerName2</lh>
<li>TestName</li>
<li>TestName</li>
.....
</ul>
I want to know how do i create such a list that each 'lh' has a line of its own,
and each 2-3 'li' are in the same line.
Can you help me find the best solution for it?
P.S - I am not using a table because it has some problems in jquery mobile that sometimes make it not fitted for the page size.

If I understand correctly, you can add some css for that:
ul>lh{
display: block;
}
ul>li{
display: inline;
}
See this jsfiddle.

Use a definition list (<dl>) and style it with css. Add a unique class to your list to avoid conflict with other CSS styles: jsfiddle
HTML
<dl class="yourclass">
<dt>ServerName</dt>
<dd>TestName</dd>
<dd>TestName</dd>
<dt>ServerName2</dt>
<dd>TestName</dd>
<dd>TestName</dd>
</dl>
CSS
dl.yourclass dd {
display:inline;
}

Related

Sorting a column display hidden rows in jqGrid

I have some rows I hide like that:
$("#"+rowid).hide();
My problem is that when the user clicks to sort a colmun, the hidden rows reappeared. There is a way to avoid this?
EDIT
I will try to explain a little bit more what I did with code example.
I start to create my grid with this params (and without datas).
var params = {
datatype: "local",
data: [],
caption: "Grid",
colNames:[ "Column A", "Column B" ],
colModel:[
{ name:"colA", key: true },
{ name:"colB" }
]
};
For some reasons, I reload next the grid with datas, like this:
$("#myGrid").jqGrid("clearGridData")
.jqGrid("setGridParam", { data: myDatas })
.trigger("reloadGrid");
And I have checkboxes with listeners, like this one:
$("#checkbox1").on("change", onCheckbox1Changed);
function onCheckbox1Changed() {
var rowid = ...;
var datas = $("#myGrid").jqGrid("getRowData");
for(var key in datas) {
if(datas[keys].colB === "" && $("#checkbox1").val() === true) {
$("#"+rowid).show();
} else if(datas[keys].colB === "" && $("#checkbox1").val() === false) {
$("#"+rowid).hide();
}
}
}
This code works like I want. Rows are hidden/shown depending on checkboxes. The problem is when I clik on a column to sort it, the hidden columns reappeared.
EDIT 2
I could force the grid to hide the rows after a sort. But I didn't find where I can find an event like "afterSort". There is "onSortCol" but it is called before the sort.
A solution will be to force that using "loadComplete". Like this:
var params = {
// ...
loadComplete: onLoadComplete
}
function onLoadComplete() {
onCheckbox1Changed();
}
I tried it and it works. But I am not very "fan" with this solution.
I find hiding some rows after displaying the page of data not the best choice. The main disadvantage is the number of rows which will be displayed. You can safe use $("#"+rowid).hide(); method inside of loadComplete only if you need to display one page of the data. Even in the case one can see some incorrect information. For example, one can use viewrecords: true option, which place the text like "View 1 - 10 of 12" on right part of the pager.
I personally would recommend you to filter the data. You need to add search: true option to the grid and specify postData.filters, which excludes some rows from displaying:
search: true,
postData: {
filters: {
groupOp: "AND",
rules: [
{ field: "colA", op: "ne", data: "rowid1" },
{ field: "colA", op: "ne", data: "rowid2" }
]
}
}
If you would upgrade from old jqGrid 4.6 to the current version (4.13.6) of free jqGrid, then you can use "ni" (NOT IN) operation:
search: true,
postData: {
filters: {
groupOp: "AND",
rules: [
{ op: "ni", field: "id", data: "rowid1,rowid2" }
]
}
}
In both cases jqGrid will first filter the local data based on the filter rules and then it will display the current page of data. As the result you will have the perfect results.
Sorting of such grid will not not change the filter.
Don't hide columns after the creation of the table, hide them directly when you create the grid using the option hidden, like this:
colNames: ['Id', ...],
colModel: [
{ key: true, hidden: true, name: 'Id', index: 'Id' },
....
]
If you want to hide columns on particular events after the creation of the grid, look at this article.
Hope it was you were looking for.

How to iterate over a div enyos component?

enyo.kind({
name: "Usr",
kind: enyo.Control,
published: {
username: "",
ToDo: [],
Done:[]
},
components: [
{tag:"title",name:"username"},
{tag:"input", name:"desc", placeholder: "to do..",
style:"width:500px; static: left; padding-right: 10px"},
{tag: "button", content: "New Task", ontap: "addTask",
style:"float: right; padding-left: 10px;
background-color:orange; color:white" },
{tag: "div", name: "ToDo" }
],
addTask: function(inSource, inEvent) {
this.createComponent({
kind: Task,
container: this.$.ToDo,
text: this.$.desc.hasNode().value,
state: "To do"
});
this.$.ToDo.render();
}
})
So, this is what i have. As you can see, I create new elements (Task kind is defined in other js file, doesn´t really matters) using (addTask) and putting them inside the ToDo "div" component. How can I iterate over those elements? something like a 'for each' sentence or so, maybe is there a $'div'.length property or something? I haven´t found an answer in any official documentation.

Javascript library for table rendering

I need to show an array of objects in the table like representation. Table has columns with the properties, and when clicked on the column it should show more data inside the table. It should be sortable.
Is there a JS library that could do this, so I dont have to write this from scratch?
Please see the attached image with the JSON object.
When the user clicks on Ana, additional row is inserted.
I created the demo https://jsfiddle.net/OlegKi/kc2537ty/1/ which demonstrates the usage of free jqGrid with subgrids. It displays the results like
after the user clicks on the "+" icon in the second line.
The corresponding code you can find below
var mydata = [
{ id: 10, name: "John", lname: "Smith", age: 31, loc: { location: "North America", city: "Seattle", country: "US" } },
{ id: 20, name: "Ana", lname: "Maria", age: 43, loc: { location: "Europe", city: "London", country: "UK" } }
];
$("#grid").jqGrid({
data: mydata,
colModel: [
{ name: "name", label: "Name" },
{ name: "lname", label: "Last name" },
{ name: "age", label: "Age", template: "integer", align: "center" }
],
cmTemplate: { align: "center", width: 150 },
sortname: "age",
iconSet: "fontAwesome",
subGrid: true,
subGridRowExpanded: function (subgridDivId, rowid) {
var $subgrid = $("<table id='" + subgridDivId + "_t'></table>"),
subgridData = [$(this).jqGrid("getLocalRow", rowid).loc];
$("#" + subgridDivId).append($subgrid);
$subgrid.jqGrid({
idPrefix: rowid + "_",
data: subgridData,
colModel: [
{ name: "location", label: "Localtion" },
{ name: "city", label: "City" },
{ name: "country", label: "Country" }
],
cmTemplate: { align: "center" },
iconSet: "fontAwesome",
autowidth: true
});
}
});
Small comments to the code. Free jqGrid saves all properties of input data in data parameter. I added id property to every item of input data. It's not mandatory, but it could be helpful if you would add more functionality to the grid. See the introduction for more details.
The columns are sortable based on the type of the data specified by sorttype property of colModel. To simplify usage some standard types of data free jqGrid provides some standard templates which are shortcurts for some set of settings. I used template: "integer" in the demo, but you could replace it to sorttype: "integer" if only sorting by integer functionality is important.
If the user click on "+" icon to expand the subgrid then jqGrid inserts new row and creates the div for the data part of the subgrid. You can replace subGridRowExpanded from above example to the following
subGridRowExpanded: function (subgridDivId) {
$("#" + subgridDivId).html("<em>simple subgrid data</em>");
}
to understand what I mean. The unique id of the div will be the first parameter of the callback. One can create any common HTML content in the subgrid. Thus one can create empty <table>, append it to the subgrid div and
then convert the table to the subgrid.
To access to the item of data, which corresponds to the expanding row one can use $(this).jqGrid("getLocalRow", rowid). The return data is the item of original data. It has loc property which we need. To be able to use the data as input for jqGrid we create array with the element. I's mostly all, what one have to know to understand how the above code works.
You can add call of .jqGrid("filterToolbar") to be able to filter the data or to add pager: true (or toppager: true, or both) to have the pager and to use rowNum: 5 to specify the number of rows in the page. In the way you can load relatively large set of data in the grid and the user can use local paging, sorting and filtering. See the demo which shows the performance of loading, sorting and filtering of the local grid with 4000 rows and another one with 40000 rows. All works pretty quickly if one uses local paging and not displays all the data at once.
I use datatables.net for all my "more complex than lists"-tables. I It's a very well kept library with loads of features and great flexibility.
In the "con" column I would say that it's so complex that it probably has quite a steep learning curve. Although the documentation is great so there is always hope for most problems.

How to read a multidimensionnal javascript object?

First of all, here is my Javascript object :
var languages = {
languages: [
{ name: "French", locale: "FR", id: "-1" },
{ name: "English", locale: "IT", id: "-2" },
{ name: "Spanish", locale: "ES", id: "-3" },
{ name: "Zoulou", locale: "ZL", id: "-4" },
{ name: "Italian", locale: "EN", id: "-5" }
]
};
I'm using Mustache.js to generate language buttons:
function generateLanguages(languages) {
var output = $("#languages-output");
var template = "{{#languages}}<button id={{id}}><img src=#FLAG onclick='changeLanguage({{locale}})' /><p>{{name}}</p></button>{{/languages}}";
html = Mustache.render(template, languages);
output.append(html);}
I also have a folder containing all the flags images called flags (e.g flags/English.png).
I'm trying to generate the buttons by adding the corresponding flag.
I really have no idea how to do it, I thought to use the "locale" property of the Javascript object in a for loop and for each locale, create a big switch to choose the right image. If someone can help me ?
http://jsfiddle.net/lBrowz/7w5grype/
A few improvements and ideas:
Move templates to your HTML. That's where they should be. Reference them from JS by their ID. It's a lot easier to modify and think about templates when they are outside of the JavaScript.
Maybe using flags for languages isn't the best choice. Sometimes a language cannot accurately be represented by a flag (see Zulu, which I chose to represent by the flag of Mozambique, but that might not be what your users expect. The same is true for English, when you think about it.)
Anyway, instead of one flag image per language, use a single image that contains all flags and then use CSS sprites to display selected flags. I used this project here: http://tkrotoff.github.io/famfamfam_flags/
Use data-* attributes to store extra information in elements.
Use event delegation to handle clicks. Never use inline event handlers (like onclick).
var languages = {
languages: [
{ name: "French", locale: "FR", id: "-1", cls: "famfamfam-flag-fr" },
{ name: "English", locale: "IT", id: "-2", cls: "famfamfam-flag-gb" },
{ name: "Spanish", locale: "ES", id: "-3", cls: "famfamfam-flag-es" },
{ name: "Zoulou", locale: "ZL", id: "-4", cls: "famfamfam-flag-mz" },
{ name: "Italian", locale: "EN", id: "-5", cls: "famfamfam-flag-it" }
]
};
function changeLanguage(locale) {
$("#locale").text(locale);
}
$(function () {
var languagesTemplate = $("#languages-template").html();
$("#languages-output").html( Mustache.render(languagesTemplate, languages) );
$(document).on("click", ".language-button", function () {
var locale = $(this).data("locale");
changeLanguage(locale);
});
});
.language-button {
width: 5em;
height: 4em;
margin: 3px;
}
.language-button > span {
display: inline-block;
margin: 2px;
}
<link href="http://tkrotoff.github.io/famfamfam_flags/famfamfam-flags.css" rel="stylesheet"/>
<script src="http://cdnjs.cloudflare.com/ajax/libs/mustache.js/0.7.0/mustache.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<div id="languages-output"></div>
<div id="locale"></div>
<script type="text/x-handlebars-template" id="languages-template">
{{#languages}}
<button id="{{id}}" class="language-button" data-locale="{{locale}}">
<span class="{{cls}}"></span>
<span>{{name}}</span>
</button>
{{/languages}}
</script>
Run the code snippet above to see it live.

how to validate div element in angular UI bootstrap?

I make view from array of object into view .But My problem is that I take div element inside the li and ui .Now I want to validate element like "number", "url","email".I need to validate element on blur and keyup.Some elements may be required some not .I need to show this using "*".I know how to validate using form when I take form I used
<span ng-show="myForm.name.$error.required" class="help-inline">
But now i have div element inside li .how I will validate .?
http://plnkr.co/edit/we1QHuDuCOOR4tDAk6yv?p=preview
function Controller($scope) {
$scope.outputs = {};
$scope.inputs = [{
type: "email",
name: "email",
required:true
}, {
type: "text",
name: "name",
}, {
type: "number",
name: "phonenumber",
}, {
type: "checkbox",
name: "whant to check",
},
{
type: "url",
name: "server Url",
}];
}
or is there any way to insert all element in form ?
You can use Angular Form Validation.
When i was searching about this, i found out this tutorial. It has helped me alot.
Try your self.

Categories