Using Sharepoint Multi-Lookup selected values to set checkboxes in datatables table

Using Sharepoint Multi-Lookup selected values to set checkboxes in datatables table

Zer0AdminZer0Admin Posts: 6Questions: 2Answers: 0

I am able to get the already selected items in a multi-lookup field in my sharepoint list.

What I am having trouble doing is actually setting the already selected fields in my table based on what was already selected in the multi-lookup list item.

The block of code I am focused on is:

$("[title='Applicable Requests selected values'] option").each(function () {

    IdRequest = ReturnRequestId($(this).text());

    table.$('tr', {'search':IdRequest}).prop('checked',true);

});  

This block is called as a last step when the page is loaded. Again I am only checking those that are already checked. Any nudge is appreciated.

Answers

  • allanallan Posts: 63,498Questions: 1Answers: 10,470 Site admin

    Couuld you give a link to the page so we can take a look please? I'm not really familiar with how SharePoint works.

    Allan

  • Zer0AdminZer0Admin Posts: 6Questions: 2Answers: 0

    Unfortunately the site is not externally exposed. Be glad, keep your distance. SharePoint is like a black hole.My plan was to use the search to locate the rows I needed checked and check them. What seems to be happening is that I am checking all of them instead. It seems that I should probably iterate through the table and set the checkbox for those that match. I will give that a go first.

  • Zer0AdminZer0Admin Posts: 6Questions: 2Answers: 0
    edited December 2015

    Below is the code for my most recent attempt.

    function markRequest(table) {
    
        var selected_values=[]; 
    //iterates through SharePoint selected values multi lookup and gets already selected vals
        $("[title='Applicable Requests selected values'] option").each(function () { 
            IdRequest = ReturnRequestId($(this).text());
            selected_values.push(IdRequest); //fills array with selected values
        });  
    
       //begins iterating through table to check the checkboxes of the ones that match
        table.rows().every( function () {
            var d = this.data();
     
        if($.inArray(selected_values,d[3]) !== -1){
          table.row().find('input[type="checkbox"]').prop('checked', true);
         table.row().addClass('selected');
        }
    
       d.counter++; // update data source for the row
     
       this.invalidate();
    });
     
    // Draw once all updates are done
    table.draw();   
    

    Edited by Allan - Syntax highlighting. Details on how to highlight code using markdown can be found in this guide.

  • glendersonglenderson Posts: 231Questions: 11Answers: 29

    I'm not seeing where you set the .data() to your updated values. 'd' is the object of the columns, so you need to push that back into the table memory and .draw() should then render it into HTML.

    Something like

    d[3] = "<input type='checkbox' checked'>" // Assuming the checkbox is the 4th element.
    d.counter ++;  // Assuming that "counter" is a named column
    
    this.data(d) // Push the updated data array back into the table
    
  • Zer0AdminZer0Admin Posts: 6Questions: 2Answers: 0
    edited December 2015

    As I was reviewing all the code, I noticed the rowCallback which is basically doing the same thing I wanted to do. I solved my problem by placing

    $("[title='Applicable Requests selected values'] option").each(function () {
        IdRequest = ReturnRequestId($(this).text());
        rows_selected.push(IdRequest);
    });  
    

    just after

    $(document).ready(function() {

    var rows_selected = [];
    

    //This actually caused other issues so this is not the final solutions.

This discussion has been closed.