This is a migrated thread and some comments may be shown as answers.

How to set checkbox in rows that have been persisted

1 Answer 43 Views
Grid
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
David
Top achievements
Rank 1
David asked on 07 Dec 2011, 04:48 AM

ASP.NET MVC 3 Razor

I know there is an example out there someplace but I’ve not been able to find it.

When binding to the Telerik grid (Ajax) how do you “check” all the checkboxes which are associated with data that has previously been persisted?

e.g. User is presented with 10 rows of data. S/he checks 4 rows (each row has a checkbox) and saves.  Tomorrow the user accesses the page and the 4 rows persisted previously will now be checked while the other 6 will not be.

Thanks in advance.

1 Answer, 1 is accepted

Sort by
0
David
Top achievements
Rank 1
answered on 08 Dec 2011, 05:46 PM

Here’s what I came up with based upon information found on the web.

-- View
@using GCSAdminPoco
  
<h3>Languages</h3>
  
@(Html.Telerik().Grid<LanguageListItem>()
    .Name("LanguagesToAddToWorkgroupGrid")
    .DataBinding(dataBinding => dataBinding
        .Ajax()
            .Select("SelectWorkgroupLanguages", "Workgroup")
        )
    .Columns(columns =>
    {
        columns.Bound(o => o.ID)
            .ClientTemplate("<input type='checkbox' name='checkedRecordsLanguages' />")
            .Title("")
            .Width(45)
            .HtmlAttributes(new { style = "text-align:center" });
          
        columns.Bound(c => c.FriendlyName).Title("Language");
              
    })
    .ClientEvents(events => events.OnRowDataBound("onRowDataBound"))
    .Scrollable(scrolling => scrolling.Height("400px"))
    .Filterable(filtering => filtering.Enabled(true))
    .Sortable()
    .NoRecordsTemplate("Currently there are no languages available.")
)

-- JavaScript
// Get a collection of attributes, languages, and channels.
function getAttributesLanguagesAndChannels() {
  
    var theAttributeIdCollection = BuildArrayOfCheckedRecords("checkedRecordsAttributes");
    var theLanguageIdCollection = BuildArrayOfCheckedRecords("checkedRecordsLanguages");
    var theChannelIdCollection = BuildArrayOfCheckedRecords("checkedRecordsChannels");
  
    return {
        AttributeIdCollection: theAttributeIdCollection,
        LanguageIdCollection: theLanguageIdCollection,
        ChannelIdCollection: theChannelIdCollection
    };
  
}
  
function BuildArrayOfCheckedRecords(checkboxName) {
  
    var theCollection = new Array();
    var loopCounter = 0;
  
    // Find all the checked checkboxes.
    jQuery("input[name=" + checkboxName + "]:checked").each
        (
          function () {
  
              // Fill the array with the values.
              theCollection[loopCounter] = jQuery(this).val();
              loopCounter += 1;
          }
        );
  
    return theCollection;
}
  
  
function onClickSaveWorkgroupAttributesLanguagesAndChannels() {
  
    var jsonData = JSON.stringify(getAttributesLanguagesAndChannels());
  
    $.ajax({
        url: 'Workgroup/LinkAttributesLanguagesAndChannelsToWorkgroup',
        type: 'POST',
        data: jsonData,
        contentType: 'application/json; charset=utf-8',
        success: function (data) {
            //if (data == 'True') {
  
            // Clear any old messages.
            $('#workgroup_attribute_status_message').html("");
  
            if (data == 0) {
  
                //alert("Successfully saved.");
                //$('#workgroup_attribute_status_message').html("<p>Successfully saved.</p>");
                $('#workgroup_attribute_status_message').html("Successfully saved.");
  
                //
                // Refresh needed grids.
                //
                var grid = $('#AttributesToAddToWorkgroupGrid');
                grid.data().tGrid.rebind();
  
                grid = $('#LanguagesToAddToWorkgroupGrid');
                grid.data().tGrid.rebind();
  
                grid = $('#ChannelsToAddToWorkgroupGrid');
                grid.data().tGrid.rebind();
  
            }
            else if (data == -100) {
  
                alert("Save did not complete.\nError code = " + data);
            }
            else if (data == -200) {
  
                alert("Save did not complete.\nThis set of attributes, languages, and channels is already being used by a different workgroup.");
  
            }
            else if (data == -300) {
  
                alert("Save did not complete.\nCould not locate the specified workgroup.");
            }
            else {
                alert("Unable to save attributes, languages, and/or channels.\nError code = " + data);
            }
        },
        error: function (result, err) {
            alert("Unable to save any attributes, languages, and/or channels.");
        }
  
    });
  
}

-- Controller
[HttpPost]
public int LinkAttributesLanguagesAndChannelsToWorkgroup(int[] attributeIdCollection, int[] languageIdCollection, int[] channelIdCollection)
{
    ...
}
Tags
Grid
Asked by
David
Top achievements
Rank 1
Answers by
David
Top achievements
Rank 1
Share this question
or