2016-07-01 7 views
1

Ich habe das folgende Diagramm mit Google Organigramm erstellt, aber ich habe einige Probleme. Wenn ich die Seite öffne oder wenn ich die Seite aktualisiere, erscheint das vollständige Diagramm wie im Bild unten. Aber ich möchte nur die Gegenstände, die im roten Quadrat sind, gezeigt werden, wie ich das tun kann.Wie wird nur 1 Artikel in C# google Organigramm angezeigt?

Ich möchte nicht ein paar Elemente aus der Liste, aber wenn ich die Seite öffnen, zeigt es das gesamte Diagramm mit seinem All-Liste Element und stattdessen beim Öffnen möchte ich nur 5 Elemente anzuzeigen und Benutzer erweitern andere verstehst du mich?

Dies ist das Bild der Grafik, wie es aussieht und wie ich es will!

Und hier ist mein voller C# -Code:

namespace OrganizationChartUsingGoogleAPI.OrganizationChart 
{ 
public static class ConvertTo 
{ 
    /// <summary> 
    /// Convert the object type in the given type and handle the DBNULL. 
    /// </summary> 
    /// <typeparam name="T">Type to convert</typeparam> 
    /// <param name="value">value</param> 
    /// <returns>converted value and if DBNULL then return the type's default value i.e. string = string.Empty, Int16=0 </returns> 
    public static T CastIn<T>(this object value) where T : IConvertible 
    { 
     if (value == DBNull.Value) 
      if (typeof(T) == typeof(string)) 
      { 
       return (T)Convert.ChangeType(string.Empty, typeof(T)); 
      } 
      else 
      { 
       return default(T); 
      } 


     if (typeof(T) == typeof(bool)) 
      return (T)Convert.ChangeType(Convert.ToInt32(value), typeof(T)); 

     return (T)Convert.ChangeType(value, typeof(T)); 
    } 
} 

public partial class OrganizationChartUserControl : UserControl 
{ 
    //Get the List name to fetch the data from 
    string listName = "OrgChart_Demo"; 
    int iRowCounter = 0; 
    string sAllNewRows = string.Empty; 

    protected void Page_Load(object sender, EventArgs e) 
    { 
     //Fetch the data (recursively) from the list 
     GetNode(string.Empty); 

     //Generate the Client Script and Register 
     GenerateClientScript(sAllNewRows); 
    } 

    private void GenerateClientScript(string sAllNewRows) 
    { 
     string csName1 = "OrgChartScript"; 
     Type csType = this.GetType(); 

     ClientScriptManager cs = Page.ClientScript; 

     // Check to see if the startup script is already registered. 
     if (!cs.IsStartupScriptRegistered(csType, csName1)) 
     { 
      StringBuilder cstext = new StringBuilder(); 
      cstext.Append("<script type='text/javascript' src='https://www.google.com/jsapi'></script>"); 
      cstext.Append("<script type='text/javascript'>"); 


      cstext.Append("google.load('visualization', '1', { packages: ['orgchart'] });"); 

      cstext.Append("google.setOnLoadCallback(drawChart);"); 

      cstext.Append("function drawChart() {"); 
      cstext.Append("var data = new google.visualization.DataTable();"); 

      cstext.Append("data.addColumn('string', 'Name');");     
      cstext.Append("data.addColumn('string', 'Manager');"); 
      cstext.Append("data.addColumn('string', 'ToolTip');"); 

      cstext.Append("var rowArr = new Array();"); 

      cstext.Append(sAllNewRows); 

      cstext.Append("data.addRows(rowArr);"); 

      cstext.Append("var chart = new google.visualization.OrgChart(document.getElementById('chart_div'));"); 
      cstext.Append("chart.draw(data, { allowHtml: true, allowCollapse: true });"); 
      cstext.Append("}"); 
      cstext.Append("</script>"); 
      cs.RegisterClientScriptBlock(csType, csName1, cstext.ToString(), false); 

     } 

    } 

    private void GetNode(string reportsTo) 
    { 
     SPListItemCollection itemCol = GetListItems(listName, reportsTo); 

     foreach (SPListItem item in itemCol) 
     { 
      //create a new row 
      sAllNewRows += createNewRow(item); 

      //Recursion 
      GetNode(item["Name"].ToString()); 
     } 

    } 

    private string createNewRow(SPListItem listItem) 
    { 

     //Converting list items to strings. 
     string sName = ConvertTo.CastIn<string>(listItem["Name"]); 
     string sTitle = ConvertTo.CastIn<string>(listItem["Title"]); 
     string sMoreInfo = ConvertTo.CastIn<string>(listItem["MoreInfo"]); 
     string sReportsTo = ConvertTo.CastIn<string>(listItem["ReportsTo"]); 

     //Checking if image field ref is empty or null (if it does't provide any img source link!) 
     ImageFieldValue pageImage = listItem["Pageimage"] as ImageFieldValue; 
     string sPicture = string.IsNullOrEmpty(pageImage.ImageUrl) ? "#" : pageImage.ImageUrl; 


     StringBuilder sText = new StringBuilder(); 
     sText.Append("var NewRow = new Array();"); 

     //this row shows the image and everything else what chart needs 
     sText.Append(String.Format("NewRow.push({{ v: '{0}', f: '<img src =\"{1}\" style=\"width:57px; height:57px; float:left;\" />{2}<div style=\"color:white; font-style:Arial\">{3}</div>' }});", sName, sPicture, sName, sTitle)); 

     sText.Append(String.Format("NewRow.push('{0}');", sReportsTo)); 
     sText.Append(String.Format("NewRow.push('{0}');", sMoreInfo)); 
     //sText.Append(String.Format("NewRow.push('{0}');", sPicture)); 
     sText.Append(String.Format("rowArr[{0}] = NewRow;", iRowCounter)); 

     //Pageimage 

     iRowCounter++; 
     return sText.ToString(); 
    } 

    private SPListItemCollection GetListItems(string destList, string reportsTo) 
    { 
     SPListItemCollection ResultListItems = null; 

     using (SPSite oSite = new SPSite(SPContext.Current.Web.Url)) 
     { 
      using (SPWeb oWeb = oSite.OpenWeb()) 
      { 
       SPList list = oWeb.Lists.TryGetList(destList); 
       if (null == list) 
        return ResultListItems; 

       string selected = DropDownList2.SelectedValue; 
       // Label1.Text = selected; 

       //Check if the item already exist. 
       StringBuilder sCAMLQuery = new StringBuilder(string.Empty); 
       sCAMLQuery.Append("<Where>"); 
       sCAMLQuery.Append("<And>"); 
       if (reportsTo != string.Empty) 
       { 
        sCAMLQuery.Append("<Eq>"); 
        sCAMLQuery.Append("<FieldRef Name='ReportsTo' />"); 
        //sCAMLQuery.Append("<Value Type='Lookup'>" + reportsTo + "</Value>"); 
        sCAMLQuery.Append("<Value Type='Text'>" + reportsTo + "</Value>"); 

        sCAMLQuery.Append("</Eq>"); 

       } 
       else 
       { 
        sCAMLQuery.Append("<IsNull>"); 
        sCAMLQuery.Append("<FieldRef Name='ReportsTo' />"); 
        sCAMLQuery.Append("</IsNull>"); 
       } 
       sCAMLQuery.Append("<Eq>"); 
       sCAMLQuery.Append("<FieldRef Name='Kompania' />"); 
       //sCAMLQuery.Append("<Value Type='Lookup'>" + reportsTo + "</Value>"); 
       sCAMLQuery.Append("<Value Type='Text'>" + selected + "</Value>"); 

       sCAMLQuery.Append("</Eq>"); 

       sCAMLQuery.Append("</And>"); 
       sCAMLQuery.Append("</Where>"); 

       SPQuery QueryResult = new SPQuery(); 
       QueryResult.Query = sCAMLQuery.ToString(); 
       ResultListItems = list.GetItems(QueryResult); 
      } 
     } 
     return ResultListItems;   
    }    
    } 
} 

Dank euch alle

Antwort

1

Ich sehe, Sie haben einen GetNode (string ReportsTo).

Könnten Sie das Level, in dem Sie sich befinden, weitergeben und zurückkehren, wenn das Level höher als gewünscht ist?

private void GetNode(string reportsTo, int level) 
{ 
    // maybe have some config or value you can set for the max level you want. 
    if (level >= 2) return; 

    SPListItemCollection itemCol = GetListItems(listName, reportsTo); 

    foreach (SPListItem item in itemCol) 
    { 
     //create a new row 
     sAllNewRows += createNewRow(item); 

     //Recursion 
     GetNode(item["Name"].ToString(). ++level); 
    } 

} 
+0

Ich habe auch diese Methode 'GetNode (string.Empty)' genannt, und ich muss einen Level-Wert übergeben, könnte ich eine 2 übergeben? – Iamnderon

+0

Ich möchte nicht ein paar Elemente aus der Liste, aber wenn ich die Seite öffnen, zeigt es das gesamte Diagramm mit seinen 50 Element und stattdessen whet es öffnet Ich möchte nur 5 Elemente anzeigen und Benutzer erweitern andere verstehen Sie mich ? – Iamnderon

Verwandte Themen