2009-05-29 12 views
5

के साथ jqGrid को सॉर्ट करना मैं एक jQuery नोब हूं, इसलिए मुझे यकीन है कि मुझे यहां कुछ आसान याद आ रहा है।एएसपी.नेट एमवीसी क्लाइंट व्यू में jQuery और LINQ-to-Entities

मुझे jqGrid एक ऐसी क्रिया के साथ काम कर रहा है जो LINQ-to-Entities ऑपरेशन से JSON डेटा बनाता है। लेकिन जब मैं ब्राउज़र में कॉलम हेडर पर क्लिक करता हूं, तो पंक्तियां क्रमबद्ध नहीं होती हैं। आरोही/अवरोही सूचक दिखाई देता है, लेकिन कुछ और नहीं होता है।

आवश्यक जावास्क्रिप्ट और सीएसएस लिंक मास्टर पृष्ठ शीर्षक में कर रहे हैं:

 
<script src="/Scripts/jquery-1.3.2.min.js" type="text/javascript"></script> 
<!-- CSS and JavaScript files for jqGrid to display on Details pages --> 
<link rel="stylesheet" type="text/css" href="/scripts/jQuery/jqGrid-3.4.4/themes/green/grid.css" title="green" media="screen" /> 
<script src="/Scripts/jQuery/jqGrid-3.4.4/jquery.jqGrid.js" type="text/javascript"></script> 
<script src="/Scripts/jQuery/jqGrid-3.4.4/js/jqModal.js" type="text/javascript"></script> 
<script src="/Scripts/jQuery/jqGrid-3.4.4/js/jqDnR.js" type="text/javascript"></script> 

यहाँ मेरी प्रवर्तन कोड है:

 
// jqGrid setup. 
$("#gridlist").jqGrid({ 
    url: '/Contact/GridData', 
    datatype: 'json', 
    mtype: 'GET', 
    colNames: ['ID', 'First Name', 'Last Name', 'Organization'], 
    colModel: [ 
     { name: 'id', index: 'id', width: 40, align: 'left', resizable: true }, 
     { name: 'first_name', index: 'first_name', width: 200, align: 'left', resizable: true, sortable: true, sorttype: "text" }, 
     { name: 'last_name', index: 'last_name', width: 200, align: 'left', resizable: true, sortable: true, sorttype: "text" }, 
     { name: 'organization', index: 'organization', width: 300, align: 'left', resizable: true, sortable: true, sorttype: "text"}], 
    pager: jQuery('#pager'), 
    rowNum: 5, 
    rowList: [5, 10, 20, 50], 
    repeatitems: false, 
    viewrecords: true, 
    imgpath: '/scripts/jQuery/jqGrid-3.4.4/themes/green/images', 
    caption: 'Contacts' 
});     

और यहाँ एचटीएमएल है:

 
    <h3>My Grid Data</h3> 
    <table id="gridlist" class="scroll" cellpadding="0" cellspacing="0"> 
    </table> 
    <div id="pager" class="scroll" style="text-align:center;"> 
    </div> 

और, सिर्फ पूर्णता के लिए, क्रिया विधि:

 
public ActionResult GridData() 
{ 
    var page = new { page = 1 }; 

    IEnumerable contacts = _db.ContactSet; 
    int i = 0; 
    var rows = new object[contacts.Count()]; 

    foreach (Contact contact in contacts) 
    { 
     rows[i] = new { id = contact.ID, cell = new[] { contact.ID.ToString(), contact.First_Name, contact.Last_Name, contact.Organization } }; 
     i++; 
    } 

    var result = new JsonResult(); 
    result.Data = new { page = 1, records = 2, rows, total = 1 }; 

    return result; 
} 

कोई विचार क्या स्पष्ट सेटिंग मैं यहां याद कर रहा हूं?

+0

क्या आप अपने सभी उत्तरों की सफाई करने और केवल नवीनतम जानकारी प्रदान करने पर विचार करेंगे? यह परिणाम है कि प्रक्रिया महत्वपूर्ण नहीं है। – Basic

उत्तर

4

इसे संभालने के दो बुनियादी तरीके हैं। ग्रिड डेटा को ही सॉर्ट कर सकता है। मुझे याद नहीं है कि इसे कैसे चालू करें, क्योंकि मैं कभी भी इस विकल्प का उपयोग नहीं करता हूं। आम तौर पर, मैं डेटासेट के साथ काम करता हूं जो पेज पर वापस जाने के लिए बहुत बड़े होते हैं, इसलिए मैं ग्रिड की पेजिंग सुविधाओं का उपयोग करता हूं। इस सर्वर पर इस तरह की आवश्यकता है, क्योंकि ग्रिड पूरे डेटासेट को नहीं देख पाएगा।

सर्वर पर पेजिंग करने के लिए, अपनी क्रिया के लिए एक सिडक्स और एक तार (दोनों तार) तर्क जोड़ें। क्रमबद्ध करने के लिए कॉलम का नाम एसआईडीएक्स होगा। तलवार दिशा, asc या desc होगा।

मेरे पास a demo project है जो दिखाता है कि यह कैसे करें (LINQ से ऑब्जेक्ट्स का उपयोग करके)। लेकिन LINQ से इकाइयों का उपयोग लगभग समान है; मैं अपने उत्पादन/गैर-डेमो कोड में LINQ से इकाइयों का उपयोग करता हूं। डेमो समाधान डाउनलोड करें और खुद के लिए देखो।

+0

धन्यवाद, क्रेग। मैंने कुछ दिन पहले ही आपकी पोस्ट देखी है। मेरा कोड ऐसा लगता है क्योंकि यह अब करता है (कोई सिडक्स और सॉर्ड) इसलिए नहीं है क्योंकि जब मैं उन्हें जोड़ता हूं और उनका उपयोग करने की कोशिश करता हूं, तो मैं खाली ग्रिड के साथ समाप्त होता हूं। –

2
I think below example should do it. 2 important points make sure your sort column has "it" keyword as prefix. (thats for linq to know). second you load only the number of objects array element as the rows the query can read. 

    var context = new HaackOverflowDataContext(); 

    int pageIndex = Convert.ToInt32(page) - 1; 
    int pageSize = rows; 
    int totalRecords = context.Question.Count(); 
    int totalPages = (int)Math.Ceiling((float)totalRecords/(float)pageSize); 

    var questions = context.Question.OrderBy("it."+ sidx + " " + sord).Skip(pageIndex * pageSize).Take(pageSize); 

    int i = 0; 
    var rowsObj = new object[pageSize>totalRecords ? totalRecords : pageSize]; 

    foreach (Question q in questions) 
    { 
     rowsObj[i] = new { id = q.Id, cell = new object[] { q.Id, q.Votes, q.Title } }; 
     i++; 
    } 

    var result = new JsonResult(); 
    result.Data = new 
    { 
     total = totalPages, 
     page = page, 
     records = totalRecords, 
     rows = rowsObj 
    }; 

    return result; 

धन्यवाद अनुज पांडेय www.anuj.co.in

वह वह ... करता है इसका मतलब मुझे पता है प्रोग्रामिंग :)

+0

इनपुट के लिए धन्यवाद। यह फिल हैक के पद से कोड जैसा दिखता है, जैसा कि मैंने मूल प्रश्न के उत्तर में कहा था, LINQ-to-Entities समाधान में काम करने में असफल रहा है। यह स्पष्ट रूप से LINQ से SQL में काम करता है। क्रेग के उदाहरण ने भी मदद की। हालांकि इसे पोस्ट करने के लिए धन्यवाद, मुझे अपने प्रश्न में इसका संदर्भ शामिल करना चाहिए था। –

1

ठीक है, मैं इस पोस्ट किया है चाहिए जब मैं लगा यह बाहर, लेकिन मैं दूसरे कार्यों में पकड़ा गया समाप्त हो गया। यहां बताया गया है कि मैंने LINQ से Entities के साथ काम किया, एक रिपोर्ट इकाई के लिए लागू किया गया। सबसे पहले, एक डिफ़ॉल्ट खोज के साथ jqGrid लोड करने के लिए कोड आसान था (एक बार मैं पता लगा कि मैं क्या याद किया था):


$(document).ready(function() { 

    // Set up jqGrid for the report search results table. 
    // This is displayed in a tab in the bottom section of the master page. 
    $("#searchResultList").jqGrid({ 
     url: '/Report/GetResultsL2E/', 
     datatype: 'json', 
     mtype: 'GET', 
     colNames: ['', 'ID', 'Title', 'Post_Date', 'Start_Date', 'End_Date', 'Summary'], 
     colModel: [ 
      { name: 'act', index: 'act', width: 75, sortable: false }, 
      { name: 'ID', index: 'ID', width: 40, align: 'left', hidden: true }, 
      { name: 'Title', index: 'Title', width: 150, align: 'left' }, 
      { name: 'Post_Date', index: 'Post_Date', width: 80, align: 'left', formatter: 'date' }, 
      { name: 'Start_Date', index: 'Start_Date', width: 80, align: 'left', formatter: 'date' }, 
      { name: 'End_Date', index: 'End_Date', width: 80, align: 'left', formatter: 'date' }, 
      { name: 'Summary', index: 'Summary', width: 240, align: 'left' } 
     ], 
     pager: jQuery('#searchResultPager'), 
     rowNum: 10, 
     rowList: [5, 10, 20, 50], 
     sortname: 'Title', 
     sortorder: "asc", 
     viewrecords: true, 
     imgpath: '/Scripts/jqGrid/themes/green/images', 
     caption: 'Report Search Results', 
     editurl: "/Report/Edit", 
     scroll: true, 
     height: 'auto', 
     recordtext: ' Reports', 
     pgtext: ' of ', 
     multiselect: true, 
     multiboxonly: true, //adds check box column 
     altRows: true, 
     loadComplete: function() { 
      var ids = jQuery("#searchResultList").getDataIDs(); 
      for (var i = 0; i "; 
       se = ""; 
       ce = ""; 
       jQuery("#searchResultList").setRowData(ids[i], { act: be + se + ce }) 
      } 
     } 
    }).navGrid('#searchResultPager', 
    { edit: false, add: false, del: false, search: false }, //options 
    {height: 280, reloadAfterSubmit: false }, // edit options 
    {height: 280, reloadAfterSubmit: false }, // add options 
    {reloadAfterSubmit: false }, // del options 
    {} // search options 
    ); 
}); 

डिफ़ॉल्ट खोज सेट लोड करने के लिए विधि के कुल सेट के पहले पृष्ठ रिटर्न उपलब्ध रिपोर्टों:

 
/// 
/// Query the ReportSet to return a paged, sorted set of Report entity properties in response to a call from a view. 
/// 
/// The name of the column to use for sorting. 
/// The order of the sort (ascending or descending). 
/// The number of the page to return to the calling process. 
/// The number of rows to return for the page. 
/// This ActionResult returns a JSON result to be used by a jqGrid using the jQuery library. 
/// jQuery requires a script tag linking the jQuery.js script. 
/// jqGrid requires stylesheet links to the following scripts and stylesheets: 
/// 
/// jQuery/themes/base/ui.all.css 
/// jqGrid/themes/green/grid.css (or other theme CSS file) 
/// jqGrid/jquery.jqGrid.js 
/// jqGrid/grid.base.js 
/// jqGrid/js/jqModal.js 
/// jqGrid/js/jqDnR.js 
/// 
public ActionResult GetResultsL2E(string sidx, string sord, int page, int rows) 
{ 
    int pageIndex = Convert.ToInt32(page) - 1; 
    int pageSize = rows; 
    int totalRecords = _db.ReportSet.Count(); 
    int totalPages = (int)Math.Ceiling((float)totalRecords/(float)pageSize); 
    int startRecord = pageIndex * pageSize; 

    List rowStrings = new List(); 
    // Get all of the reports in the model in a fixed order (for comparison). 
    //var reports = from item in _db.ReportSet 
    //    orderby item.Start_Date, item.Title 
    //    select new { item.ID, item.Title, item.Post_Date, 
    //    item.Start_Date, item.End_Date, item.Summary }; 
    // Get all of the reports in the model in a dynamic order passed from jqGrid. 
    string orderBytext = ""; 
    orderBytext = string.Format("it.{0} {1}", sidx, sord); 
    var reports = _db.ReportSet 
        .OrderBy(orderBytext); 

    List stringList = new List(); 

    int counter = reports.Count(); 
    foreach (var report in reports) 
    { 
     var rowString = new 
     { 
      id = report.ID, 
      cell = new[] { 
        "", 
        report.ID.ToString(), 
        report.Title, 
        report.Post_Date.ToShortDateString(), 
        report.Start_Date.ToShortDateString(), 
        report.End_Date.ToString(), 
        report.Summary.ToString()} 
     }; 
     stringList.Add(rowString); 
    } 

    var rowsOut = new object[counter]; 
    for (int i = 0; i

मैं बाद में एक और तरीका जोड़ा सॉर्ट करने के लिए स्तंभ का चयन उपयोगकर्ता को जवाब देने, PredicateBuilder Dynamically Composing Expression Predicates पर संक्षेप के खंड में से Albaharis 'किताब सी # पर चर्चा के प्रयोग से। मैंने अपने समाधान पर चर्चा की कि मैंने एमएसडीएन पर PredicateBuilder fails on nested predicates with LINQ to Entities

0

पर एक ही समस्या थी जहां jqGrid फ्रेम दिखाई दे रहा था लेकिन पंक्तियां नहीं थीं। मेरा समाधान निम्न कोड जोड़ना था।

**jsonData.JsonRequestBehavior = JsonRequestBehavior.AllowGet;** 
     return jsonData; 
संबंधित मुद्दे